EN

WebSocket

Example on GitHub (opens in a new tab)

Model

🚩

Problem: Suppose you have a sensor that transmits room illumination data and lamp on state via WebSocket protocol. In the data packet, the information comes in JSON format

{
  "luminosity": 134,
  "lamp": true
}

The sensor also responds to the commands to turn the lamp on and off, if in the body of the data packet comes

{
  "device": "lamp",
  "data": 1
}

or

{
  "device": "lamp",
  "data": 0
}

To connect the sensor to the platform, create a WebSocket model for it.

There are several parameters in the template model. Create the necessary parameters for illumination and lamp state, and delete all unnecessary parameters.

There are also two commands in the template model, edit them:

  1. change the identifier to a more appropriate one;

  2. change the name with which the command is displayed in the interface;

  3. specify the text of the commands according to what the device expects to receive.

View a preview of the object with the model you created by clicking the Show preview button. preview**.

Model to import. (opens in a new tab)

Object

Create an object for your sensor. Select the model you just created. Then enter the identifier that is used in the URL when connecting. Specify a name for the object. To further secure access to the object, under ws_auth section, specify a login and password.

Click the Save button.

For more information on creating an object

Object to import (opens in a new tab)

Connection

To connect the device to the platform, specify the address

  • ws://ws.dev.rightech.io - for HTTP connection;
  • wss://ws.dev.dev.rightech.io - for HTTPS connection.

The full URL should look like this

  • ws://ws.dev.rightech.io/api/v1/objects/ws_lamp/connect

If you have specified a login and password, be sure to include them in the appropriate header

Authorization: Basic bXlfbG9naW46bXlfcGFzc3dvcmQ=

otherwise you will not be able to connect to the object.

Make sure that data from the device is sent and displayed in the interface.

Send commands and verify that the connected device responds to them.