Messaging Patterns LabPython · ZeroMQ

Chapter 8

Meet the late subscriber

Find out what a subscriber receives when it joins late.

What you’ll learn

Check which prices reach a subscriber that connects after the first publication.

Prerequisite: Filter a publication. Course map →

The situation

A dashboard opens after the initial price was published and waits forever for its current value.

A subscriber receives live messages after its subscription takes effect. In the starter, the publisher sends the old price before there is a subscriber. The publisher then waits for the subscription message before it sends the new value.

A late subscriber does not acquire old history
  1. Publisher (XPUB)Publisher sends oldNo subscriber yet
  2. Publisher sees subscriptionSubscriber asks for priceSubscriber (SUB) connects
  3. Publisher sends newPublisher sends new to the subscriberSubscriber receives new

Conceptual diagram. The old publication is deliberately sent before a subscriber exists. No log or replay store retains it.

Before you run

Will the later subscriber receive old before new?

Run the experiment

The publisher sends old before a subscriber connects. Once the publisher receives a subscription, it sends new.

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

The publisher sends both values. The received field contains the topic price followed by new only. old was sent before the subscriber existed.

Full output from the original example
{
  "published": [
    "old",
    "new"
  ],
  "received": [
    "price",
    "new"
  ],
  "unconnected_publish_was_not_replayed": true
}

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

Try one change

Replace the last line in the editor with result = slow_joiner(publish_early=False). Click Run again. The received list names the topic price once, then shows the value from each message that arrived. What changed when the publisher waited for the subscription before sending both values?

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 slow_joiner(publish_early=True):
    with lab() as socket:
        pub=socket(zmq.XPUB);pub.bind('inproc://early')
        if publish_early:pub.send_multipart([b'price',b'old'])
        sub=socket(zmq.SUB);sub.subscribe=b'price';sub.connect('inproc://early');pub.recv()
        if not publish_early:pub.send_multipart([b'price',b'old'])
        pub.send_multipart([b'price',b'new'])
        messages=[[frame.decode() for frame in sub.recv_multipart()] for _ in range(1 if publish_early else 2)]
        received=messages[0] if publish_early else [messages[0][0],*(value for _,value in messages)]
        return {'published':['old','new'],'received':received,'unconnected_publish_was_not_replayed':True if publish_early else None}
Costs, waiting and recovery
AspectWhat to account for
WorkThe application still makes the early send call.
Stored informationThis example keeps no history for later readers.
Message pathThere is no subscriber to receive the early send.
WaitingThe publisher waits for a subscription message before the later send.
RecoveryA system needs retained data to offer replay when a subscriber reconnects.

Check your understanding

A new dashboard needs the current price even if there are no updates today. What must the service provide?

Show hints
  1. Hint 1

    What does the service keep after it sends the first price?

  2. Hint 2

    How would a subscriber tell whether an update came before or after the saved value?

Show explanation

Save the latest price and return it on request, or keep a history that a subscriber can replay. Use a version number to show where the saved value and later live updates meet so the client can tell whether it missed a newer price.

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

This run controls the join order inside one process. It shows which messages reach the subscriber. Measuring network loss or reconnect behavior would require different tests.

ZeroMQ reference · Runtime and model boundaries