
What is Pub/Sub?
Cloud Pub/Sub is a message queuing service that allows you to exchange messages between applications and microservices. It’s a scalable, durable, and highly available message-passing system that helps you build event-driven architectures. In this tutorial, we will show you how to use the Google Cloud console to publish and receive messages in Cloud Pub/Sub. We will also provide some tips on how to use the service more effectively.
What are the benefits of using Pub/Sub?
There are many benefits to using Pub/Sub, including the ability to:
-Easily scale your message processing
-Decouple your message producers from your message consumers
-Guarantee delivery of messages by using Google’s global infrastructure
-Monitor the health of your message processing system with built-in metrics and logs
Pub/Sub is a flexible, reliable, and cost-effective way to process large volumes of messages. it is extremely efficient at moving data between databases and in storage. A large queue of tasks can be easily distributed among multiple devices for the most balanced workload. And because Pub/Sub decouples your message producers from your message consumers, you can add or remove consumers without affecting your producers.
Types of Pub/Sub Services:-
Pub/Sub consists of two different services:
- Pub/Sub service
- Pub/Sub Lite service
Pub/Sub and Pub/Sub Lite are both horizontally scalable and managed messaging services. As the name suggests pub-sub lite is less powerful than pub-sub and In general pub-sub is the default choice.
Business requirements checklist
The following questions can help you choose the right Pub/Sub messaging service:
- Are reliability, minimal operational cost, and ease of development your top priorities? Use Cloud Pub/Sub.
- Do you require the lowest per-throughput cost, are willing to incur some additional operational overhead in terms of managing the topic capacity and can work with availability risks inherent to zonal services? Use zonal Lite topics.
- Do you require the lowest per-throughput cost, some protection against zonal failures, and are willing to incur additional operational overhead in terms of managing the topic capacity? Use regional Lite topics.
Please go through the official document to understand the differences between the two.
Core concepts of Pub/sub
- Topic: A named resource to which messages are sent by publishers.
- Subscription: In order to get the data published on the topic, we must subscribe to that topic. Subscription represents the streaming message from a unique topic to be delivered to the subscribing application.
- 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.
- Publisher: An application that creates and sends messages to single or multiple topics.
- Subscriber: An application with a subscription to single or multiple topics to receive messages from it.
- Push and pull: The two message delivery methods. A subscriber receives messages either by Pub/Sub pushing them to the subscriber-chosen endpoint or by the subscriber pulling them from the service.
How to publish and receive a message in pub/sub
First, we need to Set up a Google Cloud console project.
- Create or select a project.
- Enable the Pub/Sub API for that project.
Create a Topic:
- In a google cloud console, navigate to the pub-sub page.
- Click on create topic button and enter a unique name



Subscribe to the Topic:
- To add the subscription to the topic, click on the subscription
- Name the subscription and select one of the options as push or pull and leave the default options for the remaining options.
- Click Create



Publish a message on the topic:
- To publish a message on the created topic, click on a topic
- Click on the message tab
- Select Subscription
- Click Publish Message
- In the Message body, add the message
- Click Publish. A message at the bottom of the page says “Message published” if the publication was successful.



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



You’ll be able to see the messages you just published, The messages has the same data and time when the message was published.
Conclusion
Google Cloud Pub/Sub is a great way to receive and publish messages in an asynchronous manner. It’s simple to use and can be easily integrated into your existing applications. In this article, we’ve shown you how to use the Google Cloud console to publish and receive messages in Pub/Sub. Give it a try and see how it can benefit your application development workflow.