[zeromq-dev] Lazy subscriber, only receive last message
Mathieu Westphal
mathieu.westphal at gmail.com
Wed Oct 15 12:20:14 CEST 2014
Hello
I am tryinh to implement a pub/sub server/client communication in wich the client is way slower than the server.
server will send 10 message when the client as the time only to receive and use one.
The thing is, the client doesn't care at all about losing message, but it cares about getting the last sent message.
I've tried to use high water mark for that, but it does not seem to work at all.
I've also seen this post from 2011, http://lists.zeromq.org/pipermail/zeromq-dev/2011-May/011081.html,
wich propose the use of dedicated reader thread on the client, but the downside of this solution is the message are indeed transferred on the network while they could have been dropped !
My solution is to connect/deconnect the client each time i want to receive a message, but the connect method take some milliseconds, so it is not good also.
Here is my code with conect/deconnect:
client.cxx
#include <zmq.hpp>
#include <iostream>
#include <sstream>
#include <unistd.h>
int main (int argc, char *argv[])
{
zmq::context_t context (1);
zmq::socket_t subscriber (context, ZMQ_SUB);
while(1){
zmq::message_t update;
int counter;
subscriber.connect("tcp://localhost:5556");
subscriber.setsockopt(ZMQ_SUBSCRIBE, "", 0);
subscriber.recv(&update);
subscriber.disconnect("tcp://localhost:5556");
std::istringstream iss(static_cast<char*>(update.data()));
iss >> counter;
std::cout << counter << std::endl;
usleep(1000000);
}
return 0;
}
server.cxx
#include <zmq.hpp>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <time.h>
int main () {
// Prepare our context and publisher
zmq::context_t context (1);
zmq::socket_t publisher (context, ZMQ_PUB);
publisher.bind("tcp://*:5556");
int counter = 0;
while (1) {
counter++;
// Send message to all subscribers
zmq::message_t message(20);
snprintf ((char *) message.data(), 20 ,
"%d", counter);
publisher.send(message);
std::cout << counter << std::endl;
usleep(100000);
}
return 0;
}
Any helps very much apreciated !
Mathieu
More information about the zeromq-dev
mailing list