pgpubsub
Async PostgreSQL LISTEN/NOTIFY pub/sub client built on tokio-postgres.
Features
- Subscribe to PostgreSQL notification channels with automatic LISTEN/UNLISTEN management
- Publish notifications with NOTIFY, including atomic batches via
notify_batch - Suppress notifications sent by the same connection
- Configurable broadcast channel capacity
- TLS support via
tokio-postgresTLS backends - Automatic reconnection with exponential backoff (via backon):
active subscriptions are re-LISTENed once the connection is restored. Notifications
published while the connection is down are lost — NOTIFY is fire-and-forget — and
listen/notifycalls made during an outage fail fast with an error. SubscriptionisSend + 'static, so it works withtokio::spawn
Usage
use ;
async
Running the example
Then send a notification from another terminal:
Running the tests
Unit tests run with plain cargo test. The integration tests (round-trip delivery,
batch atomicity, own-notification suppression, and reconnect-after-backend-termination)
need the docker-compose database and are ignored by default:
Connection options
Connect with individual parameters:
new
Connect with a connection string:
from_connection_str
Connect with a tokio_postgres::Config:
use Config;
let mut config = new;
config.host;
config.dbname;
from_tokio_postgres_config
License
MIT