pub struct PgListener { /* private fields */ }Expand description
A stream of asynchronous notifications from Postgres.
This listener will auto-reconnect. If the active connection being used ever dies, this listener will detect that event, create a new connection, will re-subscribe to all of the originally specified channels, and will resume operations as normal.
Implementations§
Source§impl PgListener
impl PgListener
pub fn connect(uri: &str) -> Result<Self, Error>
pub fn connect_with(pool: &Pool<Postgres>) -> Result<Self, Error>
Sourcepub fn listen(&mut self, channel: &str) -> Result<(), Error>
pub fn listen(&mut self, channel: &str) -> Result<(), Error>
Starts listening for notifications on a channel. The channel name is quoted here to ensure case sensitivity.
Sourcepub fn listen_all<'a>(
&mut self,
channels: impl IntoIterator<Item = &'a str>,
) -> Result<(), Error>
pub fn listen_all<'a>( &mut self, channels: impl IntoIterator<Item = &'a str>, ) -> Result<(), Error>
Starts listening for notifications on all channels.
Sourcepub fn unlisten(&mut self, channel: &str) -> Result<(), Error>
pub fn unlisten(&mut self, channel: &str) -> Result<(), Error>
Stops listening for notifications on a channel. The channel name is quoted here to ensure case sensitivity.
Sourcepub fn unlisten_all(&mut self) -> Result<(), Error>
pub fn unlisten_all(&mut self) -> Result<(), Error>
Stops listening for notifications on all channels.
Sourcepub fn recv(&mut self) -> Result<PgNotification, Error>
pub fn recv(&mut self) -> Result<PgNotification, Error>
Receives the next notification available from any of the subscribed channels.
If the connection to PostgreSQL is lost, it is automatically reconnected on the next
call to recv(), and should be entirely transparent (as long as it was just an
intermittent network failure or long-lived connection reaper).
As notifications are transient, any received while the connection was lost, will not
be returned. If you’d prefer the reconnection to be explicit and have a chance to
do something before, please see try_recv.
§Example
loop {
// ask for next notification, re-connecting (transparently) if needed
let notification = listener.recv()?;
// handle notification, do something interesting
}Sourcepub fn try_recv(&mut self) -> Result<Option<PgNotification>, Error>
pub fn try_recv(&mut self) -> Result<Option<PgNotification>, Error>
Receives the next notification available from any of the subscribed channels.
If the connection to PostgreSQL is lost, None is returned, and the connection is
reconnected on the next call to try_recv().
§Example
loop {
// start handling notifications, connecting if needed
while let Some(notification) = listener.try_recv()? {
// handle notification
}
// connection lost, do something interesting
}Sourcepub fn into_stream(self) -> ChanStream<PgNotification>
pub fn into_stream(self) -> ChanStream<PgNotification>
Consume this listener, returning a Stream of notifications.
The backing connection will be automatically reconnected should it be lost.
This has the same potential drawbacks as recv.
Trait Implementations§
Source§impl Debug for PgListener
impl Debug for PgListener
Source§impl Drop for PgListener
impl Drop for PgListener
Source§impl<'c> Executor for &'c mut PgListener
impl<'c> Executor for &'c mut PgListener
type Database = Postgres
Source§fn fetch_many<'q, E>(
&mut self,
query: E,
) -> ChanStream<Either<PgQueryResult, PgRow>>
fn fetch_many<'q, E>( &mut self, query: E, ) -> ChanStream<Either<PgQueryResult, PgRow>>
Source§fn fetch_optional<'q, E>(&mut self, query: E) -> Result<Option<PgRow>, Error>
fn fetch_optional<'q, E>(&mut self, query: E) -> Result<Option<PgRow>, Error>
Source§fn prepare_with<'q>(
&mut self,
query: &'q str,
parameters: &'q [PgTypeInfo],
) -> Result<PgStatement, Error>
fn prepare_with<'q>( &mut self, query: &'q str, parameters: &'q [PgTypeInfo], ) -> Result<PgStatement, Error>
Source§fn execute<'q, E>(
&mut self,
query: E,
) -> Result<<Self::Database as Database>::QueryResult, Error>
fn execute<'q, E>( &mut self, query: E, ) -> Result<<Self::Database as Database>::QueryResult, Error>
Source§fn execute_many<'q, E>(
&mut self,
query: E,
) -> ChanStream<<Self::Database as Database>::QueryResult>
fn execute_many<'q, E>( &mut self, query: E, ) -> ChanStream<<Self::Database as Database>::QueryResult>
Source§fn fetch<'q, E>(
&mut self,
query: E,
) -> ChanStream<<Self::Database as Database>::Row>
fn fetch<'q, E>( &mut self, query: E, ) -> ChanStream<<Self::Database as Database>::Row>
Source§fn fetch_all<'q, E>(
&mut self,
query: E,
) -> Result<Vec<<Self::Database as Database>::Row>, Error>
fn fetch_all<'q, E>( &mut self, query: E, ) -> Result<Vec<<Self::Database as Database>::Row>, Error>
Vec.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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more