Skip to content

Quickstart: receive device state

Data Streaming delivers what your devices report to Kafka topics only your project can read.

In the project, open Cloud Services, choose Add service, pick Data Streaming Services and save. Open the service to see its connection: the broker address, the two topics and the consumer group prefix.

In the service, under Credentials, choose Create credential. Copy the password now: it is shown once and cannot be shown again. A project can have two credentials at a time, so you can move your consumers to a new one before revoking the old one.

With Node.js and kafkajs:

import { Kafka } from 'kafkajs'
const kafka = new Kafka({
clientId: 'my-service',
brokers: ['<broker>:9092'],
sasl: { mechanism: 'scram-sha-512', username: '<username>', password: process.env.KAFKA_PASSWORD },
})
// The group must start with the prefix shown in the dashboard.
const consumer = kafka.consumer({ groupId: 'rogo-<PARTNERID>-my-service' })
await consumer.connect()
await consumer.subscribe({ topics: ['<state topic>', '<event topic>'], fromBeginning: true })
await consumer.run({
eachMessage: async ({ topic, message }) => {
console.log(topic, message.key?.toString(), message.value.toString())
},
})

Switch a device on and off in your app: a state message arrives within a second or two.