We need more details about the issue youāre encountering. A connection should not be rejected unless there are numerous active connections.
Could you provide information on how many active connections you had when the rejection occurred and how many scrips youāve subscribed to on each connection?
This will help us troubleshoot the issue more efficiently.
@shanmu
I am getting error as soon as connection is opened even when no scrip is subscribed. Although my main application is in Java, I have also tried running python example for Upstox python github repo which is also not working.
The connection rejection issue is not related to the programming language but is intentionally blocked by Upstox due to exceeding the maximum allowed number of active connections.
Please check if your application running on any server has active socket sessions. Additionally, if youāre using a loop mechanism to establish connections without a maximum limit, this issue is expected.
Kindly verify any active connections and terminate them before initiating new ones.
@shanmu I am aware that this issue is due to exceeding the maximum allowed number of active connections. But I use the python sample just to confirm that it is not due to my code making more than maximum connections allowed.
Currently there is no application running which make connection to Upstox and if it is still showing on your side then it must be showing some older connection which are no longer active, so if itās possible can you please terminate those connections from your side
I too am getting issues with websocket connection . It just hangs up after saying connection established with below code. Nothing happens. Earlier on restarting it use to stream but not today :
async def fetch_market_data(scrips, token):
"""Fetch market data using WebSocket and print it."""
max_reconnect_delay = 60 # Maximum reconnection delay in seconds
reconnect_delay = 5 # Initial reconnection delay in seconds
print('Starting Websocket')
# Create default SSL context
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
# Configure OAuth2 access token for authorization
configuration = upstox_client.Configuration()
api_version = '2.0'
configuration.access_token = token
while True:
try:
# Get market data feed authorization
response = get_market_data_feed_authorize(
api_version, configuration)
# Connect to the WebSocket with SSL context
async with websockets.connect(response.data.authorized_redirect_uri, ssl=ssl_context) as websocket:
print('Connection established')
await asyncio.sleep(1) # Wait for 1 second
# Data to be sent over the WebSocket
data = {
"guid": "someguid",
"method": "sub",
"data": {
"mode": "full",
"instrumentKeys": scrips
}
}
# Convert data to binary and send over WebSocket
binary_data = json.dumps(data).encode('utf-8')
await websocket.send(binary_data)
# Continuously receive and decode data from WebSocket
while True:
message = await websocket.recv()
decoded_data = decode_protobuf(message)
# Convert the decoded data to a dictionary
data_dict = MessageToDict(decoded_data)
print(data_dict,'\n')
process_ticks_Upstox.delay(data_dict)
except websockets.exceptions.ConnectionClosedError as e:
logger.error(f"WebSocket connection closed unexpectedly: {e}")
logger.info("Reconnecting in 5 seconds...")
except Exception as e:
logger.error(f"An error occurred: {e}")
logger.info("Reconnecting in 5 seconds...")
if dt.datetime.now().time() > dt.datetime.strptime('15:30', '%H:%M').time():
logger.info("Market is closed. Exiting...")
break
# Wait before reconnecting
await asyncio.sleep(reconnect_delay) # Adjust the delay as needed
# Implement exponential backoff
reconnect_delay = min(2 * reconnect_delay, max_reconnect_delay)