
Introduction
PUB/SUB stands for “Publisher/Subscriber” and is a messaging system used to exchange messages between different services and applications in a distributed system.
Publishers send messages to a topic in PUB/SUB, while subscribers receive messages from a subscription. PUB/SUB manages message delivery, scales automatically, and supports multiple delivery methods, making it a powerful messaging system that simplifies communication between different components of an application.
Here are the core concepts
- Topic: To which publisher sends the messages.
- Publisher: The component or service that sends the message to the topic.
- Subscription: In order to get the data published on the topic, we must subscribe to that topic. A request from a subscriber to receive messages published on a specific topic.
- Message: A data that is going to be published and subscribed.
- Message attribute: Depending on the receiver’s language, the publisher can define a key-value pair for the message.
Data flow in Pub/Sub
It is mainly dependent on the publisher application, topic, and subscribing application.
Publishers are the applications or services that generate data and want to share it with other services or applications. They send messages to a topic in Pub/Sub.
Subscribers are the applications or services that receive the messages sent to the topic. They subscribe to the topic by creating a subscription to receive messages from them.
Pull subscribers using the Pub/Sub API to request messages from a topic. When a pull subscriber requests messages, Pub/Sub returns a batch of messages.
Push subscribers: Applications that receive messages from Pub/Sub. Push subscribers are configured with a URL endpoint that receives messages from Pub/Sub. When a message is published to a topic, Pub/Sub sends the message to all push subscribers subscribed to that topic.

Overall, the data flow of Pub/Sub involves publishers sending messages to a topic, subscribers subscribing to the topic, Pub/Sub delivering messages to subscribers, and subscribers acknowledging and deleting messages once they have been processed.
How are data processes in Pub/Sub?
Create a topic:
- In the google cloud console, navigate to the Pub/Sub topic page.
- Click Create a Topic and enter a unique name

Subscribe to the Topic:
- Add a subscriber after creating Pub/Sub Topic to this. Click: Create subscriptions.
- Name the subscription and select one option push or pull and then create.

Publish a message on the topic:
To publish a message on the created topic click on the message tab, select the subscription, and add a message in the body. Now click Publish.

Pull the data Published:
- Go to subscriptions, and click on subscription Id
- In the Messages tab, click Pull.

Conclusion
Overall, PUB/SUB is a valuable messaging system that facilitates efficient and reliable communication between services and applications in a distributed system, making it an essential tool for developers working with large-scale applications.
Reference
https://cloud.google.com/pubsub/docs/overview