Skip to main content

SubSocket

Struct SubSocket 

Source
pub struct SubSocket<S = TcpStream>
where S: AsyncRead + AsyncWrite + Unpin,
{ /* 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

Source

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 messages
Source

pub fn is_connected(&self) -> bool

Check if the socket is currently connected.

Source

pub async fn try_reconnect(&mut self) -> Result<()>

Try to reconnect to the stored endpoint, re-sending all active subscriptions.

Source

pub async fn recv_with_reconnect(&mut self) -> Result<Option<Vec<Bytes>>>

Receive with automatic reconnection on EOF or network error.

Source

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"");
Source

pub async fn from_tcp(stream: TcpStream) -> Result<Self>

Create a SUB socket from a TCP stream with TCP_NODELAY enabled.

Source

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?;
Source

pub async fn with_options<Stream>( stream: Stream, options: SocketOptions, ) -> Result<SubSocket<Stream>>
where Stream: AsyncRead + AsyncWrite + Unpin,

Create a SUB socket from any stream with custom options.

Source§

impl<S> SubSocket<S>
where S: AsyncRead + AsyncWrite + Unpin,

Source

pub fn monitor(&mut self) -> SocketMonitor

Enable monitoring for this socket.

Returns a receiver for socket lifecycle events.

Source

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.

Source

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.

Source

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.

Source

pub const fn socket_type() -> SocketType

Get the socket type.

§ZeroMQ Compatibility

Corresponds to ZMQ_TYPE (16) option.

Source

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.

Source

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.

Source

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 receive
  • 2 (POLLOUT) - Socket is ready to send
  • 3 (POLLIN | POLLOUT) - Socket is ready for both
§ZeroMQ Compatibility

Corresponds to ZMQ_EVENTS (15) option.

Source

pub fn options_mut(&mut self) -> &mut SocketOptions

Get a mutable reference to this socket’s options.

Source§

impl SubSocket<UnixStream>

Source

pub async fn from_unix_stream(stream: UnixStream) -> Result<Self>

Create a SUB socket from an existing Unix domain socket stream (IPC).

Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more