Google Associate Cloud Engineer – Asynchronous Communication in Google Cloud with Cloud Pub Sub Part 2

  • By
  • August 20, 2023
0 Comment

5. Step 05 – Playing with Cloud Pub Sub from Command Line

Back in this step, let’s play with Pub Sub from the command line. The Gcloud command is pub. Sub. So gcloud pub sub topics create and create your topic. Let’s reconnect to our Google Cloud shell and I’ll just take the command from here. Gcloud pub Sub Topics Create as we discussed earlier, we would create a topic, we’d create a subscription and then we can pull this subscription. And then we can look at all the messages which are put into that specific topic. So Gcloud Pub serves Topics Create and I would want to create a topic before that, we would want to set up project, right? So let’s start with actually setting up the project first. So let’s do a set project and let’s copy this and set the project ID. So the project ID is set.

Let’s do an authorized and then I can say gcloud pub Sub topics Create and give the name of the topic. So I’ll say Topic from Gcloud so we are creating this in Gcloud. So I’ll say topic from Gcloud. Is that sufficient to create a topic? Looks like that. So we were able to easily create a topic. And once we have a topic, what we can do is we can create subscriptions for the topic. So the next thing that we want to do is to create a subscription for that specific topic. So gcloud Pub Sub subscriptions Create give the name of the subscription and the topic name. So let’s try that. Create the name of the subscription that we would want to create is subscription g Cloud One so I’m saying Subscription g Cloud One and out.

Specify the topic on which topic do you want to create this subscription on? I want to create it on this specific topic. Topic from Gcloud so gcloud pub submissions create subscription gcloud one topic is called topic from Gcloud So that’s the subscription we are creating. Fingers crossed. Will it work? Let’s wait for it. The subscription is now created. That’s awesome. Now let’s not stop with one subscription. Let’s create one more subscription and I’ll call this Gcloud Two. So subscription gcloud Hyphen Two that’s the next subscription that I’m creating. So we now have a topic and if this command is successful and it is, we have two subscriptions as well.

When we are creating a subscription, you can pass in additional options if you want ordered Message Delivery all the messages should be sent to the subscription in the order in which they are arrived. Then you can use the option Hyphen Hyphen enable Message Ordering you can also configure an acknowledgement deadline. How long? What is the deadline before you have to receive the Acknowledgment? For that specific subscription, you can also configure a message filter. Not all Message some subscriptions might not be interested in all messages on this specific topic. You can say I am only interested in a specific set of messages by configuring a message filter, we have our topic ready. Our subscriptions ready.

The next thing we want to do is to pull the subscription. What would happen if I pull a subscription right now? Let’s just say subscriptions. Let’s not really worry about the topic north so I’ll remove the topic. So this is the command which we created which we use to create the subscription. I’ll just change create to pull so gcloud pub sub subscriptions pull gcloud pub sub and the name of the subscription. Let’s see what would happen. Listed zero items. Why is it listing zero items? Because there are no messages to be pulled. Even if I pull this first. Subscription subscription g cloud One you’d see that there are listed zero items. So if I would want to actually pull something, there must be something on the topic.

Let’s actually put something on the topic. How could I put a message on the topic? Try and guess the command. Will you put the message on a topic or a subscription? You’ll put it on the topic, right? So let’s go to the same command that we used to create the topic. So this is the one which we created. This is the one which we used to create the topic. And I would want to publish a message. It should be easy to guess, right? It should be published to this specific topic. And I can say what is the message? I can say Hi, F and Hyphen message is equal to what is the message I would want to publish? I would say my first message. So we are publishing this message to the topic and I would again publish a couple of more messages.

My second message. And let’s also publish my third message. So my third message. That’s awesome, right? We have a topic. We published three messages. We have subscriptions now I would go and pull subscription one. So let’s do a pull on subscription one. So gcloud pub sub subscriptions pull subscription gcloud one what would it get? It should get one of the messages, right? There are messages available. So it’s getting me my first message now. And you can see that there is data which is coming in. The message ID is coming in along with it. You would also see that there is an Acknowledgment ID which is coming in. If you want to acknowledge this message, this is the acknowledgement ID that you need to make use of to acknowledge the processing of this specific message.

If the consumer has successfully processed the message, he can use this acknowledgement ID and he can send an acknowledgement and he can send an acknowledgement back to the topic. And this should happen within a deadline. The default is 10 seconds now because I have not sent an acknowledgement back. What would happen when I actually pull the subscription again? Let’s see what would happen. You are getting the same message back again. Because we have not successfully processed the message. Pub sub says hey, you have not processed this message successfully again last time. So I’ll send you another message. So if you keep executing this pull again and again, you would see that you would get the different kinds of messages.

My first message of my second message of if you immediately execute the whole thing, you’d see that you would receive the other messages. My third message. So as we keep pulling, we get the messages, but what we are not doing in here is we are not sending an acknowledgement back. And as long as we don’t send the Acknowledgment back, the Pub sub would be sending the same message to the same subscription to enable us to send Acknowledgment. What I would do is I would say hyphen hyphen auto acknowledgement. Instead of trying to copy this entire Acknowledgment ID from here, what I’m doing is I’m taking a shortcut, I’m saying pull and immediately acknowledge the message that’s autoac.

Let’s see what would happen, how to pull the message. My first message message ID is here. However, there is no acknowledgement ID because we don’t need the Acknowledgment ID. We have used autnowledged. So you’d see that if you again pull another message, so if I’m executing the next one, I’m just pulling without auto acknowledgment, you’d see that either the second message or the third message would come. So the second message has come. If I execute it enough, the third message would come. However, the first message will never come again. Because on that specific subscription, subscription one, we have already processed all the messages.

