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
impl XPubSocket
Sourcepub async fn bind(addr: &str) -> Result<XPubSocket, Error>
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?;Sourcepub async fn bind_with_options(
addr: &str,
options: SocketOptions,
) -> Result<XPubSocket, Error>
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.
Sourcepub async fn accept(&mut self) -> Result<(), Error>
pub async fn accept(&mut self) -> Result<(), Error>
Accept new subscriber connections (non-blocking).
Call this periodically to accept new subscribers.
Sourcepub async fn recv_subscription(
&mut self,
) -> Result<Option<SubscriptionEvent>, Error>
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);
}
}
}Sourcepub async fn send(&mut self, msg: Vec<Bytes>) -> Result<(), Error>
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?;Sourcepub fn subscriber_count(&self) -> usize
pub fn subscriber_count(&self) -> usize
Get the number of active subscribers.
Sourcepub fn local_addr(&self) -> Result<SocketAddr, Error>
pub fn local_addr(&self) -> Result<SocketAddr, Error>
Get the local address.
Sourcepub const fn socket_type(&self) -> SocketType
pub const fn socket_type(&self) -> SocketType
Get the socket type.
Sourcepub fn has_more(&self) -> bool
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.
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 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.
Sourcepub fn set_verbose(&mut self, verbose: bool)
pub fn set_verbose(&mut self, verbose: bool)
Set verbose mode.
When enabled, all subscription messages are reported (including duplicates).
Sourcepub fn set_manual(&mut self, manual: bool)
pub fn set_manual(&mut self, manual: bool)
Set manual mode.
When enabled, subscriptions must be explicitly approved by calling send_subscription().
Sourcepub async fn connect_upstream(&mut self, addr: &str) -> Result<(), Error>
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?;
}Sourcepub async fn send_subscription(
&mut self,
event: SubscriptionEvent,
) -> Result<(), Error>
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
impl Debug for XPubSocket
Source§impl ProxySocket for XPubSocket
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,
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,
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,
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,
Source§fn socket_desc(&self) -> &'static str
fn socket_desc(&self) -> &'static str
Source§impl Socket for XPubSocket
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,
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,
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,
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,
Source§fn socket_type(&self) -> SocketType
fn socket_type(&self) -> SocketType
Auto Trait Implementations§
impl !Freeze for XPubSocket
impl !RefUnwindSafe for XPubSocket
impl !Send for XPubSocket
impl !Sync for XPubSocket
impl Unpin for XPubSocket
impl UnsafeUnpin for XPubSocket
impl UnwindSafe for XPubSocket
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