Skip to content

Subjects and Wildcards

Outcome: you can design a subject tree for a small system, and predict — before running anything — exactly which of three test messages each of two wildcard subscriptions will receive. The transcript then grades your prediction.

Subjects are names, designed by you

A subject is a dot-joined list of words: shop.order.1. The dots are not decoration; they make the subject a path in a tree, and the wildcards below prune by tree position. Nothing registers or validates your tree — the first publisher to use a name brings it into being. That freedom rewards a little design:

  • General to specific, left to right. shop.order.1 reads like a filing path: the shop, its orders, this one. Wildcards can then cover "all orders" or "the whole shop" without a list.
  • Words are yours, but keep them dull. Lowercase letters, digits, dashes. Spaces are not allowed at all — the client refuses them outright (transcript t12 meets nats: error: nats: invalid subject).
  • Version later if you must. A subject tree is an interface other programs will depend on; a v2 word near the front (shop.v2.order.1) is the honest way to change shape without breaking the old listeners.

The two wildcards

  • * — exactly one word. shop.* matches shop.order and shop.refund. It does not match shop.order.1 — that is two words below shop..
  • > — one or more words, and it must be the last thing in the pattern. shop.> matches shop.order, shop.order.1, and shop.eu.refund.late alike. A pattern of just > hears the entire server.

Publishers never use wildcards; only subscriptions do. A publisher speaks one exact name at a time.

The demonstration

Two subscribers, three messages (transcript t03):

nats sub 'orders.*'     # exactly one word after orders.
nats sub 'orders.>'     # one or more words after orders.

nats pub orders.new            "order 1001 placed"
nats pub orders.eu.new         "order 1002 placed in eu"
nats pub orders.eu.cancelled   "order 1003 cancelled"

Quote the patterns — the shell has its own opinions about * and >, and chapter 14 returns to that.

Results, exactly as the tree predicts:

Message orders.* orders.>
orders.new ✔ received ✔ received
orders.eu.new — ✔ received
orders.eu.cancelled — ✔ received

The orders.* window shows precisely one message; the orders.> window shows all three. When a subscription seems to be missing messages, the usual story is this table: a * where the tree is deeper than one word.

A word on listening to everything

Subscribing to > (or a broad tree like shop.>) is the standard way to watch a system while developing — the lab's neighbours chapter suggests it as a debugging habit. It is also a responsibility: on a busy server that is a copy of all traffic, and on a secured server it is exactly the sort of subscription permissions exist to fence (chapter 10). Listen widely on your own machine; ask before doing it on anyone else's.

Check: repeat the demonstration with your own tree — say sensors.* against sensors.kitchen and sensors.kitchen.temp — and confirm the * window sees only the one-word subject.