Create your first Cloud Pub/Sub topic

A Cloud Pub/Sub topic is a named resource to which devices send messages. Create your first topic with the following command:

gcloud pubsub topics create my-topic

You will send several messages to this topic later.

Create a subscription to the device’s topic

Run the following command to create a subscription, which allows you to view the messages published by your device:

gcloud pubsub subscriptions create \

projects/gcp-fundamentals-240210/subscriptions/my-subscription \


Clone the Cloud IoT Core Node.js sample files from GitHub

You’ll use the MQTT sample to send messages to Cloud IoT Core.

Note: If the directory already exists, remove the previous files before cloning:rm -rf nodejs-docs-samples

Clone the sample program with the following command:git clone \

Grant permission to the Cloud IoT Core service account

In this section, you use a helper script to add the service account to the Cloud Pub/Sub topic with the Publisher role.

Navigate to the iot/ directory:

cd nodejs-docs-samples/iot/

Install the dependencies:

npm –prefix ./scripts install

Run the helper script:

node scripts/iam.js my-topic

The script grants permission to the Cloud IoT Core service account on the my-topic topic.

Create a device registry

A device registry contains devices and defines properties shared by all of the contained devices. Create your device registry with the following command:gcloud iot registries create \ my-registry \ –project=gcp-fundamentals-240210 \ –region=us-central1 \ –event-notification-config=topic=projects/gcp-fundamegcloud iot registries create \ my-registry \ –projects/gcp-fundamentals-240210/topics/my-topic

Generate your signing keys

To authenticate to Cloud IoT core, a device needs a private key and a public key. Generate your signing keys by running the following command:./scripts/

Create a device and add it to the registry

Run the following command to create a device and add it to the registry:gcloud iot devices create \ my-node-device \ –project=gcp-fundamentals-240210 \ –region=us-central1 \ –registry=my-registry \ –public-key \ path=rsa_cert.pem,type=rs256

Connect your device and publish messages

In this section, you send messages from a virtual device to Cloud Pub/Sub.

Navigate to the MQTT sample directory

cd mqtt_example

Install the Node.js dependencies

npm install

Connect a virtual device to Cloud IoT Core using the MQTT bridge

node \ cloudiot_mqtt_example_nodejs.js \ mqttDeviceDemo \ –cloudRegion=us-central1 \ –projectId=gcp-fundamentals-240210 \ –registryId=my-registry \ –deviceId=my-node-device \ –privateKeyFile=../rsa_private.pem \ –numMessages=25 \ –algorithm=RS256 \ –mqttBridgePort=443

The output shows that the virtual device is publishing messages to the telemetry topic. 25 messages are published.

Pull published messages

Pull the messages published by the device with the following command:gcloud pubsub subscriptions pull \ –auto-ack \ projects/gcp-fundamentals-240210/subscriptions/my-subscription

Running this command returns the messages published by the device. The messages have the following data, my-registry/my-node-device-payload-[INTEGER], a MESSAGE_ID, and an ATTRIBUTES list of information about the device. The MESSAGE_ID is a unique ID assigned by the server.

Note: Cloud Pub/Sub doesn’t guarantee the order of the messages. It is also possible that you’ll see only one message in Cloud Shell. In that case, run the same command multiple times to see the other messages.

View resources in the GCP Console

This concludes the gcloud command line tutorial, but you can also use the GCP Console to view the resources you just created.

Open the Navigation menu in the upper-left corner of the console, and then select IoT Core.

IoT Core

You can also use this graphical user interface to create and manage device registries and devices.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s