title: What is a simple example of a working XSUB / XPUB proxy in zeromq
body: <p>I have a follow-up to <a href="https://stackoverflow.com/questions/18570810/how-to-implement-pub-sub-network-with-a-proxy-by-using-xpub-and-xsub-in-zeromqc">How to implement Pub-Sub Network with a Proxy by using XPUB and XSUB in ZeroMQ(C++)?</a></p>
  
  <p>That question requested a C++ proxy using XSUB and XPUB.  The answer given is essentially the proxy main() function quoted below.</p>
  
  <p>I extended this proxy to a full working example including a publisher and subscriber.  The catch is that my code only works with dealer / router options (as shown in comments below).  With the actual (uncommented) XPUB / XSUB options below, subscribers don't get messages. What's going wrong?  Is there a tweak to get messages to arrive?</p>
  
  <p><strong>Proxy</strong> not working with XPUB/XSUB (working dealer / router in comments)</p>
  
  <pre><code>#include &lt;zmq.hpp&gt;
  
  int main(int argc, char* argv[]) {
      zmq::context_t ctx(1);
      zmq::socket_t frontend(ctx, /*ZMQ_ROUTER*/ ZMQ_XSUB);
      zmq::socket_t backend(ctx, /*ZMQ_DEALER*/ ZMQ_XPUB);
      frontend.bind("tcp://*:5570");
      backend.bind("tcp://*:5571");
      zmq::proxy(frontend, backend, nullptr);
      return 0;
  }
  </code></pre>
  
  <p><strong>Subscriber</strong> not working with ZMQ_SUB (working dealer / router option in comments)</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;zmq.hpp&gt;
  
  std::string GetStringFromMessage(const zmq::message_t&amp; msg) {
      char* tmp = new char[msg.size()+1];
      memcpy(tmp,msg.data(),msg.size());
      tmp[msg.size()] = '\0';
      std::string rval(tmp);
      delete[] tmp;
      return rval;
  }
  
  int main(int argc, char* argv[]) {
      zmq::context_t ctx(1);
      zmq::socket_t socket(ctx, /*ZMQ_DEALER*/ ZMQ_SUB);
      socket.connect("tcp://localhost:5571");
      while (true) {
          zmq::message_t identity;
          zmq::message_t message;
          socket.recv(&amp;identity);
          socket.recv(&amp;message);
          std::string identityStr(GetStringFromMessage(identity));
          std::string messageStr(GetStringFromMessage(message));
          std::cout &lt;&lt; "Identity: " &lt;&lt; identityStr &lt;&lt; std::endl;
          std::cout &lt;&lt; "Message: "  &lt;&lt; messageStr  &lt;&lt; std::endl;
      }
  }
  </code></pre>
  
  <p><strong>Publisher</strong> not working with ZMQ_PUB (working dealer / router option in comments)</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;sstream&gt;
  #include &lt;zmq.hpp&gt;
  
  int main (int argc, char* argv[])
  {
      // Context
      zmq::context_t ctx(1);
  
      // Create a socket and set its identity attribute
      zmq::socket_t socket(ctx, /*ZMQ_DEALER*/ ZMQ_PUB);
      char identity[10] = {};
      sprintf(identity, "%d", getpid());
      socket.setsockopt(ZMQ_IDENTITY, identity, strlen(identity));
      socket.connect("tcp://localhost:5570");
  
      // Send some messages
      unsigned int counter = 0;
      while (true) {
          std::ostringstream ss;
          ss &lt;&lt; "Message #" &lt;&lt; counter &lt;&lt; " from PID " &lt;&lt; getpid();
          socket.send(ss.str().c_str(),ss.str().length());
          counter++;
          sleep(1);
      }
      return 0;
  }
  </code></pre>