Search code examples
javascriptnode.jsgoogle-cloud-platformpublish-subscribegoogle-cloud-pubsub

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


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 ?


Solution

  • 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.