Zum Hauptinhalt springen
Zum Hauptinhalt springen

Concept

Publish/subscribe (pub/sub)

Publish/subscribe is an asynchronous messaging pattern where senders publish to topics and receivers subscribe, via a broker that decouples both.

Publish/subscribe (pub/sub) is a messaging pattern in which senders, called publishers, send messages to a named channel, the topic, instead of to specific receivers. A broker or messaging service delivers each message to every subscriber registered for that topic, so publishers and subscribers never need to know about each other. MQTT and Google Cloud Pub/Sub are both built on it.

How publish/subscribe works

Publishers produce messages, subscribers declare which topics they want, and the broker matches the two. A temperature sensor publishes to a topic such as plant1/line2/temperature; a dashboard and a historian can each subscribe to it without any change on the sensor.

That decoupling has practical effects:

  • The publisher needs only the topic name, not the address of each subscriber.
  • One message can reach many subscribers, and you can add new consumers without touching the publisher.
  • Communication is asynchronous: the publisher doesn't wait for subscribers to process the message.

Pub/sub vs request/response

In request/response (HTTP, REST, or CoAP in its basic form), a client asks a specific server for something and waits for the answer. That fits commands and queries. Pub/sub fits streams of events flowing from many devices to many consumers. Many IoT systems use both: devices publish telemetry over MQTT, and applications query the stored data through an HTTP API. Our MQTT vs REST comparison covers the trade-offs.

Pub/sub in MQTT and in the cloud

In MQTT, the broker routes messages by topic. Subscriptions can use wildcards: + matches one level and # matches any number of levels, so factory/+/temperature covers every line. MQTT also attaches a delivery guarantee to each message (QoS 0, 1 or 2).

Google Cloud Pub/Sub applies the same pattern as a managed service: publishers send to topics, and subscribers receive through pull, push or export subscriptions. Delivery is at least once by default, with an exactly-once option.

Pub/sub in Cloud Studio IoT

Devices that connect to a Cloud Studio IoT instance over MQTT use the pattern in both directions: they publish readings to {MQTTUserID}/uplink/{DeviceAddress} and subscribe to {MQTTUserID}/downlink/{DeviceAddress} to receive commands, through the instance's own MQTT server with TLS on port 8883.

Frequently asked questions

What does pub/sub mean?+

Pub/sub is short for publish/subscribe: messages are published to topics and an intermediary delivers them to whoever has subscribed, without the sender and the receivers knowing each other.

Is MQTT a pub/sub protocol?+

Yes. The MQTT specification describes it as a client server publish/subscribe messaging transport: clients publish to topics on a broker, and the broker delivers the messages to subscribers.

What is the difference between pub/sub and a message queue?+

In pub/sub, every subscriber to a topic gets its own copy of each message. In a queue, each message goes to a single consumer, which spreads work across workers. MQTT 5.0 covers that second case with shared subscriptions.

Related terms

Related articles