Websocket

The embedded Websocket server provides push notifications for applications which require real-time feedback from devices like light switches and motion sensors.

since version 2.04.40

Websocket port

By default the Websocket server is started on a proxy friendly port which, depending on the system, is either 443, 8088, 8088, 20877 or any other unused random port. The port can also be specified on commandline with the --ws-port=<port> switch.

The port in use can be queried via configuration REST-API endpoint.

GET /api/<apikey>/config

Response

{
    "websocketport": 8088,
    ...
}

Possible errors

403 Forbidden

503 Service Unavailable

Open connection

How to establish a connection to an websocket server depends on the underlying programming environment.

Javascript example

The following example demonstrates how to establish a connection with Javascript in a browser or NodeJS implementation.

var host = '192.168.1.202';
var port = 8088;

var ws = new WebSocket('ws://' + host + ':' + port);

ws.onmessage = function(msg) {
    console.log( JSON.parse(msg.data) );
}

Message format

Messages received over a Websocket connection contain data in JSON format.

Button event example

{
    "t": "event",
    "e": "changed",
    "r": "sensors",
    "id": "12",
    "state": { "buttonevent": 2002 }
}

When a button of a switch is pressed the event will fire immediatly. For switches which support hold and long press/release multiply events will fire, like 1001 (hold) and 1003 (release).

Sensor presence signal example

{
    "t": "event",
    "e": "changed",
    "r": "sensors",
    "id": "7",
    "state": { "presence": true }
}

Message fields

Name Type Description
t String

Message type

  • event — the message holds an event
e String

Type of a event message

  • changed — resource attributes have changed
  • added — resource was deleted
  • deleted — resource was added
r String

Type of the related resource

  • sensors — the id field refers to a sensor resource
  • lights — the id field refers to a light resource
  • groups — the id field refers to a group resource
id String Identifier of the related resource
state Object State object for lights, groups and sensors with changed attributes
config Object Config object for lights and sensors with changed attributes