Skip to content
Stand with Ukraine flag

Google Cloud Pub/Sub Integration

Google Cloud Pub/Sub is a fully managed publish/subscribe messaging service. The ThingsBoard Pub/Sub integration connects to a GCP pull subscription as a subscriber: it reads messages published to a topic, decodes them with an uplink converter, and stores the resulting telemetry and attributes in ThingsBoard.

A device or cloud service publishes a message to a Pub/Sub topic, and the message is delivered to the pull subscription that ThingsBoard consumes. The integration reads the message and forwards it to the uplink converter, which decodes the payload into telemetry and attributes, and the ThingsBoard Core Service stores the data, provisioning the device automatically on the first message.

Before creating the integration, ensure:

  • You have access to ThingsBoard PE or ThingsBoard Cloud with integration functionality enabled for your tenant.
  • You have permissions to create integrations and data converters.
  • You have a Google Cloud project with the Cloud Pub/Sub API enabled.
  • You can create Pub/Sub topics, subscriptions, and service accounts in that project (or someone can provide them to you).

Prepare the topic, subscription, and service account in the Google Cloud Console before creating the integration in ThingsBoard.

  1. Open your GCP project and click Search in the top toolbar, then search for Cloud Pub/Sub API.
  2. Select Cloud Pub/Sub API from the search results.
  3. On the Cloud Pub/Sub API page, click Enable if not already enabled.
  4. Once enabled, the API details page shows Status: Enabled.
  1. Click Search in the top toolbar, search for Pub/Sub, and select Pub/Sub from the results.
  2. Go to the Topics page and click Create topic.
  3. Enter a Topic ID (e.g. tb-uplink) and keep Add a default subscription checked — this creates a pull subscription together with the topic.
  4. Scroll down and click Create.
  5. GCP creates the topic and its subscription (e.g. tb-uplink-sub) together, listed in the topic’s Subscriptions tab.
  6. Open the subscription to confirm its Subscription state is active and it uses Pull delivery.

Create a Service Account and Download the Key

Section titled “Create a Service Account and Download the Key”

ThingsBoard authenticates to GCP using a service account key. The account needs at least the Pub/Sub Subscriber role to receive uplink messages.

  1. Click Search in the top toolbar, search for Service Accounts, and select it from the results.
  2. On the Service accounts page, click Create service account.
  3. Enter a name (e.g. tb-pubsub) and click Create and continue.
  4. In the Permissions step, search for the Pub/Sub Subscriber role and select it.
  5. With the role assigned, click Continue, then Done.
  6. The service account appears in the list with status Enabled. Click its email to open it.
  7. Go to the Keys tab and click Add key ⇾ Create new key.
  8. Select JSON as the key type and click Create.
  9. The private key file downloads automatically — store it securely; you will upload it to ThingsBoard when creating the integration.
  10. The key now appears as Active in the Keys tab.

To set up the Pub/Sub integration, first create an uplink data converter to process incoming messages, then create and configure the integration.

The uplink converter decodes incoming Pub/Sub messages and maps them to the ThingsBoard data model. Pub/Sub uses a generic uplink converter.

The decoder function receives:

  • payload — the raw Pub/Sub message data as a UTF-8 byte array
  • metadataintegrationName and any key-value pairs configured in the integration’s Metadata settings

Sample payload:

{
"deviceName": "Sensor A1",
"deviceType": "thermostat",
"temperature": 23.5,
"humidity": 60
}
  1. Go to Integrations center ⇾ Data converters.
  2. Click + Add data converter ⇾ Create new converter.

In the Add data converter dialog:

  1. Converter type — leave Uplink (selected by default).
  2. Integration type — in the search field, enter Pub/Sub and select Pub/Sub from the list.
  3. Name — enter a converter name, for example Pub/Sub Uplink Converter.
  4. Main decoding configuration — a code editor with the function signature function decoder(payload, metadata) {. Paste the decoder function shown below. By default the editor opens in TBEL; use the TBEL / JS toggle (upper right) to switch languages.
  5. Click Add.

The decoder function used in this tutorial:

// Decode an uplink message from a buffer
// payload - array of bytes
// metadata - key/value object
/** Decoder **/
// decode payload to JSON
var data = decodeToJson(payload);
// --- Device name and type ---
var deviceName = data.deviceName != null ? data.deviceName : 'Unknown Device';
var deviceType = data.deviceType != null ? data.deviceType : 'default';
// var customerName = 'Customer C';
// var groupName = 'thermostat devices';

To adapt this converter to your device:

  • Different device name / type fields — replace data.deviceName and data.deviceType with the field names your device actually sends (e.g. data.id, data.sensorType).
  • Timestamp — if your payload includes a Unix millisecond timestamp, name the field ts; for an ISO 8601 string, name it timestamp. If neither is present, the converter falls back to the server receive time automatically.
  • Telemetry fields — all JSON fields not listed in excludeFromTelemetryList are mapped to telemetry automatically via toFlatMap. To exclude additional non-telemetry fields (e.g. a firmware version you want as an attribute), add their names to the exclude list.
  • Static attributes — replace model and serialNumber in the attributes object with the actual device properties from your payload or hard-coded values; remove the entries if not needed.
  • Assets instead of devices — uncomment assetName / assetType and comment out deviceName / deviceType to provision assets instead of devices.
  • Customer or group assignment — uncomment customerName or groupName and set the appropriate values to assign the device to a customer or device group.
  1. Go to Integrations center ⇾ Integrations and click + Add integration.
  2. Basic settings:
    • Set Integration type to Pub/Sub.
    • Enter a Name for the integration, or keep the default Pub/Sub integration.
    • Leave Enable integration and Allow create devices or assets on so a device is created automatically on first message.
    • Click Next.
  3. Uplink data converter:
    • Click Select existing and choose the Pub/Sub Uplink Converter created above, or click Create new to define the decoder inline.
    • Click Next.
  4. Connection:
    • Project ID — your GCP project ID (e.g. tb-pubsub-integration).
    • Subscription ID — the pull subscription name (e.g. tb-uplink-sub).
    • Service account key — upload the JSON key file via Browse file, or click Use Secret storage to reference a stored secret.
    See connection settings for a full description of each parameter.
  5. Click Add.
Project ID

Your Google Cloud project ID. Found in the project selector dropdown in the GCP Console or on the Project info card on the dashboard.

Subscription ID

The pull subscription name on the uplink topic (e.g. tb-uplink-sub). The subscription must exist in GCP before the integration is created. ThingsBoard reads messages through this subscription — it is already bound to the topic in GCP.

Service Account Key

The GCP service account key JSON file. The account must have at least roles/pubsub.subscriber on the uplink subscription. Upload the file using Browse file, or store it in ThingsBoard Secret storage and reference it via Use Secret storage.

Execute Remotely

When enabled, ThingsBoard generates an Integration key and Integration secret that allow the integration to run as a separate process outside the ThingsBoard cluster — useful when access to GCP is restricted to a specific network. See Remote Integration.

Advanced Settings
ParameterDescription
DescriptionOptional text description for the integration.
MetadataKey-value pairs injected into every uplink message as integrationMetadata in the converter script.
Section titled “Publish a Test Message to the Uplink Topic”

Publish a test message to the uplink topic using the gcloud CLI or the GCP Console.

  1. Go to Pub/Sub ⇾ Topics and open the tb-uplink topic.
  2. On the Messages tab, click Publish message under Step 1.
  3. Paste the JSON payload into the Message body field and click Publish.
  4. A Message published confirmation appears once the message is delivered.
{
"deviceName":"Sensor A1",
"deviceType":"thermostat",
"temperature":23.5,
"humidity":60
}

Go to Integrations center ⇾ Integrations, open the Pub/Sub integration, and check the Events tab. One Uplink event appears with status OK. Click in the Message column to inspect the raw payload pulled from the subscription.

Go to Integrations center ⇾ Data converters, click the uplink converter, and open its Events tab. Click in the respective column to inspect each field:

  • In — the raw payload passed to the converter.
  • Out — the decoded result: deviceName, deviceType, attributes, and telemetry (temperature, humidity).
  • MetadataintegrationName and pubSubMsgId (the unique message ID assigned by GCP Pub/Sub).

Go to Entities ⇾ Devices. The device Sensor A1 is automatically provisioned on the first message. Open it and check the Latest telemetry tab — temperature and humidity should reflect the published values.

The Pub/Sub integration is uplink-only — it has no downlink converter. To publish messages from ThingsBoard back to a GCP Pub/Sub topic — for example, sending a command to a device or triggering a Cloud Function — add the GCP Pub/Sub rule node directly to a Rule Chain. It publishes the message payload it receives as-is, independent of the uplink integration.

In the GCP Console, create a separate topic for outgoing messages (e.g. tb-downlink) — do not reuse the uplink topic.

  1. Go to Pub/Sub ⇾ Topics and click Create topic.
  2. Enter a Topic ID (e.g. tb-downlink). Keep Add a default subscription checked — it gives you a subscription (e.g. tb-downlink-sub) you can pull from later to verify published messages.
  3. Scroll down and click Create.
  4. GCP creates the topic and its subscription together, listed in the topic’s Subscriptions tab.

Grant Publish Access to the Service Account

Section titled “Grant Publish Access to the Service Account”

The service account created earlier (see Create a Service Account and Download the Key) only has the Pub/Sub Subscriber role, which lets it pull messages — it cannot publish. Grant it Pub/Sub Publisher on the new topic, or the rule node fails with a PERMISSION_DENIED error.

  1. Open the tb-downlink topic and go to its Permissions tab, then click Add principal.
  2. Under New principals, enter the service account’s email (e.g. tb-pubsub@tb-pubsub-integration.iam.gserviceaccount.com).
  3. Under Assign roles, select Pub/Sub Publisher, then click Save.
  4. The topic’s Permissions tab now lists Pub/Sub Publisher alongside the existing Pub/Sub Subscriber role.
  1. Go to Rule chains and open the Root Rule Chain.
  2. In the node palette on the left, search for gcp pubsub (under External) and drag it onto the canvas.
  3. In the Add rule node dialog, enter a Name (e.g. PubSub Downlink), the GCP project ID (e.g. tb-pubsub-integration), and the Topic name (e.g. tb-downlink).
  4. Upload the GCP service account key file — you can reuse the same key from the uplink service account now that it has the Publisher role.
  5. Optionally add Message attributes (Name/Value pairs using ${metadataKey} for a value from message metadata, or $[messageKey] for a value from the message body) to pass extra context like deviceName as a Pub/Sub attribute rather than in the payload.
  6. Click Add, then connect the node’s input to the Message Type Switch node’s Attributes Updated relation (or another relation for whatever message type should trigger a downlink).
  7. Click Apply changes.

Trigger the rule node by adding an attribute to device Sensor A1:

  1. Go to Entities ⇾ Devices and open Sensor A1.
  2. Open the Attributes tab and click +.
  3. Enter key powerState, keep type String, enter value on, and click Add.

Verify Rule Node Events

Adding the attribute generates an Attributes Updated message that reaches the gcp pubsub node. Open the node’s Events tab — a debug event with Relation Type: Success confirms the publish succeeded. Click in the Data column to see the payload sent, and in the Metadata column to see the messageId GCP assigned to the message.

Verify Delivery in Google Cloud Pub/Sub

Pub/Sub has no “sent messages” log on the topic itself — read the message back through the subscription created earlier:

  1. Go to Pub/Sub ⇾ Subscriptions and open tb-downlink-sub.
  2. Open the Messages tab and click Pull.
  3. The published message appears with its Message body — confirming it reached GCP.
SymptomLikely causeFix
Integration status is not ActiveWrong Project ID or Subscription IDConfirm both exactly match the values in the GCP Console.
Integration status is not ActiveInvalid or incomplete Service account keyVerify the JSON is complete and belongs to an active service account.
Integration status is not ActiveMissing subscriber roleGrant roles/pubsub.subscriber to the service account on the subscription.
Integration status is not ActivePub/Sub API disabledEnable the Cloud Pub/Sub API in your GCP project.
Messages not appearing in ThingsBoardProcessing errorsCheck the integration Events tab for errors.
Messages not appearing in ThingsBoardSubscription unbound or deactivatedConfirm the pull subscription is attached to the correct topic and no expiration policy has deactivated it.
Messages received but not decodedConverter does not match the payloadInspect Converter Events → In and adjust the decoder to the actual message format.
Device not createdAllow create devices or assets disabledOpen the integration, click the edit icon, and enable Allow create devices or assets.
Downlink not published to GCPWrong rule-node config or missing publisher roleConfirm the GCP Pub/Sub rule node has the correct Project ID, Topic name, and a key with roles/pubsub.publisher.
  • Integrations Overview — how ThingsBoard connects to external platforms and how uplink/downlink flow works
  • Uplink Data Converter — full decoder function reference: input parameters, output fields, and scripting patterns
  • GCP Pub/Sub Rule Node — publish messages from the Rule Engine back to a GCP Pub/Sub topic (downlink)
  • Remote Integration — run the integration outside the ThingsBoard server to reach a broker on a private network
  • TBEL scripting reference — built-in functions and operators for writing converter scripts
  • Rule Engine — how rule chains route messages to nodes like the GCP Pub/Sub Rule Node