pub struct XSubSocket<S = TcpStream>{ /* 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>
impl<S> XSubSocket<S>
Sourcepub async fn new(stream: S) -> Result<XSubSocket<S>, Error>
pub async fn new(stream: S) -> Result<XSubSocket<S>, Error>
Create a new XSUB socket from a stream.
Sourcepub async fn with_options(
stream: S,
options: SocketOptions,
) -> Result<XSubSocket<S>, Error>
pub async fn with_options( stream: S, options: SocketOptions, ) -> Result<XSubSocket<S>, Error>
Create a new XSUB socket with custom configuration and options.
Sourcepub async fn subscribe(&mut self, prefix: impl Into<Bytes>) -> Result<(), Error>
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?;Sourcepub async fn unsubscribe(
&mut self,
prefix: impl Into<Bytes>,
) -> Result<(), Error>
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?;Sourcepub async fn send_subscription_event(
&mut self,
event: SubscriptionEvent,
) -> Result<(), Error>
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?;Sourcepub async fn recv(&mut self) -> Result<Option<Vec<Bytes>>, Error>
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);
}
}Sourcepub fn subscription_count(&self) -> usize
pub fn subscription_count(&self) -> usize
Get the number of active subscriptions.
Sourcepub fn is_subscribed(&self, topic: &[u8]) -> bool
pub fn is_subscribed(&self, topic: &[u8]) -> bool
Check if subscribed to a specific topic.
Sourcepub fn subscriptions(&self) -> Vec<Subscription>
pub fn subscriptions(&self) -> Vec<Subscription>
Get all subscriptions.
Sourcepub const fn socket_type(&self) -> SocketType
pub const fn socket_type(&self) -> SocketType
Get the socket type.
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.
Source§impl XSubSocket
impl XSubSocket
Sourcepub async fn connect(addr: &str) -> Result<XSubSocket, Error>
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?;Sourcepub async fn connect_with_options(
addr: &str,
options: SocketOptions,
) -> Result<XSubSocket, Error>
pub async fn connect_with_options( addr: &str, options: SocketOptions, ) -> Result<XSubSocket, Error>
Connect with custom socket options, storing the endpoint for automatic reconnection.
Sourcepub 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<(), Error>
pub async fn try_reconnect(&mut self) -> Result<(), Error>
Try to reconnect to the stored endpoint, re-sending all active subscriptions.
Trait Implementations§
Source§impl ProxySocket for XSubSocket
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,
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,
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,
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,
Source§fn socket_desc(&self) -> &'static str
fn socket_desc(&self) -> &'static str
Source§impl<S> Socket for XSubSocket<S>
impl<S> Socket for XSubSocket<S>
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,
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,
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,
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,
Source§fn socket_type(&self) -> SocketType
fn socket_type(&self) -> SocketType
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> 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