AsyncMessageHandler

Trait AsyncMessageHandler 

Source
pub trait AsyncMessageHandler: Send {
    // Required method
    fn handle(&mut self, msg: &AsyncMessage);
}
Expand description

Handler for asynchronous messages from the server.

These messages can arrive at any time during query execution:

  • Notification - from LISTEN/NOTIFY
  • Notice - warnings and informational messages
  • ParameterChanged - server parameter updates

§Example

use zero_postgres::{sync::Conn, AsyncMessage};

let mut conn = Conn::new(opts)?;

conn.set_async_message_handler(|msg: &AsyncMessage| {
    match msg {
        AsyncMessage::Notification { channel, payload, .. } => {
            println!("Notification on {}: {}", channel, payload);
        }
        AsyncMessage::Notice(err) => {
            println!("Notice: {:?}", err);
        }
        AsyncMessage::ParameterChanged { name, value } => {
            println!("Parameter {} changed to {}", name, value);
        }
    }
});

// Subscribe to a channel
conn.query_drop("LISTEN my_channel")?;

// Notifications will be delivered to the handler during any query
conn.query_drop("")?; // empty query to poll for notifications

Required Methods§

Source

fn handle(&mut self, msg: &AsyncMessage)

Handle an asynchronous message from the server.

Implementors§