pub struct PgListener { /* private fields */ }Expand description
PostgreSQL LISTEN handler for receiving NOTIFY events
Creates a long-lived connection that subscribes to one or more channels and streams incoming notifications.
Implementations§
Source§impl PgListener
impl PgListener
Sourcepub async fn from_url(url: &str, channels: Vec<String>) -> Result<Self, PgError>
pub async fn from_url(url: &str, channels: Vec<String>) -> Result<Self, PgError>
Create a new listener from a connection URL
§Arguments
url- PostgreSQL connection stringchannels- List of channel names to subscribe to
§Errors
Returns error if:
- Cannot connect to database
- Cannot subscribe to channels
§Example
let channels = vec!["warpdrive:cache:invalidate".to_string()];
let listener = PgListener::from_url(
"postgresql://localhost/mydb",
channels
).await?;Sourcepub async fn new(
_pool: &PgPool,
_channels: Vec<String>,
) -> Result<Self, PgError>
pub async fn new( _pool: &PgPool, _channels: Vec<String>, ) -> Result<Self, PgError>
Create a new listener for the specified channels using a pool
Note: This creates a dedicated connection separate from the pool. The pool is used only to verify connectivity and extract the connection URL.
§Arguments
pool- Connection pool (used to get database URL)channels- List of channel names to subscribe to
§Errors
Returns error if:
- Cannot obtain connection from pool
- Cannot subscribe to channels
§Example
let channels = vec![
"warpdrive:cache:invalidate".to_string(),
"warpdrive:circuit:state".to_string(),
];
// Use from_url instead, as pool doesn't expose the connection string
let listener = PgListener::from_url(url, channels).await?;Sourcepub fn stream(
&mut self,
) -> Pin<Box<dyn Stream<Item = PgNotification> + Send + '_>>
pub fn stream( &mut self, ) -> Pin<Box<dyn Stream<Item = PgNotification> + Send + '_>>
Get a stream of notifications
Returns a stream that yields PgNotification items as they arrive.
§Example
let mut stream = listener.stream();
while let Some(notification) = stream.next().await {
println!("Channel: {}, Payload: {}", notification.channel, notification.payload);
}Auto Trait Implementations§
impl Freeze for PgListener
impl !RefUnwindSafe for PgListener
impl Send for PgListener
impl Sync for PgListener
impl Unpin for PgListener
impl !UnwindSafe for PgListener
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more