[zeromq-dev] Fwd: Inverted PUB-SUB doesn't work

Rajalakshmi Iyer raj at blismedia.com
Thu Feb 5 19:27:15 CET 2015


Hello,

I have a use case where multiple publishers need to send messages to a
single subscriber. I have tried implementing the inverted PUB-SUB pattern
as follows -

#include <zmq.hpp>
#include <iostream>

int main()
{
   try
   {
      int major, minor, patch;
      zmq_version(&major, &minor, &patch);
      std::cout << "ZeroMQ version " << major << "." << minor << "." <<
patch << " " << std::endl;

      zmq::context_t ctx(1);
      zmq::socket_t sender(ctx, ZMQ_PUB);
      zmq::socket_t receiver(ctx, ZMQ_SUB);


      receiver.bind("tcp://127.0.0.1:5557");
      std::cout << "Subscriber bind" << std::endl;

      sender.connect("tcp://127.0.0.1:5557");
      std::cout << "Publisher connect" << std::endl;

      zmq::message_t sendMsg;
      memcpy(sendMsg.data(), (char*)("Hello!"), 6);
      sender.send(sendMsg);
      std::cout << "Publisher send" << std::endl;

      zmq::message_t recvMsg;
      receiver.recv(&recvMsg);
      std::string message = std::string(static_cast<char*>(recvMsg.data()),
recvMsg.size());
      std::cout << "Subscriber recv: " << message << std::endl;

   } catch (zmq::error_t err)
   {
      std::cout << "Error : " << err.what() << std::endl;
   }
}

The output of this is -
ZeroMQ version 4.0.4
Subscriber bind
Publisher connect
Publisher send

As can be seen, the subscriber is unable to receive the message sent by the
publisher and the recv() call blocks.

Any ideas on what might be going wrong would be useful.

Thanks!

-- 

@BlisMedia <http://twitter.com/BlisMedia>

www.blismedia.com <http://blismedia.com>

This email and any attachments to it may be confidential and are intended solely 
for the use of the individual to whom it is addressed. Any views or opinions 
expressed are solely those of the author and do not necessarily represent 
those of BlisMedia Ltd, a company registered in England and Wales with 
registered number 06455773. Its registered office is 3rd Floor, 101 New 
Cavendish St, London, W1W 6XH, United Kingdom.

If you are not the intended recipient of this email, you must neither take 
any action based upon its contents, nor copy or show it to anyone. Please 
contact the sender if you believe you have received this email in error. 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20150205/474c65cf/attachment.htm>


More information about the zeromq-dev mailing list