In this guide, you will learn how to setup Pub/Sub and use PHP to send messages from one VM Instance and Nodejs to receive messages from another VM Instance and perform any automation tasks
Steps to setup Pub-Sub
- Create a Pub/Sub Topic
- Create a Pub/Sub Subscription
- Create a VM Instance and setup PHP to send messages
- Create a VM Instance and setup Nodejs to receive messages
- Test Pub/Sub
Create a Pub/Sub Topic
Go to your Google Cloud Console and navigate to Big Data >> Pub/Sub >> Topics
Click Create Topic
Enter a topic name
Click Create
Create a Pub/Sub Subscription
Once your topic is created click the 3 dots right to your topic and click New Subscription
Enter a subscription name
Click Create
Create a VM Instance and setup PHP to send messages
If you have your instance running you can follow the below steps to install Pub/Sub Client Libraries for PHP else you can follow this tutorial to setup VM Instance in Google Cloud
SSH to your instance and install Composer to install Pub/Sub client libraries
Navigate to your desired directory and run the following command
composer require google/cloud-pubsub
Once the installation is completed you can find the vendor
folder and two files composer.json
and composer.lock
is created
Now we can create a new file and write the code to publish a message to the created Pub/Sub Topic
sudo nano sendmessage.php
Replace YOUR_PROJECT_ID
with your Project ID, TOPIC_NAME
with your Topic name and MESSAGE
with your message and paste the below code to publish a message to the topic
require __DIR__ . '/vendor/autoload.php'; use Google\Cloud\PubSub\PubSubClient; $pubsub = new PubSubClient([ 'projectId' => 'YOUR_PROJECT_ID', ]); $topic = $pubsub->topic('TOPIC_NAME'); $topic->publish([ 'data' => 'MESSAGE' ]);
Save the file sendmessage.js
Create a VM Instance and setup Nodejs to receive messages
Create a new instance with Ubuntu 16.10 LTS Boot Image without enabling HTTP and HTTPS Firewall rules because we are not going to use this instance publically instead we are using only for running automation process.
Once you have your instance running proceed to install Nodejs and client libraries for Nodejs
SSH to your new instance and issue the commands one by one
sudo apt update curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - sudo apt-get install -y nodejs sudo apt-get install -y build-essential
Now you have installed Nodejs for listening and ready to make Nodejs listen for incoming messages
mkdir automation cd automation sudo nano listener.js
Replace YOUR_PROJECT_ID
with your Project ID, SUBSCRIPTION_NAME
with your Subscription name paste the below code to receive the messages that are published to Pub/Sub Topic
const PubSub = require('@google-cloud/pubsub'); const pubsub = new PubSub({ projectId: 'YOUR_PROJECT_ID' }); const subscription = pubsub.subscription('SUBSCRIPTION_NAME'); let messageCount = 0; const messageHandler = message => { console.log(`Received message ${message.id}:`); console.log(`\tData: ${message.data}`); messageCount += 1; message.ack(); }; subscription.on('message', messageHandler); setTimeout(() => { subscription.removeListener('message', messageHandler); console.log(`${messageCount} message(s) received.`); }, timeout * 1000);
Save the file listener.js
Start Nodejs to listen for new incoming messages
node listener.js
Now your Nodejs app is listening for incoming messages. Keep the SSH open to check for incoming messages
Testing Pub/Sub
Go to your PHP installed instance and edit the sendmessage.php
file and replace the MESSAGE
with your message and run the following command
php sendmessage.php
Now you will receive the message in the Nodejs installed instance.