Skip to main content

Device SDK

Runs on your device. Publish telemetry, respond to commands, manage configuration — with automatic reconnection and offline buffering.

JavaScriptPython

Install

npm install @relay-x/device-sdk

Initialize

Create a RelayDevice instance with your API key and secret from console.relay-x.io.

const { RelayDevice } = require("@relay-x/device-sdk");

const device = new RelayDevice({
api_key: "your_api_key",
secret: "your_secret",
mode: "production",
});

The mode parameter separates your data into isolated environments so test data never mixes with production:

  • "production" — Live environment for real device data
  • "test" — Isolated sandbox for development and testing

Connect

await device.connect();

connect() authenticates with RelayX, fetches your device schema, and initializes all modules. It returns true on success, false if already connected.

Modules

Quick Example

A complete example that connects a device, monitors the connection, and publishes temperature readings every 5 seconds.

const { RelayDevice } = require("@relay-x/device-sdk");

const device = new RelayDevice({
api_key: "your_api_key",
secret: "your_secret",
mode: "production",
});

// monitor connection status
device.connection.listeners((event) => {
console.log("Connection:", event.type);
});

async function main() {
await device.connect();

// publish telemetry every 5 seconds
setInterval(async () => {
const temperature = 20 + Math.random() * 10;
const sent = await device.telemetry.publish("temperature", temperature);
console.log(sent ? "Sent" : "Buffered (offline)");
}, 5000);
}

main();

Connection Lifecycle

The Device SDK handles reconnection automatically. If the connection drops, the SDK will:

  1. Emit a reconnecting event
  2. Keep retrying until a connection is re-established
  3. Resubscribe to all active listeners on reconnect
  4. Flush any messages that were buffered while offline
  5. Emit a reconnected event

Offline Buffering

When the device is disconnected, telemetry and event publishes are buffered in memory. Once the connection is restored, buffered messages are flushed in order. telemetry.publish() and event.send() return false when a message is buffered instead of sent.

RPC and config operations require an active connection and will throw a NotConnectedError if called while disconnected.

Disconnect

await device.disconnect();

Gracefully closes the connection, cleans up all consumers, and clears the offline buffer.