Use these for normal device communication. The platform routes by target ID.
// Send a payload to a target (e.g. a device)ws.send(targetId, payload);// Receive messages; returns an unsubscribe functionconst off = ws.onMessage((message) => { console.log('received', message);});// later: off();
ws.send / ws.onMessage are the right tools for talking to devices — they go
through the platform’s realtime routing, so you only need the target ID.
For advanced cases, a script can open a connection to your own external
WebSocket server (not the platform’s). Pass the URL you control:
await ws.connect(url, (data) => { /* messages from your server */ });ws.sendTo(url, data); // send to that connectionws.isConnected(url); // booleanws.disconnect(url); // close it
connect / sendTo / disconnect are for external servers you operate.
Don’t use them to reach platform internals — use ws.send / ws.onMessage for
device communication.