Messaging Patterns LabPython · ZeroMQ

Chapter 7

Filter a publication

Match the first bytes of a topic name.

What you’ll learn

Find which topics reach the subscriber for each prefix.

Prerequisite: Who owns the socket?. Course map →

The situation

A subscriber asks for price and also receives a topic named pricey.

The publisher sends a topic name as the first frame. A subscription specifies a prefix, the bytes the name must start with. A SUB socket receives topics that match. An XPUB socket is a publisher that can see messages announcing subscriptions, so the example can wait for the subscription before sending the data.

Prefixes select different publications
PublisherXPUBFirst-frame topics: prices, pricey, orders
  1. SUB · prefix priceprices and priceyBoth begin with the same byte prefix.
  2. SUB · prefix ordersordersOnly orders starts with this prefix.

Conceptual diagram. The diagram compares two filter settings. Each run creates one subscriber and waits for its subscription to reach the publisher.

Before you run

Which two topic names match price?

Run the experiment

The subscriber asks for price. Once the publisher sees the subscription, it sends prices/12, pricey/99 and orders/a.

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

received contains prices and pricey. In this run the publisher saw the subscription before it sent any data.

Full output from the original example
{
  "prefix": "price",
  "received": [
    [
      "prices",
      "12"
    ],
    [
      "pricey",
      "99"
    ]
  ],
  "subscription_seen_by_publisher": true
}

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

Try one change

Replace the last line with result = subscriptions(prefix='orders') and click Run. Then use result = subscriptions(prefix='') and click Run again. In each result, check the topics in received. Which topics match each prefix?

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 subscriptions(prefix='price'):
    with lab() as socket:
        pub,sub=publisher(socket,prefix.encode())
        rows=[['prices','12'],['pricey','99'],['orders','a']]
        for topic,value in rows:pub.send_multipart([topic.encode(),value.encode()])
        expected=[row for row in rows if row[0].startswith(prefix)]
        received=[[f.decode() for f in sub.recv_multipart()] for _ in expected]
        return {'prefix':prefix,'received':received,'subscription_seen_by_publisher':True}
Costs, waiting and recovery
AspectWhat to account for
WorkThe socket examines topic bytes to match subscriptions.
Stored informationEach subscriber can have its own queue.
Message pathMore than one matching subscriber can receive a copy.
WaitingWhen XPUB sees a subscription, it can send matching messages. An application reply would be separate evidence of processing.
RecoveryA late subscriber needs a separate source for history.

Check your understanding

Only a few customers should receive the orders topic. What can topic names do, and where would you enforce permission?

Show hints
  1. Hint 1

    Which part of the topic does SUB examine?

  2. Hint 2

    What would stop a client from asking for a broader prefix?

Show explanation

A naming rule can separate topic prefixes. Enforce permissions at a boundary that controls which data a client can receive. For example, an application can authorize subscriptions before forwarding messages to a client.

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

XPUB and SUB apply the topic prefix in this example. A service that restricts who can read a topic needs an authorization rule at its application or connection boundary.

ZeroMQ reference · Runtime and model boundaries