""" AUTHOR: Khushal P Soonderji DATE: monday, 23rd Dec., 2024 OBJECTIVE: To provide a SocketIO app for socket-base communication with the front-end. REFERENCES: 01. YouTube: https://www.youtube.com/watch?v=H1eLJMC5oTg&t=3s DOWNLOADS: N/A """ # ***************************************************************************************************************** # ***** **** # *** IMPORT *** # ***** **** # ***************************************************************************************************************** # To make sibling directories accessible for imports: import sys sys.path.append(".") sys.path.append("..") # System-level activities: import io import os # my utils: from utils_v2.string import json # To work with SocketIO: import socketio from aiohttp import web # For asynchronous activities: import asyncio # for debugging: from icecream import IceCreamDebugger # To work with date and time: import time import datetime # To work with Zerodha's Kite platform: from kiteconnect import KiteConnect, KiteTicker # ***************************************************************************************************************** # ***** **** # *** MACROS / ONE-TIME INIT *** # ***** **** # ***************************************************************************************************************** INSTRUMENT_MAP = { 256265: "NIFTY 50", 260617: "NIFTY 100", 259849: "NIFTY IT", 341249: "HDFCBANK", 738561: "RELIANCE", 408065: "INFY", 2953217: "TCS", 356865: "HINDUNILVR", 1270529: "ICICIBANK", 492033: "KOTAKBANK", 110630919: "GOLD25JAN75800CE", 110050823: "SILVER25FEB76000CE", 10670594: "NIFTY24DEC23650PE", 17167874: "BANKNIFTY24DEC45000PE", } INSTRUMENT_TOKENS = list(INSTRUMENT_MAP.keys()) # ***************************************************************************************************************** # ***** **** # *** VARIABLES *** # ***** **** # ***************************************************************************************************************** # The SocketIo server: sio = socketio.AsyncServer(cors_allowed_origins = "*") app = web.Application() sio.attach(app) # Debugging: printer = IceCreamDebugger(prefix = "SocketIO | ", includeContext = True) # ***************************************************************************************************************** # ***** **** # *** FUNCTIONS *** # ***** **** # ***************************************************************************************************************** @sio.event async def connect(sid, environ): printer(sid) # --------------------------------------------------------------------------------------------------------------------- @sio.event async def disconnect(sid): printer(sid) # ***************************************************************************************************************** # ***** **** # *** MAIN PROGRAM *** # ***** **** # ***************************************************************************************************************** if __name__ == "__main__": # Import the needed events: from wsocket.finstitutions.trading import live_feed async def main(): # register all the events: sio.on("subscribe", live_feed.subscribe) # Run the web server: runner = web.AppRunner(app) await runner.setup() site = web.TCPSite(runner, "0.0.0.0", 5214) await site.start() # Keep the server running: while True: await asyncio.sleep(3_600) asyncio.run(main())