Messaging Patterns LabPython · ZeroMQ

Chapter 17

Route an envelope

Address a reply to the right connection.

What you’ll learn

Use the output to distinguish a connection address from the data the receiver sees.

Prerequisite: What does silence tell you?. Course map →

The situation

A service has two clients. Returning the right value to the wrong connection is still a protocol error.

A DEALER socket can send several messages before receiving a reply. Its peer, ROUTER, adds an address frame when it receives a message. The application puts this address before the reply so ROUTER can send the rest of the frames to the right connection.

ROUTER separates the route from the body
One multipart message
  1. ROUTER addressing framealpha
  2. Application framejob
  3. Application framealpha

Conceptual diagram. ROUTER uses the first alpha to choose the connected peer. DEALER receives the application frames that follow. Authenticating a sender needs a separate policy.

Before you run

Does the receiver see its ROUTER address in the reply frames?

Run the experiment

Two DEALER sockets use the routes alpha and beta. Each sends job and its application name. ROUTER uses the first frame to address the reply.

For the original example, click Restore code if it is shown, then click Run. Compare your output with the worked result.

Python prepares in the background while you read. First use can download about 71 MiB. Your code runs only when you select Run; each run starts with clean experiment state.

Your edits are saved in this browser when storage is available.

Troubleshooting

If Python stops responding, restart the environment and try again. Your code and last completed result are kept.

Downloads

Worked result

alpha and beta are the routes in the output. Each route has three application frames, starting with ACK and job.

Full output from the original example
{
  "alpha": [
    "ACK",
    "job",
    "alpha"
  ],
  "beta": [
    "ACK",
    "job",
    "beta"
  ]
}

The build ran the original editor code to produce this output.

Try one change

Replace the last line in the editor with result = routing(first_route='desk-a'). Click Run again. Compare the output keys and the last reply frame for the renamed route. Which value is a transport address and which is application data?

Inspect the socket implementation

The editor calls this read-only helper. Read all socket helpers or read the separate-process code. The code download includes both files.

def routing(first_route='alpha'):
    if not first_route or first_route=='beta':raise ValueError('First route must be nonempty and distinct from beta')
    with lab() as socket:
        router=socket(zmq.ROUTER);router.bind('inproc://routes');clients={}
        for route,name in [(first_route,'alpha'),('beta','beta')]:
            client=socket(zmq.DEALER);client.identity=route.encode();client.connect('inproc://routes');clients[route]=client
            client.send_multipart([b'job',name.encode()]);frames=router.recv_multipart()
            router.send_multipart([frames[0],b'ACK',*frames[1:]])
        return {key:[v.decode() for v in client.recv_multipart()] for key,client in clients.items()}
Costs, waiting and recovery
AspectWhat to account for
WorkROUTER reads an address frame to choose a connection. The application handles the data frames separately.
Stored informationEach connection has an address and can hold queued messages.
Message pathThe receiver gets the reply frames after ROUTER uses the first frame to select the connection.
WaitingAn existing connection lets a message be sent. A reply can report what the application did.
RecoveryUse a stable business ID if a request must be identified across connections.

Check your understanding

Can a client-chosen routing identity be trusted as proof of customer identity?

Show hints
  1. Hint 1

    Who chose the address in this example?

  2. Hint 2

    What should remain stable if the client reconnects?

Show explanation

Check the client’s identity with an authentication mechanism and separately check whether the request is allowed. The routing address tells ROUTER which connection to use. A client can choose that address here.

ReferencesFunction documentation and helper source

References open in a new tab; course helpers are documented separately from library APIs. PyZMQ API links use version 27.3.0. ZeroMQ manuals are pinned to libzmq 4.3.5.

Experiment scope

The ROUTER and DEALER sockets share one process. The application chooses the addresses in this example. A production design needs separate identity checks and reconnect rules.

ZeroMQ reference · Runtime and model boundaries