I'm trying to implement something like the model described in Figure 14 - Extended Pub-Sub .
I've looked at this snippet, which works fine. But when trying to implement the first part (i.e.: {pub, ...} -> {xsub
I don't receive any data on the xsub
socket.
The following code blocks forever, instead of receiving the message:
use zmq::{Context, SocketType::XSUB, PUB};
const ADDRESS_XSUB: &'static str = "tcp://127.0.0.1:9123";
fn main() {
let context = Context::new();
let xsubscriber = {
let socket = context.socket(XSUB).unwrap();
socket.bind(ADDRESS_XSUB).unwrap();
socket
};
let publisher = {
let context = Context::new();
let socket = context.socket(PUB).unwrap();
socket.connect(ADDRESS_XSUB).unwrap();
socket
};
publisher.send("", zmq::SNDMORE).unwrap();
publisher.send("HI", 0x00).unwrap();
assert_eq!(b"HI".as_ref(), &xsubscriber.recv_bytes(0).unwrap());
}
XSUB is usually paired with XPUB to create a separate proxy, this allows subscription messages as well as data to be routed between many publisher and subscribers.
If you want to use XSUB you will need to send a subscribe message (first byte 0x1 followed by filter) to the XSUB socket.
Just to make sure the code is working I would suggest switching XSUB to SUB and call the subscribe API with an empty "" filter.
Also make sure the subscriber is connected to the publisher before you send the message. Publishers only queue messages once the connection is made.