pub struct SubSocket<S = TcpStream>{ /* private fields */ }Expand description
A SUB socket for receiving filtered messages.
SUB sockets connect to PUB peers and filter messages by topic prefix. They’re used for:
- Event subscriptions
- Topic-based message filtering
- Many-to-one aggregation
§ZeroMQ Compatibility
Compatible with zmq::SUB and zmq::PUB sockets from libzmq.
§Example
use monocoque::zmq::SubSocket;
let mut socket = SubSocket::connect("127.0.0.1:5555").await?;
// Subscribe to topic
socket.subscribe(b"topic");
// Receive filtered messages
loop {
match socket.recv().await? {
Some(msg) => println!("Received: {:?}", msg),
None => break, // Connection closed
}
}Implementations§
Source§impl SubSocket
impl SubSocket
Sourcepub async fn connect(endpoint: &str) -> Result<Self>
pub async fn connect(endpoint: &str) -> Result<Self>
Connect to a PUB peer and create a SUB socket.
Accepts TCP endpoints or raw socket addresses:
"tcp://127.0.0.1:5555""127.0.0.1:5555"
For IPC (Unix domain sockets), use SubSocket::connect_ipc().
§Example
use monocoque::zmq::SubSocket;
let mut socket = SubSocket::connect("127.0.0.1:5555").await?;
socket.subscribe(b""); // Subscribe to all messagesSourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Check if the socket is currently connected.
Sourcepub async fn try_reconnect(&mut self) -> Result<()>
pub async fn try_reconnect(&mut self) -> Result<()>
Try to reconnect to the stored endpoint, re-sending all active subscriptions.
Sourcepub async fn recv_with_reconnect(&mut self) -> Result<Option<Vec<Bytes>>>
pub async fn recv_with_reconnect(&mut self) -> Result<Option<Vec<Bytes>>>
Receive with automatic reconnection on EOF or network error.
Sourcepub async fn connect_ipc(path: &str) -> Result<SubSocket<UnixStream>>
pub async fn connect_ipc(path: &str) -> Result<SubSocket<UnixStream>>
Connect to a PUB peer via IPC (Unix domain sockets).
Unix-only. Accepts IPC paths with or without ipc:// prefix:
"ipc:///tmp/socket.sock""/tmp/socket.sock"
§Example
use monocoque::zmq::SubSocket;
let mut socket = SubSocket::connect_ipc("/tmp/pubsub.sock").await?;
socket.subscribe(b"");Sourcepub async fn from_tcp(stream: TcpStream) -> Result<Self>
pub async fn from_tcp(stream: TcpStream) -> Result<Self>
Create a SUB socket from a TCP stream with TCP_NODELAY enabled.
Sourcepub async fn from_tcp_with_options(
stream: TcpStream,
options: SocketOptions,
) -> Result<Self>
pub async fn from_tcp_with_options( stream: TcpStream, options: SocketOptions, ) -> Result<Self>
Create a SUB socket from a TCP stream with custom options.
§Example
use monocoque::zmq::{SubSocket, SocketOptions};
use monocoque_core::rt::TcpStream;
let stream = TcpStream::connect("127.0.0.1:5555").await?;
let socket = SubSocket::from_tcp_with_options(
stream,
SocketOptions::default()
.with_recv_hwm(500)
.with_buffer_sizes(4096, 4096)
).await?;Sourcepub async fn with_options<Stream>(
stream: Stream,
options: SocketOptions,
) -> Result<SubSocket<Stream>>
pub async fn with_options<Stream>( stream: Stream, options: SocketOptions, ) -> Result<SubSocket<Stream>>
Create a SUB socket from any stream with custom options.
Source§impl<S> SubSocket<S>
impl<S> SubSocket<S>
Sourcepub fn monitor(&mut self) -> SocketMonitor
pub fn monitor(&mut self) -> SocketMonitor
Enable monitoring for this socket.
Returns a receiver for socket lifecycle events.
Sourcepub async fn subscribe(&mut self, topic: &[u8]) -> Result<()>
pub async fn subscribe(&mut self, topic: &[u8]) -> Result<()>
Subscribe to messages matching the given topic prefix.
Empty topic subscribes to all messages.
This sends a subscription message to the PUB socket.
Sourcepub async fn unsubscribe(&mut self, topic: &[u8]) -> Result<()>
pub async fn unsubscribe(&mut self, topic: &[u8]) -> Result<()>
Unsubscribe from messages matching the given topic prefix.
This sends an unsubscription message to the PUB socket.
Sourcepub async fn recv(&mut self) -> Result<Option<Vec<Bytes>>>
pub async fn recv(&mut self) -> Result<Option<Vec<Bytes>>>
Receive a multipart message.
Only messages matching subscribed topics will be received.
Returns None if the connection is closed.
Sourcepub const fn socket_type() -> SocketType
pub const fn socket_type() -> SocketType
Sourcepub fn last_endpoint(&self) -> Option<&Endpoint>
pub fn last_endpoint(&self) -> Option<&Endpoint>
Get the endpoint this socket is connected/bound to, if available.
Returns None if the socket was created from a raw stream.
§ZeroMQ Compatibility
Corresponds to ZMQ_LAST_ENDPOINT (32) option.
Sourcepub fn has_more(&self) -> bool
pub fn has_more(&self) -> bool
Check if the last received message has more frames coming.
Returns true if there are more frames in the current multipart message.
§ZeroMQ Compatibility
Corresponds to ZMQ_RCVMORE (13) option.
Sourcepub fn events(&self) -> u32
pub fn events(&self) -> u32
Get the event state of the socket.
Returns a bitmask indicating ready-to-receive and ready-to-send states.
§Returns
1(POLLIN) - Socket is ready to receive2(POLLOUT) - Socket is ready to send3(POLLIN | POLLOUT) - Socket is ready for both
§ZeroMQ Compatibility
Corresponds to ZMQ_EVENTS (15) option.
Sourcepub fn options_mut(&mut self) -> &mut SocketOptions
pub fn options_mut(&mut self) -> &mut SocketOptions
Get a mutable reference to this socket’s options.
Source§impl SubSocket<UnixStream>
impl SubSocket<UnixStream>
Sourcepub async fn from_unix_stream(stream: UnixStream) -> Result<Self>
pub async fn from_unix_stream(stream: UnixStream) -> Result<Self>
Create a SUB socket from an existing Unix domain socket stream (IPC).
Sourcepub async fn from_unix_stream_with_options(
stream: UnixStream,
options: SocketOptions,
) -> Result<Self>
pub async fn from_unix_stream_with_options( stream: UnixStream, options: SocketOptions, ) -> Result<Self>
Create a SUB socket from an existing Unix stream with custom options.
This method provides full control over socket behavior through SocketOptions.
Auto Trait Implementations§
impl<S = TcpStream> !Freeze for SubSocket<S>
impl<S> RefUnwindSafe for SubSocket<S>where
S: RefUnwindSafe,
impl<S> Send for SubSocket<S>where
S: Send,
impl<S> Sync for SubSocket<S>where
S: Sync,
impl<S> Unpin for SubSocket<S>
impl<S> UnsafeUnpin for SubSocket<S>where
S: UnsafeUnpin,
impl<S> UnwindSafe for SubSocket<S>where
S: UnwindSafe,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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