If you recall from Chapter 2, Introduction to Sockets, we said that messages are prefix matched when using the publisher-subscriber pattern and we have showed a work-around to get what the subscriber really wants. This time, we visit the PUB-SUB sockets by enveloping messages with separate keys.
The following is the server code:
/* PUB – SUB wrap messages. server.c */ #include "czmq.h" int main (int argc, char const *argv[]) { zctx_t* context = zctx_new(); void* pub = zsocket_new(context, ZMQ_PUB); zsocket_bind(pub, "tcp://*:4040"); printf("starting server\n"); for(;;) { zstr_sendm(pub, "Company1"); zstr_send(pub, "Company Message to be ignored."); zstr_sendm(pub, "Company10"); zstr_send(pub, "Company message to receive."); zclock_sleep(10); } zsocket_destroy(context, pub); zctx_destroy(&context); return 0; }
And the following is the client code:
/* PUB – SUB envelop messages. client.c */ #include...