Skip to main content

XPubSocket

Struct XPubSocket 

Source
pub struct XPubSocket { /* private fields */ }
Expand description

XPUB (Extended Publisher) socket.

Receives subscription events and broadcasts messages to matching subscribers.

§Features

  • Subscription tracking: Know what topics subscribers want
  • Verbose mode: Report all subscriptions (including duplicates)
  • Manual mode: Explicit subscription control
  • Welcome messages: Send initial message to new subscribers

§Examples

use monocoque_zmtp::xpub::XPubSocket;
use bytes::Bytes;

let mut xpub = XPubSocket::bind("127.0.0.1:5555").await?;
     
    loop {
        // Receive subscription events from subscribers
        if let Some(event) = xpub.recv_subscription().await? {
            println!("Subscription event: {:?}", event);
        }
         
        // Broadcast messages to matching subscribers
        xpub.send(vec![Bytes::from("topic"), Bytes::from("data")]).await?;
    }

Implementations§

Source§

impl XPubSocket

Source

pub async fn bind(addr: &str) -> Result<XPubSocket, Error>

Bind to an address and start listening for subscribers.

§Examples
let xpub = XPubSocket::bind("127.0.0.1:5555").await?;
Source

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

Bind with custom socket options.

Honors options.reuse_port: when set, the listener is bound with SO_REUSEPORT so several XPUB acceptors can share one port.

Source

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

Accept new subscriber connections (non-blocking).

Call this periodically to accept new subscribers.

Source

pub async fn recv_subscription( &mut self, ) -> Result<Option<SubscriptionEvent>, Error>

Receive a subscription event from subscribers (non-blocking).

Returns None if no events are available.

§Examples
if let Some(event) = xpub.recv_subscription().await? {
    match event {
        monocoque_core::subscription::SubscriptionEvent::Subscribe(topic) => {
            println!("New subscription: {:?}", topic);
        }
        monocoque_core::subscription::SubscriptionEvent::Unsubscribe(topic) => {
            println!("Unsubscription: {:?}", topic);
        }
    }
}
Source

pub async fn send(&mut self, msg: Vec<Bytes>) -> Result<(), Error>

Broadcast a message to all matching subscribers.

Only subscribers whose subscriptions match the message’s first frame will receive it.

§Examples
xpub.send(vec![
    Bytes::from("topic.temperature"),
    Bytes::from("23.5"),
]).await?;
Source

pub fn subscriber_count(&self) -> usize

Get the number of active subscribers.

Source

pub fn local_addr(&self) -> Result<SocketAddr, Error>

Get the local address.

Source

pub const fn socket_type(&self) -> SocketType

Get the socket type.

Source

pub fn has_more(&self) -> bool

Check if the last received message has more frames coming.

For XPUB, subscription events are always single-frame.

§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 (has pending subscription events)
  • 2 (POLLOUT) - Socket is ready to send (has active subscribers)
  • 3 (POLLIN | POLLOUT) - Socket is ready for both
§ZeroMQ Compatibility

Corresponds to ZMQ_EVENTS (15) option.

Source

pub fn set_verbose(&mut self, verbose: bool)

Set verbose mode.

When enabled, all subscription messages are reported (including duplicates).

Source

pub fn set_manual(&mut self, manual: bool)

Set manual mode.

When enabled, subscriptions must be explicitly approved by calling send_subscription().

Source

pub async fn connect_upstream(&mut self, addr: &str) -> Result<(), Error>

Connect to an upstream publisher so that subscription events can be forwarded.

The upstream is typically a PUB or XSUB socket. After calling this method, send_subscription() (manual mode) writes subscription messages to the upstream connection, causing the upstream publisher to start or stop delivering matching messages.

§Examples
let mut xpub = XPubSocket::bind("127.0.0.1:5556").await?;
xpub.set_manual(true);
xpub.connect_upstream("127.0.0.1:5555").await?;

// Receive a subscription from a downstream client and forward it upstream.
if let Some(event) = xpub.recv_subscription().await? {
    xpub.send_subscription(event).await?;
}
Source

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

Manually send a subscription event to the upstream connection.

Requires both manual mode (set_manual(true)) and an upstream connection (connect_upstream()). Writes the subscription message directly to the upstream publisher so it starts (or stops) delivering matching messages.

Trait Implementations§

Source§

impl Debug for XPubSocket

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl ProxySocket for XPubSocket

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, XPubSocket: '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, XPubSocket: '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 Socket for XPubSocket

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, XPubSocket: '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, XPubSocket: '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§

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