WebSocket Overview
This guide explains the two FYERS WebSockets, when to use each one and how a WebSocket connection differs from REST.
What are WebSockets?
A WebSocket keeps one persistent, two-way connection open so FYERS can push updates to you as they happen, instead of you polling REST endpoints.
FYERS exposes two WebSocket types: the Data Socket (which includes the Tick-by-Tick (TBT) socket) and the General WebSocket (which carries order, trade, position, price alert and EDIS updates). Pick the socket that matches the data you need.
Types of WebSockets
| Socket | What it streams |
|---|---|
| Data Socket | Live market data: symbol (quote) updates, index updates, five-level market depth and lite mode. |
| Tick-by-Tick (TBT) Socket | Part of the Data Socket. Streams 50-level market depth for NFO and NSE equity symbols, with Protobuf responses and channels you can pause and resume. |
| General WebSocket | Real-time updates for your account’s orders, trades and positions, plus price alert triggers and EDIS updates, over a single connection instead of polling the Order Book. |
Best practices
Subscription Limit
Data Socket symbol limits are per user, across all connections: Standard allows 100 symbols and Prime allows 5,000. The Tick-by-Tick (TBT) socket has its own limits: Standard allows 1 symbol and Prime allows 15 (up to 3 connections with 5 symbols each). See Plans & rate limits.
Connection Instance
For the Data Socket and General WebSocket, create only one WebSocket instance at a time. The Tick-by-Tick (TBT) Socket has its own connection limits, listed under Rate limits.
Dedicated Thread
The WebSocket runs on a dedicated thread, independent of your main application thread, so primary work is not blocked by socket I/O.
Updating Subscribed Symbols
To update symbols in an already subscribed socket, it is recommended to unsubscribe from the existing list of symbols and then resubscribe with the updated list.
Customizable Callbacks
Tailor your application’s behavior using the callback functions provided by the WebSocket, to define specific actions for events like data updates or error occurrences:
on_connect on_message on_error on_close.Auto-Reconnect
Enable automatic reconnection on disconnect by setting
reconnect=True during WebSocket initialization, so your application can recover without manual intervention. You can set a maximum reconnection count of up to 50.Reconnect Retry
Set a dynamic retry count with
reconnect_retry=10 (max 50). In Node use fyersdata.autoreconnect(trycount).Logging to File
Set
write_to_file=True to save received data to a log file. write_to_file works only when callback functions are not used.Disable Logging (Node)
Pass a logging flag in the Node constructor to disable logs:
new FyersSocket("token", "logpath", true)Last updated on