Skip to main content

Keeping Connection Alive

To keep the WebSocket connection alive over times of inactivity it is recommended to send PING messages to server within the Keep-Alive time span. Currently this Keep-Alive time span is 5 minutes. Thus to keep the connection alive it is recommended to send PING message to server within every 4 minutes or so.

Following code example shows how you can send PING message to server.

let wire = 1; // Use same wire ID as was used in the related OPEN message for the targeted network

if (webSocket.readyState === webSocket.OPEN) {
const PING = JSON.stringify({
"method": "ping",
"wire": wire
});

webSocket.send(decodeURIComponent(escape(PING)));
}

When server receives PING message it will respond with PONG message. Response to the PONG message looks like this.

{"response":"pong"}

This PONG message does not have any other function than to notify client that server has received PING message. Regardless of if PING messages are sent or not it is highly recommended that one implements WebSocket disconnected functionality to keep the connection open every time WebSocket disconnects for some reason.