Search code examples
node.jsgoogle-cloud-pubsubgoogle-cloud-consolegoogle-cloud-pubsub-emulator

Test Google Cloud PubSub emulator with a push subscription


I'm trying to setup a GCP PubSub service that will work with a push type subscription. However it's impossible to create one in the developement stage, while I have no accessible endpoints.

I assumed that the emulator would allow me to specify a local endpoint so that the service would run flawlessly in local.

However, after setting it up, I couldn't find a way in the Node.js pubsub library to create a subscription while specifying its options, there is no example for this.

This is the pretty simple way to create a simple, default, pull, subscription:

await pubsub.topic(topicName).createSubscription(subscriptionName);

Solution

  • Here is an example of how you would set up push subscription. It is the same as how you would set it up if you were running in the actual Pub/Sub environment. Specify ‘pushEndpoint’ as your local endpoint. When running on the emulator, it will not require authentication for your endpoint.

    You can do something like the following:

     // Imports the Google Cloud client library
     const {PubSub} = require('@google-cloud/pubsub');
    
     // Creates a client
     const pubsub = new PubSub();
    
     const options = {
       pushConfig: {
         // Set to your local endpoint.
         pushEndpoint: `your-local-endpoint`,
       },
     };
    
     await pubsub.topic(topicName).createSubscription(subscriptionName, options);