Using Google Cloud Pub/Sub in a Local Environment
Google Cloud Pub/Sub is a robust, serverless messaging service
In this article, we will explore how to setup and use the Google Cloud Pub/Sub emulator for local development. This will allow you to develop and test your applications locally without needing to interact with the actual Pub/Sub service in the cloud, saving costs and allowing for faster development cycles
We will cover the following topics:
By the end of this article, you will have a solid understanding of how to effectively use Google Cloud Pub/Sub in your local development environment.
Setting up the Google Cloud Pub/Sub emulator
The Google Cloud Pub/Sub emulator is part of the Google Cloud SDK. If you haven't installed the Google Cloud SDK yet, you can download it from the official Google Cloud SDK documentation.
Once you have the Google Cloud SDK installed, you can start the Pub/Sub emulator using the gcloud command-line tool.
Step 1: Install the Pub/Sub emulator
First, you need to install the Pub/Sub emulator. You can do this using the gcloud command-line tool:
gcloud components install pubsub-emulator
Step 2: Start the Pub/Sub emulator
After the emulator is installed, you can start it with the following command:
gcloud beta emulators pubsub start
This command will start the emulator and print the emulator's host and port number, which you will need to configure your application
Step 3: Set environment variables
To tell your application to use the Pub/Sub emulator instead of the real Pub/Sub service, you need to set the PUBSUB_EMULATOR_HOST environment variable to the host and port number of the emulator. You can do this in the terminal with the following command:
export PUBSUB_EMULATOR_HOST=localhost:8085
Replace localhost:8085 with the actual host and port number of your emulator if it's different.
Now, your application will use the Pub/Sub emulator when it uses the Pub/Sub client libraries. You can test this by running your application locally and seeing if it interacts with the emulator.
In the next section, we will discuss how to configure your application to use the Pub/Sub emulator.
Configuring your application to use the local Pub/Sub emulator
Once you have the Pub/Sub emulator running, you need to configure your application to use it. This involves setting the PUBSUB_EMULATOR_HOST environment variable and using it in your application.
Step 1: Set the PUBSUB_EMULATOR_HOST environment variable
As mentioned earlier, you need to set the PUBSUB_EMULATOR_HOST environment variable to the host and port number of the emulator. This is typically done in the terminal before you start your application:
export PUBSUB_EMULATOR_HOST=localhost:8085
Replace localhost:8085 with the actual host and port number of your emulator if it's different.
Step 2: Use the PUBSUB_EMULATOR_HOST environment variable in your application
In your application, you need to create a Pub/Sub client that connects to the emulator instead of the real Pub/Sub service. This is done by using the PUBSUB_EMULATOR_HOST environment variable when creating the client.
Recommended by LinkedIn
Here's an example of how to do this in a Node.js application using the @google-cloud/pubsub library:
const {PubSub} = require('@google-cloud/pubsub');
// Creates a client; cache this for further use
const pubSubClient = new PubSub({
projectId: 'YOUR_PROJECT_ID',
apiEndpoint: process.env.PUBSUB_EMULATOR_HOST,
});
Replace 'YOUR_PROJECT_ID' with your actual Google Cloud project ID.
Now, whenever you use pubSubClient in your application, it will interact with the Pub/Sub emulator instead of the real Pub/Sub service.
In the next section, we will discuss how to test your application with the Pub/Sub emulator.
Testing your application with the Pub/Sub emulator
Once you have your application configured to use the Pub/Sub emulator, you can test it by publishing and subscribing to messages.
Step 1: Publish a message
First, let's publish a message. Here's an example of how to do this in a Node.js application:
const {PubSub} = require('@google-cloud/pubsub');
// Creates a client; cache this for further use
const pubSubClient = new PubSub({
projectId: 'YOUR_PROJECT_ID',
apiEndpoint: process.env.PUBSUB_EMULATOR_HOST,
});
async function publishMessage() {
const topicName = 'my-topic';
const data = JSON.stringify({foo: 'bar'});
const dataBuffer = Buffer.from(data);
const messageId = await pubSubClient.topic(topicName).publish(dataBuffer);
console.log(`Message ${messageId} published.`);
}
publishMessage();
This code publishes a message with the data {foo: 'bar'} to the topic 'my-topic'.
Step 2: Subscribe to messages
Next, let's subscribe to messages. Here's an example of how to do this:
async function subscribe() {
const subscriptionName = 'my-subscription';
// References an existing subscription
const subscription = pubSubClient.subscription(subscriptionName);
// Create an event handler to handle messages
let messageCount = 0;
const messageHandler = (message) => {
console.log(`Received message ${message.id}:`);
console.log(`\tData: ${message.data}`);
console.log(`\tAttributes: ${message.attributes}`);
messageCount += 1;
// "Ack" (acknowledge receipt of) the message
message.ack();
};
// Listen for new messages until timeout is hit
subscription.on('message', messageHandler);
setTimeout(() => {
subscription.removeListener('message', messageHandler);
console.log(`${messageCount} message(s) received.`);
}, timeout * 1000);
}
subscribe();
This code subscribes to the subscription 'my-subscription' and logs any messages it receives.
Step 3: Verify the results
Run your application and verify that it publishes and receives messages as expected. You should see the published message ID in the console, followed by the received message data.
Remember to start the Pub/Sub emulator and set the PUBSUB_EMULATOR_HOST environment variable before running your application.
Conclusion
In this article, we've explored how to set up and use the Google Cloud Pub/Sub emulator for local development and testing. We've seen how to configure our application to use the emulator, how to publish and subscribe to messages, and how to test error handling
The Pub/Sub emulator is a powerful tool for developing and testing applications that use Google Cloud Pub/Sub. It allows you to work offline, reduces costs by not using real Pub/Sub resources, and makes it easier to test error handling and edge cases.
However, keep in mind that the emulator does not perfectly replicate the real Pub/Sub service. There may be differences in behavior, performance, and supported features. Always test your application with the real Pub/Sub service before deploying it to production.
Finally, remember to start the emulator and set the PUBSUB_EMULATOR_HOST environment variable before running your application. Happy coding!
Next Steps
Now that you're familiar with the Pub/Sub emulator, you might want to explore other parts of the Google Cloud SDK. The SDK includes emulators for other Google Cloud services, such as Datastore and Bigtable, as well as tools for deploying your application to Google Cloud.
You might also want to learn more about Google Cloud Pub/Sub itself. Pub/Sub is a powerful service for building event-driven applications and microservices. It supports a variety of message delivery methods, including push and pull, and it can scale to handle millions of messages per second.
Further Reading
That's it! I hope you found this article helpful. If you have any questions or feedback, please let me know in the comment section below.