How to subscribe to Google Cloud pubsub using Topic name alone (instead of subscription name)?

Viewed 658

I am working on a Nodejs project which publish and subscribe data to Google Cloud Platform PubSub. Currently, I am subscribing to Google Cloud Platform PubSub by providing subscription name. But to improve performance I was recommended to find out a way we can subscribe to PubSub Topic with topic name instead of a subscription name. So, is there a way we can do that ?

2 Answers

There is no notion of subscribing to a topic without using a subscription and so there is no such thing as a performance improvement in doing so in Google Cloud Pub/Sub. The subscription is the logical entity that expresses interest in the messages published to a topic. It is also the entity to which the state of whether or not messages are acked is attached. This means that if you stop your subscriber and restart it, you won't receive the messages that were successfully acked. It also means that independent subscriptions can exist per topic, where subscribers for each subscription receive all of the messages. Therefore, a subscription is fundamental to how Cloud Pub/Sub operates.

You won't speed up anything by listening a topic! At the opposite, you will slow down your startup time!!

Indeed, you can't receive messages from Pubsub without a subscription. If you subscribe directly to a topic, a new subscription is created and then you start to listen the message.

if you listen directly a subscription, you don't have to create it, and you start quicker to listen the messages.

The real question that you need to have is: what is your requirement?

  • Do you want to keep all the messages, even when your app is down and it doesn't listen the subscription? -> In this case, you have to create a subscription. All the messages published in the topic are forwarded to all the subscriptions (according to the filter, if applicable) and kept up to 7 days (by default). Of course, when you start to listen the subscription, you will process all the messages, first the old one, then the new one, or a mix between old and new. Pubsub doens't guarantee the order by default (except if you have activated the ordered messages option)
  • Do you not care of the past, you want to process only the instant/real time/new messages? -> In this case, create your subscription when the app start. All the old messages won't be present, only the new published one are forwarded to the new created subscription.
Related