ALLTERCO/shelly-script-examples

Outbound websocket example: publisher/subscriber to provide a working starting point?

Opened this issue · 1 comments

https://shelly-api-docs.shelly.cloud/gen2/ComponentsAndServices/Ws shows it's possible to create outbound websockets from scripts. Cool.

But all it provides in terms of showing how to communicate is this:

The Outbound Websocket component uses ws as RPC namespace and provides the methods:

There's only one very vague example in this repo:

function emitOverWs(measurement) {
  Shelly.emitEvent(CONFIG.event_name, measurement);
}

function emitOverWs(measurement) {

Can I just start listening for incoming data using Shelly.addEventHandler()?

Why not create a typical "publisher/subscriber" example, combined with a HTTPServer? The publisher sends an event over the websocket to the subscriber, and the subscriber sets the received value in KVS. That way, there's no dependency on external components and it'd provide a great starting point to connect other devices.

Thanks for considering! 😊

Mircho commented

Maybe the example is misleading a bit. In a Shelly the transport is not related to the notification mechanism. One may have MQTT and oubound WebSocket channels open and publish notifications over them. Or receive RPC requests. One can communicate directly with a script by calling the Script.Eval method if he knows which script id the script has.

To directly communicate with the device you can add an http endpoint handler in a script or subscribe for an MQTT topic: