Skip to main content

Crate pgpubsub

Crate pgpubsub 

Source
Expand description

Async PostgreSQL LISTEN/NOTIFY pub/sub client built on tokio-postgres.

§Example

use pgpubsub::{PgPubSub, PgPubSubOptionsBuilder, RecvError};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let options = PgPubSubOptionsBuilder::new("localhost", "mydb", "user", "pass")
        .channel_capacity(16)
        .build();

    let pubsub = PgPubSub::connect(options).await?;
    let mut subscription = pubsub.listen("my_channel").await?;

    loop {
        match subscription.recv().await {
            Ok(n) => println!("{}: {}", n.channel, n.payload),
            Err(RecvError::Lagged(n)) => eprintln!("lagged, {n} dropped"),
            Err(RecvError::Closed) => break,
            Err(err) => { eprintln!("{err}"); break; }
        }
    }
    Ok(())
}

Modules§

tokio_postgres
Re-exports from tokio_postgres needed for connection configuration and TLS.

Structs§

Notification
Notification will be received when a NOTIFY command was sent on a channel that the client listens to. If there was no payload, the corresponding member will be set to the empty string (and not None for example).
PgPubSub
Client for PostgreSQL LISTEN/NOTIFY pub/sub.
PgPubSubOptions
PgPubSubOptionsBuilder
Subscription
A subscription to a PostgreSQL notification channel.

Enums§

PubSubError
Errors returned by PgPubSub operations.
RecvError
Error returned by Subscription::recv.