The previous example is half-complete. It can manage a set of workers with dummy requests and replies, but it has no way to talk to clients.
If we add a second frontend ROUTER socket that accepts client requests, and turn our example into a proxy that can switch messages from frontend to backend, we get a useful and reusable tiny load-balancing message broker (Figure 3-7).
Figure 3-7. Load-balancing broker
This broker does the following:
Accepts connections from a set of clients
Accepts connections from a set of workers
Accepts requests from clients and holds these in a single queue
Sends these requests to workers using the load-balancing pattern
Receives replies back from workers
Sends these replies back to the original requesting client
The broker code (listed in Example 3-5) is fairly long, but worth understanding.
Example 3-5. Load-balancing broker (lbbroker.c)
// Load-balancing broker
// Clients and workers are shown here in-process
#define NBR_CLIENTS 10
#define NBR_WORKERS 3
// Dequeue operation for queue implemented as array of anything
#define DEQUEUE(q) memmove (&(q), &(q), sizeof (q) - sizeof (q ))
// Basic request-reply client using REQ socket.
// Since s_send and s_recv can't handle 0MQ binary identities we
// set a printable text identity to allow routing.