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:
- 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()
- 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()
- 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?