Chapter 27
Observe a connection
Distinguish a working connection from a completed request.
Compare a connection event, a handshake event and the reply from the application.
Prerequisite: Who may issue the request?. Course map →
The situation
The transport dashboard is green while the business service is stuck in a handler.
A socket monitor reports connection events. A handshake is the initial exchange the sockets use to establish the connection. This example reads events until it sees HANDSHAKE_SUCCEEDED. It also records the server’s reply so you can see evidence at both levels.
- TransportCONNECTEDA TCP connection exists.
- ProtocolHANDSHAKE_SUCCEEDEDThe ZeroMQ handshake completed.
- Applicationpong receivedThis one handler exchange completed.
Conceptual diagram. The monitor reports the connection and handshake. Receiving pong shows this application exchange finished.
Before you run
If the handshake succeeds, what do you still need to know to tell whether the server handled ping?
Run the experiment
A client sends ping over TCP inside the guest and the server replies pong. The monitor also reports the connection and handshake.
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.
Full error details
Run output
Troubleshooting
If Python stops responding, restart the environment and try again. Your code and last completed result are kept.
Downloads
Worked result
connected and handshake_succeeded report transport events. application_reply reports the actual reply and contains pong.
Full output from the original example
{
"connected": true,
"handshake_succeeded": true,
"application_reply": "pong",
"boundary": "Transport events and one completed application exchange; not ongoing service health."
}The build ran the original editor code to produce this output.
Read the implementation
Open Inspect the socket implementation and find server.send. Which output field depends on that reply, even when connected and handshake_succeeded are true?
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 monitor():
from zmq.utils.monitor import recv_monitor_message
with lab() as socket:
server=socket(zmq.REP);port=server.bind_to_random_port('tcp://127.0.0.1')
client=socket(zmq.REQ);events=client.get_monitor_socket();events.rcvtimeo=5000;events.linger=0
try:
client.connect(f'tcp://127.0.0.1:{port}');client.send(b'ping');server.recv();server.send(b'pong');client.recv()
observed=set()
while zmq.EVENT_HANDSHAKE_SUCCEEDED not in observed:observed.add(recv_monitor_message(events)['event'])
return {'connected':zmq.EVENT_CONNECTED in observed,'handshake_succeeded':True,'application_reply':'pong','boundary':'Transport events and one completed application exchange; not ongoing service health.'}
finally:client.disable_monitor();events.close(0)
Costs, waiting and recovery
| Aspect | What to account for |
|---|---|
| Work | Collecting and reading monitor events takes work. |
| Stored information | Decide how many events to keep if the reader falls behind. |
| Message path | Monitor events travel on their own local socket. |
| Waiting | The connection can be up while the request handler is waiting or stuck. |
| Recovery | Use application records to check the status of lost or unfinished requests. |
Check your understanding
Which metrics would you add for a service that connects successfully but accumulates unfinished jobs?
Show hints
Hint 1
What can you measure even when a job never finishes?
Hint 2
How would you tell a network problem from a handler delay?
Show explanation
Track the number of accepted, completed and rejected jobs, and the age of the oldest unfinished job. Include application failures and retries. Compare those figures with connection events to locate the delay.
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.
Observe transport events
Socket.get_monitor_socket(new tab)Socket.disable_monitor(new tab)libzmq socket monitor (4.3.5)(new tab)Read how monitor events report connection changes and compare them with an application reply.
Course helper implementation
These are functions written for this course, not additional library APIs. Inspect their parameters, operator calls and authored policies. The source shown is generated from the same Python files packaged with Run and Download Python.
Experiment scope
The monitor and reply come from one exchange inside the lab. Test repeated service requests and application failures to measure ongoing service health.
ZeroMQ reference · Runtime and model boundaries