title: ZeroMQ (cppzmq) subscriber skips first message
body: <p>I'm trying to use <a href="http://zeromq.org" rel="nofollow noreferrer">ZMQ</a> with the <a href="http://zeromq.org/topics:cppzmq" rel="nofollow noreferrer">CPPZMQ</a> C++ wrapper, as it seems it is the one suggested in <a href="http://zeromq.org/bindings:cpp" rel="nofollow noreferrer">C++ Bindings</a>.</p>
  
  <p>The client/server (REQ/REP) seems to work fine.
  When trying to implement a publish/subscribe pair of programs, it looks like the first message is lost in the subscriber. Why?</p>
  
  <p>publisher.cpp:</p>
  
  <pre><code>#include &lt;boost/date_time/posix_time/posix_time.hpp&gt;
  #include &lt;boost/thread/thread.hpp&gt;
  #include &lt;boost/format.hpp&gt;
  #include &lt;zmq.hpp&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  int main()
  {
      zmq::context_t context(1);
      zmq::socket_t publisher(context, ZMQ_PUB);
      publisher.bind("tcp://*:5555");
  
      for(int n = 0; n &lt; 3; n++) {
          zmq::message_t env1(1);
          memcpy(env1.data(), "A", 1);
          std::string msg1_str = (boost::format("Hello-%i") % (n + 1)).str();
          zmq::message_t msg1(msg1_str.size());
          memcpy(msg1.data(), msg1_str.c_str(), msg1_str.size());
          std::cout &lt;&lt; "Sending '" &lt;&lt; msg1_str &lt;&lt; "' on topic A" &lt;&lt; std::endl;
          publisher.send(env1, ZMQ_SNDMORE);
          publisher.send(msg1);
  
          zmq::message_t env2(1);
          memcpy(env2.data(), "B", 1);
          std::string msg2_str = (boost::format("World-%i") % (n + 1)).str();
          zmq::message_t msg2(msg2_str.size());
          memcpy(msg2.data(), msg2_str.c_str(), msg2_str.size());
          std::cout &lt;&lt; "Sending '" &lt;&lt; msg2_str &lt;&lt; "' on topic B" &lt;&lt; std::endl;
          publisher.send(env2, ZMQ_SNDMORE);
          publisher.send(msg2);
  
          boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
      }
      return 0;
  }
  </code></pre>
  
  <p>subscriber.cpp:</p>
  
  <pre><code>#include &lt;zmq.hpp&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  int main()
  {
      zmq::context_t context(1);
      zmq::socket_t subscriber(context, ZMQ_SUB);
      subscriber.connect("tcp://localhost:5555");
      subscriber.setsockopt(ZMQ_SUBSCRIBE, "B", 1);
  
      while(true)
      {
          zmq::message_t env;
          subscriber.recv(&amp;env);
          std::string env_str = std::string(static_cast&lt;char*&gt;(env.data()), env.size());
          std::cout &lt;&lt; "Received envelope '" &lt;&lt; env_str &lt;&lt; "'" &lt;&lt; std::endl;
  
          zmq::message_t msg;
          subscriber.recv(&amp;msg);
          std::string msg_str = std::string(static_cast&lt;char*&gt;(msg.data()), msg.size());
          std::cout &lt;&lt; "Received '" &lt;&lt; msg_str &lt;&lt; "'" &lt;&lt; std::endl;
      }
      return 0;
  }
  </code></pre>
  
  <p>Program output:</p>
  
  <pre><code>$ ./publisher
  Sending 'Hello-1' on topic A
  Sending 'World-1' on topic B
  Sending 'Hello-2' on topic A
  Sending 'World-2' on topic B
  Sending 'Hello-3' on topic A
  Sending 'World-3' on topic B
  
  $ ./subscriber
  Received envelope 'B'
  Received 'World-2'
  Received envelope 'B'
  Received 'World-3'
  </code></pre>
  
  <p>(note: subscriber is executed before executing publisher)</p>
  
  <p><strong>Bonus question:</strong> By the way, is it my impression or this C++ wrapper it is quite low level? I see no direct support for std::string and the code to transmit a simple string looks quite verbose.</p>