WebSocket Connection

Catalyst Data Streams enables clients to receive real-time data by establishing a WebSocket connection to a channel. The client listens for published messages and processes them as they are received.

To subscribe to a channel, you must first generate a token pair, then create a WebSocket client with the token credentials, and finally set event handlers to handle incoming messages.

The datastreams reference used in the code below is the component instance created earlier.

Establish WebSocket Connection

You must initially generate a token pair using the getTokenPair() method as shown below. Then, create a WebSocket client by passing the token credentials to the DataStreamsWebSocket constructor.

Note: * CRICKET_CHANNEL_ID - Replace with your actual Channel ID which you can get from Dashboard → General Details section. * userId - A unique identifier used to generate WebSocket credentials for that subscriber. * subscribe(“0”) - This subscribes the client to live data.
copy
//Generate token pair
const response = await datastreams.getTokenPair(CRICKET_CHANNEL_ID, {
  userId: "userId"
});

//Create WebSocket client const socket = new DataStreamsWebSocket({ url: response.url, zuid: response[“wss-id”], key: response.key, enableLogging: true });

//Handling WebSocket events socket.on(“open”, () => { console.log(“WebSocket connection opened”); socket.subscribe(“0”); //Subscribe to live data });

socket.on(“message”, (data) => { console.log(“WebSocket message received:”, data); socket.sendAck(); //Acknowledge to receive next message });

socket.on(“error”, (error) => { console.error(“WebSocket error:”, error); });

socket.on(“close”, () => { console.log(“WebSocket connection closed”); });

Last Updated 2026-10-05 20:43:57 +0530 IST