Skip to main content

Messages

Publish Message

Publish a topic, payload pair to the gateway.

DeviceScript will stringify and upload your object as a JSON payload. The gateway add this messages into an Azure Event Hub, additonal sinks can be configured online.

import { publishMessage } from "@devicescript/cloud"

await publishMessage("greetings", { msg: "hello" })

In the device gateway, this topic will be expanded to devs/{deviceid}/from/{topic}. If you want to override this behavior, use /{topic} and the leading will be removed the MQTT topic to be {topic}.

tip

Keep those field names very short as the payload needs to be small (< 236 bytes).

In the Visual Studio Code extension, you can upload a JSON message from the device context menu.

Subscribe Message

Subscribes to able to receive messages from the gateway.

import { subscribeMessage } from "@devicescript/cloud"

subscribeMessage("greetings", async msg => {
console.log(msg)
})

Visual Studio Code extension

In the DeviceScript Gateway extension, you can connect to the device and see the messages in the output window.