Skip to main content

XSubSocket

Struct XSubSocket 

Source
pub struct XSubSocket<S = TcpStream>
where S: AsyncRead + AsyncWrite + Unpin,
{ /* private fields */ }
Expand description

XSUB (Extended Subscriber) socket.

Receives data messages and can send subscription messages upstream.

§Features

  • Dynamic subscriptions: Subscribe/unsubscribe at runtime
  • Subscription forwarding: Forward subscriptions in proxies
  • Verbose unsubscribe: Optionally send explicit unsubscribe messages

§Examples

use monocoque_zmtp::xsub::XSubSocket;
use bytes::Bytes;

let mut xsub = XSubSocket::connect("127.0.0.1:5555").await?;
     
    // Subscribe to topics
    xsub.subscribe("topic.").await?;

    // Receive messages
    if let Some(msg) = xsub.recv().await? {
        println!("Received: {:?}", msg);
    }

    // Unsubscribe
xsub.unsubscribe("topic.").await?;

Implementations§

Source§

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

Source

pub async fn new(stream: S) -> Result<XSubSocket<S>, Error>

Create a new XSUB socket from a stream.

Source

pub async fn with_options( stream: S, options: SocketOptions, ) -> Result<XSubSocket<S>, Error>

Create a new XSUB socket with custom configuration and options.

Source

pub async fn subscribe(&mut self, prefix: impl Into<Bytes>) -> Result<(), Error>

Subscribe to messages with the given prefix.

Sends a subscription message upstream to the publisher.

§Examples
// Subscribe to all messages starting with "topic."
xsub.subscribe("topic.").await?;

// Subscribe to all messages (empty prefix)
xsub.subscribe("").await?;
Source

pub async fn unsubscribe( &mut self, prefix: impl Into<Bytes>, ) -> Result<(), Error>

Unsubscribe from messages with the given prefix.

Optionally sends an unsubscribe message upstream (if verbose mode enabled).

§Examples
let prefix = Bytes::from_static(b"topic.");
xsub.unsubscribe(prefix).await?;
Source

pub async fn send_subscription_event( &mut self, event: SubscriptionEvent, ) -> Result<(), Error>

Send a raw subscription event upstream (for proxies).

This allows forwarding subscription messages in broker patterns.

§Examples
let event = SubscriptionEvent::Subscribe(Bytes::from("topic"));
xsub.send_subscription_event(event).await?;
Source

pub async fn recv(&mut self) -> Result<Option<Vec<Bytes>>, Error>

Receive a data message (non-blocking).

Returns None if no message is available.

§Examples
if let Some(msg) = xsub.recv().await? {
    for frame in msg {
        println!("Frame: {:?}", frame);
    }
}
Source

pub fn subscription_count(&self) -> usize

Get the number of active subscriptions.

Source

pub fn is_subscribed(&self, topic: &[u8]) -> bool

Check if subscribed to a specific topic.

Source

pub fn subscriptions(&self) -> Vec<Subscription>

Get all subscriptions.

Source

pub const fn socket_type(&self) -> SocketType

Get the socket type.

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§

impl XSubSocket

Source

pub async fn connect(addr: &str) -> Result<XSubSocket, Error>

Connect to a publisher, storing the endpoint for automatic reconnection.

§Examples
let xsub = XSubSocket::connect("127.0.0.1:5555").await?;
Source

pub async fn connect_with_options( addr: &str, options: SocketOptions, ) -> Result<XSubSocket, Error>

Connect with custom socket options, storing the endpoint for automatic reconnection.

Source

pub fn is_connected(&self) -> bool

Check if the socket is currently connected.

Source

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

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>>, Error>

Receive a message with automatic reconnection on EOF or network error.

Respects max_reconnect_attempts - returns NotConnected when exhausted.

Trait Implementations§

Source§

impl ProxySocket for XSubSocket

Source§

fn recv_multipart<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = Result<Option<Vec<Bytes>>, Error>> + 'async_trait>>
where 'life0: 'async_trait, XSubSocket: 'async_trait,

Receive a multipart message from the socket. Read more
Source§

fn send_multipart<'life0, 'async_trait>( &'life0 mut self, msg: Vec<Bytes>, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + 'async_trait>>
where 'life0: 'async_trait, XSubSocket: 'async_trait,

Send a multipart message to the socket. Read more
Source§

fn socket_desc(&self) -> &'static str

Get a description of the socket for logging.
Source§

impl<S> Socket for XSubSocket<S>
where S: AsyncRead + AsyncWrite + Unpin + 'static,

Source§

fn send<'life0, 'async_trait>( &'life0 mut self, msg: Vec<Bytes>, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + 'async_trait>>
where 'life0: 'async_trait, XSubSocket<S>: 'async_trait,

Send a multipart message on the socket. Read more
Source§

fn recv<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = Result<Option<Vec<Bytes>>, Error>> + 'async_trait>>
where 'life0: 'async_trait, XSubSocket<S>: 'async_trait,

Receive a multipart message from the socket. Read more
Source§

fn socket_type(&self) -> SocketType

Get the socket type. Read more
Source§

fn has_more(&self) -> bool

Check if socket has more message frames pending. Read more

Auto Trait Implementations§

§

impl<S = TcpStream> !Freeze for XSubSocket<S>

§

impl<S> RefUnwindSafe for XSubSocket<S>
where S: RefUnwindSafe,

§

impl<S> Send for XSubSocket<S>
where S: Send,

§

impl<S> Sync for XSubSocket<S>
where S: Sync,

§

impl<S> Unpin for XSubSocket<S>

§

impl<S> UnsafeUnpin for XSubSocket<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for XSubSocket<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