kite connect websocket flask code example

Example: how to import websocket from kiteconnect

from kiteconnect import WebSocket

# Initialise.
kws = WebSocket("your_api_key", "your_public_token", "logged_in_user_id")

# Callback for tick reception.
def on_tick(tick, ws):
	print tick

# Callback for successful connection.
def on_connect(ws):
	# Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
	ws.subscribe([738561, 5633])

	# Set RELIANCE to tick in `full` mode.
	ws.set_mode(ws.MODE_FULL, [738561])

# Assign the callbacks.
kws.on_tick = on_tick
kws.on_connect = on_connect

# Infinite loop on the main thread. Nothing after this will run.
# You have to use the pre-defined callbacks to manage subscriptions.
kws.connect()