Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 500
Nicolò Gasparini Avatar asked May 13 '26 18:05

Nicolò Gasparini


1 Answers

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);
like image 115
Qiqi Wu Avatar answered May 15 '26 06:05

Qiqi Wu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!