So if you keep executing the pull without auto acknowledge, you’d see that you would keep getting my second and third messages. And what I can do now is I would again execute based on auto acknowledgment. So let’s acknowledge do auto acknowledgement for the next message. It should be pulled so this can be second or third message. Let’s see what would be coming back. Second message is coming back and we are setting an auto acknowledgment. So if I do a pull again without auto acknowledgment, what would I get back? I would get the third message. I’m getting the third message because that is the only message that is not processed by this specific subscription. So if I keep sending that a few times, you’d see that it would keep repeating my third message.

Now, what I would do is I would now say auto acknowledgement. I would say auto acknowledgment for the third one as well. Now, if I do a pull on the first subscription, you would see that I will not get any messages anymore because all the messages are successfully processed by that specific subscription. However, what do you think will happen if I do a pull on the second one? So I’m using subscription gcloud two NAV. You’d see that the messages are still yet to be processed by the second subscription and therefore the consumer will receive the message. So you can actually keep pulling the message and you’d see that if you keep running it often, you’d see that we would circle between the My 1st, second and the third messages as it was the case with the subscription gcloud one.

And if you want, you can send Auto Acknowledge. So it’s Hyphen hyphen auto Acknowledge and if you execute it three times, you would see that first message, second message, and third message. And once we have processed all the messages, if you try and do pull again, you’ll get zero messages back. We already looked at the pull command subscriptions pull my Subscription you can specify Auto Acknowledgment and also you can specify limit to how many messages you would want to be able to pull. We talked about the fact that we can send an acknowledgement back. If you are not using Auto ECH, then you can use the subscriptions Acknowledge My subscription and pass in the Acknowledgment ID.

So that long string that we saw earlier so this long string that we are seeing in here is the Acknowledgement ID. So you need to manually send that Acknowledgment ID as part of your command. Now, let’s look at a few more commands. Related Topics if I would want to list all the topics, I can say g Cloud Pub sub Topics List so let’s just try that. Let’s give it a try. Where am I? G Cloud Pub Subtopics List so this would list all the active topics. Let’s do a clear and let’s if you want, you can also delete a specific topic. Let’s now delete the topic which we have created from Gcloud. So I can say pick the topic name and say gcloud pub sub topics delete and the name of the topic. This would delete the topic, so the topic is now deleted.

You can also do a list on the topic and you can also list the subscriptions on a specific topic. Let’s try this command so gcloud pub sub Topics List Subscriptions because we have deleted the topic from Gcloud, I’ll use the My first topic, so I’ll say oops my first topic. Let’s see what would happen. Cool. You can see that there are two subscriptions that are on that specific topic. Instead, we played around with Pub sub. We were able to create a topic. We created subscriptions to that topic, we put messages on that particular topic, and then we were able to pull the messages from the subscriptions and we were able to process those messages. I’m sure you’re having a wonderful time.

Comments
* The most recent comment are at the top

Interesting posts

The Growing Demand for IT Certifications in the Fintech Industry

The fintech industry is experiencing an unprecedented boom, driven by the relentless pace of technological innovation and the increasing integration of financial services with digital platforms. As the lines between finance and technology blur, the need for highly skilled professionals who can navigate both worlds is greater than ever. One of the most effective ways… Read More »

CompTIA Security+ vs. CEH: Entry-Level Cybersecurity Certifications Compared

In today’s digital world, cybersecurity is no longer just a technical concern; it’s a critical business priority. With cyber threats evolving rapidly, organizations of all sizes are seeking skilled professionals to protect their digital assets. For those looking to break into the cybersecurity field, earning a certification is a great way to validate your skills… Read More »

The Evolving Role of ITIL: What’s New in ITIL 4 Managing Professional Transition Exam?

If you’ve been in the IT service management (ITSM) world for a while, you’ve probably heard of ITIL – the framework that’s been guiding IT professionals in delivering high-quality services for decades. The Information Technology Infrastructure Library (ITIL) has evolved significantly over the years, and its latest iteration, ITIL 4, marks a substantial shift in… Read More »

SASE and Zero Trust: How New Security Architectures are Shaping Cisco’s CyberOps Certification

As cybersecurity threats become increasingly sophisticated and pervasive, traditional security models are proving inadequate for today’s complex digital environments. To address these challenges, modern security frameworks such as SASE (Secure Access Service Edge) and Zero Trust are revolutionizing how organizations protect their networks and data. Recognizing the shift towards these advanced security architectures, Cisco has… Read More »

CompTIA’s CASP+ (CAS-004) Gets Tougher: What’s New in Advanced Security Practitioner Certification?

The cybersecurity landscape is constantly evolving, and with it, the certifications that validate the expertise of security professionals must adapt to address new challenges and technologies. CompTIA’s CASP+ (CompTIA Advanced Security Practitioner) certification has long been a hallmark of advanced knowledge in cybersecurity, distinguishing those who are capable of designing, implementing, and managing enterprise-level security… Read More »

Azure DevOps Engineer Expert Certification: What’s Changed in the New AZ-400 Exam Blueprint?

The cloud landscape is evolving at a breakneck pace, and with it, the certifications that validate an IT professional’s skills. One such certification is the Microsoft Certified: DevOps Engineer Expert, which is validated through the AZ-400 exam. This exam has undergone significant changes to reflect the latest trends, tools, and methodologies in the DevOps world.… Read More »

img