Skip to main content


Manually manage the connection

The first time you access the realtime module, Agnost client library automatically establishes a connection to the realtime server and manages this connection for you. Most of the time you do not need to manually open or close a connection. However, if your use case requires closing the connection and then later opening it then you can use the open and close methods to manage your websocket connection.


If you manually disconnect the connection, the socket will not try to reconnect.

// Manually open the websocket connection;

// Manually close the websocket connection

Get websocket info

You can check whether the realtime connection has been establised successfully or not using the isConnected method and get the underlying websocket identifier using the getSocketId method.

// Check if the websocke is successfully connected to the realtime server.
// Returns true if the realtime socket is connected otherwise false
if (agnost.realtime.isConnected()) console.log("Connected");

// Get the unique identifier of the underlying websocket connection identifier
const socketId = agnost.realtime.getSocketId();

Listen to connection events

You can listen to key connection events through their respective listener functions. The following are the key connection events and their counterpart listener functions.

EventDescriptionListener function
ConnectFired upon successful connection to the realtime server.onConnect
DisconnectFired upon disconnection. The disconnection can happen due to forceful disconnection by the server, manual closing of the connection, or network problems etc.onDisconnect
Connect errorFired upon when an error occurs while establishing the websocket connection.onError
Reconnect attemptFired upon an attempt to reconnect.onReconnectAttempt
// Listen to Connect
agnost.realtime.onConnect(() => console.log("Connection established!"));

// Listen to Disconnect
agnost.realtime.onDisconnect((reason) => {
console.log("Disconnected due to", reason, agnost.realtime.getSocketId());

// Listen to Reconnect attempt
agnost.realtime.onReconnectAttempt((attemptNumber) =>
console.log("Reconnecting attempt#", attemptNumber);

// Listen to Connect error
agnost.realtime.onError((error) =>
console.log("Connection error", error,;