Issue with Multiple WebSocket Connections

Hello Upstox Support,

I am experiencing an issue with establishing multiple WebSocket connections using the Upstox client library. According to the documentation, it states that up to three connections are allowed. However, when I run two separate WebSocket streams for “Nifty 50” and “Nifty Bank,” only one of them is actively receiving messages.

Here are the complete scripts I’m using:

  1. Nifty 50 Stream (nifty_50_stream.py):

python

Copy code

import upstox_client
import logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

def stream_nifty_50():
    configuration = upstox_client.Configuration()
    access_token = open('nifty_50_token.txt', 'r').read()
    configuration.access_token = access_token

    streamer = upstox_client.MarketDataStreamer(
        upstox_client.ApiClient(configuration), ["NSE_INDEX|Nifty 50"], "ltpc"
    )

    def on_message(message):
        logging.info(f"Nifty 50: {message}")

    streamer.on("message", on_message)

    try:
        streamer.connect()
    except Exception as e:
        logging.error(f"Error connecting Nifty 50: {e}")

if __name__ == "__main__":
    stream_nifty_50()
  1. Nifty Bank Stream (nifty_bank_stream.py):

python

Copy code

import upstox_client
import logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

def stream_nifty_bank():
    configuration = upstox_client.Configuration()
    access_token = open('nifty_bank_token.txt', 'r').read()
    configuration.access_token = access_token

    streamer = upstox_client.MarketDataStreamer(
        upstox_client.ApiClient(configuration), ["NSE_INDEX|Nifty Bank"], "ltpc"
    )

    def on_message(message):
        logging.info(f"Nifty Bank: {message}")

    streamer.on("message", on_message)

    try:
        streamer.connect()
    except Exception as e:
        logging.error(f"Error connecting Nifty Bank: {e}")

if __name__ == "__main__":
    stream_nifty_bank()
  1. Master Script (run_streams.py):

python

Copy code

import subprocess
import sys
import time

def main():
    python_executable = sys.executable

    # Launch the first process
    nifty_50_process = subprocess.Popen([python_executable, "nifty_50_stream.py"])
    time.sleep(2)  # Add delay before starting the second process

    # Launch the second process
    nifty_bank_process = subprocess.Popen([python_executable, "nifty_bank_stream.py"])

    # Wait for both processes to finish
    nifty_50_process.wait()
    nifty_bank_process.wait()

if __name__ == "__main__":
    main()

Despite running them as independent scripts, I only receive messages from one stream at a time. Could you please provide clarification on this limitation or any steps I can take to resolve the issue?

Also, the error am getting while trying to run parallely, websockets.legacy.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 403

@nirmal_kumar Could you please share your user ID so we can look into this issue?

Thanks!

Hi, I have sent you my id through private message Shanmu, thanks!

@nirmal_kumar please check if it’s working for you now.

Thanks!.

Thanks @shanmu, I think the issue seems to be resolved. I will revert if the same occurs again. thanks for the fix!