Skip to content

Instantly share code, notes, and snippets.

@purem
Created August 14, 2012 16:55
Show Gist options
  • Save purem/3350838 to your computer and use it in GitHub Desktop.
Save purem/3350838 to your computer and use it in GitHub Desktop.
zmq: Publish / Subscribe problem
//Publisher
int zipcode, temperature, relhumidity;
// Get values that will fool the boss
zipcode = 10001;
temperature = 1;
relhumidity = 2;
// Send message to all subscribers
zmq::message_t topic(6);
memcpy ((void *) topic.data (), "NASDAQ", 6);
publisher.send(topic, ZMQ_SNDMORE);
zmq::message_t message(20);
snprintf ((char *) message.data(), 20 ,
"%05d %d %d", zipcode, temperature, relhumidity);
publisher.send(message);
//Subscriber
const char *filter = "NASDAQ";¬
subscriber.setsockopt(ZMQ_SUBSCRIBE, filter, strlen (filter));
zmq::message_t topic_message;
subscriber.recv(&topic_message);
char* topic = static_cast<char*>(topic_message.data());
std::cout << topic << std::endl;
zmq::message_t body_message;
subscriber.recv(&body_message);
char* body = static_cast<char*>(body_message.data());
std::cout << body << std::endl;
//Prints
NASDAQ1 2
10001 1 2
Instead of
NASDAQ
10001 1 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment