ÃMQâs Built-in Proxy Function
It turns out that the core loop in the previous sectionâs
rrbroker is very useful, and reusable. It lets us
build pub-sub forwarders and shared queues and other little
intermediaries with very little effort. ÃMQ wraps this up in a single
method, zmq_proxy()
:
zmq_proxy
(
frontend
,
backend
,
capture
);
The two sockets (or three if we want to capture data) must be
properly connected, bound, and configured. When we call the zmq_proxy()
method, itâs exactly like starting
the main loop of rrbroker. Letâs rewrite the
request-reply broker to call zmq_proxy()
, and re-badge this as an
expensive-sounding âmessage queueâ (people have charged houses for code
that did less). Example 2-6 shows the result.
Example 2-6. Message queue broker (msgqueue.c)
//
// Simple message queuing broker
// Same as request-reply broker but using QUEUE device
//
#include "zhelpers.h"
int
main
(
void
)
{
void
*
context
=
zmq_ctx_new
();
// Socket facing clients
void
*
frontend
=
zmq_socket
(
context
,
ZMQ_ROUTER
);
int
rc
=
zmq_bind
(
frontend
,
"tcp://*:5559"
);
assert
(
rc
==
0
);
// Socket facing services
void
*
backend
=
zmq_socket
(
context
,
ZMQ_DEALER
);
rc
=
zmq_bind
(
backend
,
"tcp://*:5560"
);
assert
(
rc
==
0
);
// Start the proxy
zmq_proxy
(
frontend
,
backend
,
NULL
);
// We never get here...
zmq_close
(
frontend
);
zmq_close
(
backend
);
zmq_ctx_destroy
(
context
);
return
0
;
}
If youâre like most ÃMQ users, at this stage youâre starting to think, âWhat kind of evil stuff can I do if ...
Get ZeroMQ now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.