WSstart_websocket(...)
Code Examples
How the official example scripts wire up order-update and quote callbacks against the WebSocket feed — the real callback pattern, not the raw frame format.
How the official example scripts wire up order-update and quote callbacks against the WebSocket feed — the real callback pattern, not the raw frame format.
Overview
The raw
t/tk/tfframe format on Subscribe to Market Feed and Order Update Feed is what travels over the wire. The Python SDK hides that behind three callbacks passed to a singlestart_websocketcall, as used in bothexample_orders.pyandexample_market.py.The three callbacks
{"t":"ck","s":"OK"}on WebSocket Overviewt:'tk'andt:'tf'on Subscribe to Market Feedt:'om'on Order Update Feedsubscribe_callbackhands you each message exactly as received — if you need a complete, always-current quote object, mergemessageinto your own dict keyed by token; the example script'sprint(message)is not doing that for you.Why subscribe happens inside open_callback
Both example scripts call
api.subscribe(...)from insideopen_callback— never immediately after callingstart_websocket.start_websocketreturns before the connection handshake necessarily completes; subscribing beforeopen_callbackfires risks sending a subscribe frame down a socket that isn't confirmed open yet. Thesocket_openedglobal flag exists purely so the rest of the program (e.g. a menu option to start the socket) can check connection state without re-enteringstart_websocket:Subscribing to more than one token
Both scripts show the single-token call live and the multi-token call commented out directly beneath it:
Passing a list subscribes to every token in one call — this is the wrapper-level equivalent of the
#-delimitedkfield documented on Subscribe to Market Feed ("NSE|22#BSE|522032"). Prefer batching into onesubscribecall over looping single-token calls, for the same reasons documented there.Best practices
event_handler_quote_updateandevent_handler_order_updatefast — per WebSocket Overview, slow handlers block the read loop for every other message on the same socket. The example'sprint()calls are fine for a demo; route to a queue in production.api.subscribe(...)call from inside a freshopen_callbackafter a reconnect — subscriptions are not remembered across a dropped socket by the server, and the wrapper doesn't replay them for you either.api.subscribe(...)beforesocket_openedisTrue— mirror the example's pattern of putting the subscribe calls insideopen_callback, not right after invokingstart_websocket.Notes
Field comments in
event_handler_quote_update(e,tk,lp,pc,v,o,h,l,c,ap) match the raw tick fields on Subscribe to Market Feed — cross-reference that page for the full field table, since the example script only comments the handful it expects to see most often.