Skip to Content

General WebSocket - Connect

This guide explains how to connect to the General WebSocket using the endpoint and an authorization header.

USE CASE

Open one connection when your app starts to receive live updates on your orders, trades and positions.

Details

To connect to the General WebSocket, the below two input params are mandate. Based on the programming language chosen, respective socket connection libraries can be used. For reference, the sample code for socket connection is written in Python.
Mandatory connection parameters

Pass the websocket endpoint and an authorization header in <appId:accessToken> format.

Websocket endpoint: wss://socket.fyers.in/trade/v3

Header

Format: <appId:accessToken>

Sample header format: 7ABXUX38S-100:eyJ0eXAi**********qiTnzd2lGwS17s

The connection uses parameters and callback functions on_message, on_error, on_close, and on_open, which are required in the Python socket connection library and would change as per the other programming library used. A sample callback function is shared below.

Note

Handle accordingly in your programming language.

Connection response

On success: returns the socket object.

On failure: possible error status code 403.

Error: Handshake status 403 Forbidden

Handshake status 403 Forbidden -+-+- {'date': 'Tue, 19 Dec 2023 04:46:45 GMT', 'content-length': '0', 'connection': 'keep-alive', 'cf-cache-status': 'DYNAMIC', 'set-cookie': '__cf_bm=BOE16LGB7NHpNqW0AJOuFN1rcL3Q9TgnhmtpBfb3.Wk-1702961205-1- AfmECmK9cbVA2XGvkpx+jFuXyRsJET/ZOQYmw3LyZJ68pYLZTgtpbalvNs09ECZZ4GpPiogeYGhhFo+3PCp20nE=; path=/; expires=Tue, 19-Dec-23 05:16:45 GMT; domain=.fyers.in; HttpOnly; Secure', 'server': 'cloudflare', 'cf-ray': '837d00eec8ed17b6-MAA'} -+-+- b''

  • Reason: this error will come when your accessToken is wrong.
  • How to solve: provide the correct accessToken. AccessToken format: <appID:accesstoken>.

Error: Handshake status 404 Not Found

Handshake status 404 Not Found -+-+- {'date': 'Tue, 19 Dec 2023 10:04:35 GMT', 'content-type': 'text/plain; charset=utf-8', 'content-length': '0', 'connection': 'keep-alive', 'cf-cache-status': 'DYNAMIC', 'set-cookie': '__cf_bm=I5oN6zdeKjfGsicqFiXZ57J5SX2IjsFDspaLIEGKPDE-1702980275-1-Ae+ldjrb3WfSuM7yNOzo3ykOIBQ1m+50QqcIqU26A+wqPIHhIEIGSy9kT2OG3OWNI0hwcmh7U+PnJ/aWWhz6fOA=; path=/; expires=Tue, 19-Dec-23 10:34:35 GMT; domain=.fyers.in; HttpOnly; Secure', 'server': 'cloudflare', 'cf-ray': '837ed28169c817ae-MAA'} -+-+- b''

  • Reason: socket connection URL would be wrong.
  • How to solve: provide a valid URL.

Sample callback function: for more reference, see the on_open callback in the Python request sample.

Good to know
  • This is the General WebSocket endpoint for account events. Live market data uses the separate Data Socket.
  • Connecting alone does not deliver any data. Subscribe to the actions you want once the connection is open.
  • The Python SDK sends the access token in the authorization header. With your own client, send <appId:accessToken> in the header.
  • With order_ws.FyersOrderSocket, the callbacks are on_connect, on_close, on_error, on_orders, on_trades, on_positions and on_general. Subscribe inside on_connect, then call keep_running().
  • The SDK starts its ping loop as soon as the connection opens. With your own client, send ping yourself (see Ping method).
  • Create only one WebSocket connection instance at a time, as advised in the WebSocket guidance.
Last updated on