pub struct PubSocket<T: Transport<A>, A: Address> { /* private fields */ }Expand description
A publisher socket. This is thread-safe and can be cloned.
Publisher sockets are used to publish messages under certain topics to multiple subscribers.
§Session
Per subscriber, the socket maintains a session. The session manages the underlying connection and all of its state, such as the topic subscriptions. It also manages a queue of messages to be transmitted on the connection.
Implementations§
Source§impl<T> PubSocket<T, SocketAddr>where
T: Transport<SocketAddr>,
impl<T> PubSocket<T, SocketAddr>where
T: Transport<SocketAddr>,
Sourcepub async fn bind(&mut self, addr: impl ToSocketAddrs) -> Result<(), PubError>
pub async fn bind(&mut self, addr: impl ToSocketAddrs) -> Result<(), PubError>
Binds the socket to the given socket address.
This method is only available for transports that support SocketAddr as address type,
like Tcp and Quic.
Source§impl<T, A> PubSocket<T, A>
impl<T, A> PubSocket<T, A>
Sourcepub fn new(transport: T) -> Self
pub fn new(transport: T) -> Self
Creates a new reply socket with the default PubOptions.
Sourcepub fn with_options(transport: T, options: PubOptions) -> Self
pub fn with_options(transport: T, options: PubOptions) -> Self
Creates a new publisher socket with the given transport and options.
Sourcepub fn with_compressor<C: Compressor + 'static>(self, compressor: C) -> Self
pub fn with_compressor<C: Compressor + 'static>(self, compressor: C) -> Self
Sets the message compressor for this socket.
Sourcepub fn with_connection_hook<H>(self, hook: H) -> Selfwhere
H: ConnectionHook<T::Io>,
pub fn with_connection_hook<H>(self, hook: H) -> Selfwhere
H: ConnectionHook<T::Io>,
Sets the connection hook for this socket.
The connection hook is called when a new connection is accepted, before the connection is used for pub/sub communication.
§Panics
Panics if the socket has already been bound (driver started).
Sourcepub async fn try_bind(&mut self, addresses: Vec<A>) -> Result<(), PubError>
pub async fn try_bind(&mut self, addresses: Vec<A>) -> Result<(), PubError>
Binds the socket to the given addresses in order until one succeeds.
This also spawns the socket driver task.
Sourcepub async fn publish(
&self,
topic: impl Into<String>,
message: Bytes,
) -> Result<(), PubError>
pub async fn publish( &self, topic: impl Into<String>, message: Bytes, ) -> Result<(), PubError>
Publishes a message to the given topic. If the topic doesn’t exist, this is a no-op.
Sourcepub fn try_publish(&self, topic: String, message: Bytes) -> Result<(), PubError>
pub fn try_publish(&self, topic: String, message: Bytes) -> Result<(), PubError>
Publishes a message to the given topic, compressing the payload if a compressor is set. If the topic doesn’t exist, this is a no-op.
pub fn stats(&self) -> &PubStats
Sourcepub fn local_addr(&self) -> Option<&A>
pub fn local_addr(&self) -> Option<&A>
Returns the local address this socket is bound to. None if the socket is not bound.