The REQ to ROUTER Combination

In the same way as we can replace REQ with DEALER, we can replace REP with ROUTER. This gives us an asynchronous server that can talk to multiple REQ clients at the same time. If we rewrote our “Hello World” server using ROUTER, we’d be able to process any number of “Hello” requests in parallel. We saw this in the mtserver example in Chapter 2.

We can use ROUTER in two distinct ways:

  • As a proxy that switches messages between frontend and backend sockets

  • As an application that reads the message and acts on it

In the first case, the ROUTER simply reads all frames, including the artificial identity frame, and passes them on blindly. In the second case, the ROUTER must know the format of the reply envelope it’s being sent. As the other peer is a REQ socket, the ROUTER gets the identity frame, an empty frame, and then the data frame.

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.