Multi-Program WebSocket Sharing: A Practical Guide

In Indian Market there are many brokers like ZerodhaUpstoxIIFL etc. Sometimes one broker stop streaming Live LTP data. So, When you have high AUM like near 1000cr like we do because of our Theta Program. You can not rely on a single broker. 

Here is a demonstration of a failsafe mechanism we deploy to switch over datastreams in case one broker gets disconnected from websocket. Let’s tell Zerodha’s program to get Live LTP as Program A and Let’s tell Upstox’s program to get Live LTP as Program B.

To implement a failover mechanism in the face of server failures or other issues where Program A and Program B switch to WebSocket 2 if WebSocket 1 fails, you can modify the code to handle this scenario. Here’s an example of how you can achieve it using Python:

WebSocket Server 1:

				
					import asyncio
import websockets
import threading

# Function to start WebSocket Server 1 in a separate thread
async def websocket_server_1():
    async with websockets.serve(handle_connection, "localhost", 8765):
        await asyncio.Future()

# Function to handle WebSocket connections
async def handle_connection(websocket, path):
    while True:
        message = await websocket.recv()
        print(f"Received on WebSocket 1: {message}")
        await websocket.send(f"Response from WebSocket 1: {message}")

# Start WebSocket Server 1 in a separate thread
server_thread_1 = threading.Thread(target=lambda: asyncio.run(websocket_server_1()))
server_thread_1.start()

				
			

WebSocket Server 2 (similar to WebSocket Server 1):

				
					import asyncio
import websockets
import threading

# Function to start WebSocket Server 2 in a separate thread
async def websocket_server_2():
    async with websockets.serve(handle_connection, "localhost", 8766):
        await asyncio.Future()

# Function to handle WebSocket connections
async def handle_connection(websocket, path):
    while True:
        message = await websocket.recv()
        print(f"Received on WebSocket 2: {message}")
        await websocket.send(f"Response from WebSocket 2: {message}")

# Start WebSocket Server 2 in a separate thread
server_thread_2 = threading.Thread(target=lambda: asyncio.run(websocket_server_2()))
server_thread_2.start()

# Keep WebSocket Server 2 running
server_thread_2.join()

				
			

Program A and Program B (with failover):

				
					import asyncio
import websockets

websocket_server_1_url = "ws://localhost:8765"
websocket_server_2_url = "ws://localhost:8766"  # WebSocket Server 2's URL

async def program_a():
    while True:
        try:
            async with websockets.connect(websocket_server_1_url) as websocket:
                await websocket.send("Hello from Program A")
                response = await websocket.recv()
                print(f"Program A received from WebSocket 1: {response}")
        except websockets.exceptions.ConnectionClosedError:
            print("WebSocket 1 connection closed. Switching to WebSocket 2.")
            websocket_server_1_url, websocket_server_2_url = websocket_server_2_url, websocket_server_1_url

if __name__ == "__main__":
    asyncio.run(program_a())

				
			

Program B should be modified similarly to Program A for failover.

In this setup, Program A and Program B will initially connect to WebSocket Server 1. If the connection to WebSocket Server 1 fails, they will switch to WebSocket Server 2. The failover is achieved by catching the websockets.exceptions.ConnectionClosedError exception and swapping the WebSocket server URLs.

Ensure that WebSocket Server 2 is running on a different port (e.g., port 8766) and that you’ve implemented it similarly to WebSocket Server 1.

Previously We used to deploy timeseries database InfluxDB which used to hold overlapped stream of live data but currently We have moved to servers in Mumbai in NSE Colation but the core principle of failover mechanism is same. There is an elaborate Retry Mechasim and Load Balancers architecture. 

Diving into more details will make us lose our edge. So, Lets conclude this as a basic overview.

Post a comment

Leave a Comment

Your email address will not be published. Required fields are marked *

×Close