Skip to main content

OPEN Message

When WebSocket connection has opened you must send valid OPEN message to server to initialize the connection and to keep it open. Otherwise server will automatically close the connection after few seconds. Use "onopen" event listener of WebSocket to do this. For OPEN message you will need to have valid session and network as parameters as shown in the following code example.

webSocket.onopen = function () {
let session_id = "SESSION-ID"; // Received from API create session request
let network_id = "NETWORK-ID"; // Received from API create session request
let reference = "REFERENCE-ID"; // Reference handle created by client to link messages to relevant callbacks
let wire = 1; // Connection ID, incremental value to identify messages of network/connection
let type = 1; // Client type, use value 1 (FRONTEND)

const OPEN = JSON.stringify({
"method": "open",
"id": network_id,
"session": session_id,
"ref": reference,
"wire": wire,
"type": type
});

this.send(decodeURIComponent(escape(OPEN)));
};

Expected way to use OPEN message is to create one wire per one network. Thus event messages from each network can be mapped to specific wire ID. Wire ID is expected to be integer value starting from 1. Wire ID is unique to the opened WebSocket connection and messages from specific wire can be "paused".

In addition use of reference ID is recommended. When given by client in the OPEN message it can be then used to map initial WebSocket response message from server to relevant callback on client side. Note that reference ID (if given) is added only to initial response message from server per related OPEN message.

note

If client tries to open wire to the same network from the same connection more than once then server will block this and return tooManyWires response with wireStatus: "Only one wire allowed per network!". Different WebSocket connections can point to the same network up to maximum of 20 active wire connections per network. If client or clients try to open more than 20 WebSocket connections to the same network then server will block this and return tooManyWires response with wireStatus: "Too many wires opened!".

On OPEN success​

When OPEN message is successful server will send initial openWireSucceed response message along with possible reference ID (if given). In addition if there is active (authorized) gateway related to the network defined in the OPEN message then server will also send peerChanged event message. If gateway is active and any devices are connected then server will send also current state of connected devices as event message. After this new messages will be sent only when some event occurs within the network(s) related to given wire. For more information see receiving messages.

If there is no active (authorized) gateway added related to the network(s) of the OPENed wire then server will only send openWireSucceed message and nothing else until the gateway is active again. Also note that if server does not receive any traffic from the Opened WebSocket (and wire) then after 5 minutes of inactivity it will automatically close the WebSocket connection. See Keeping connection alive for more info.

On OPEN failure​

When OPEN message fails server will send one message with information about the failure along with possible reference ID (if given). For more information see OPEN message response wire status types.