--- title: "WebSocket Connection" url: https://docs.catalyst.zoho.com/en/sdk/nodejs/v2/cloud-scale/data-streams/websocket-connection/ description: "This page describes how to connect to a Data Streams channel over WebSocket and subscribe to real-time data in your Node.js application with sample code snippets." last_updated: 2026-10-05T15:13:57Z service: Cloud Scale type: general related_links: - name: "Data Streams" url: /en/cloud-scale/help/data-streams/introduction --- # WebSocket Connection [Catalyst Data Streams](https://docs.catalyst.zoho.com/en/cloud-scale/help/data-streams/introduction/) 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](https://docs.catalyst.zoho.com/en/sdk/nodejs/v2/cloud-scale/data-streams/get-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. ```javascript //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"); }); ```