Websocket Errors while Streaming

I have been using Upstox WebsocketV3 for some time for running a strategy and only in a past few days i have been facing more errors with stream disconnection which causes the bot to completely stop… Is this an issue from my side and if any comments on how i can improve it so that i face less disconnection…

I subscribe to one instrument at the beginning but can subscribe using the subscribe function to few more instruments during execution, the subscription goes fine but I have seen the no of disconnects rising in the last few days.

Also got this error when trying to get the option chain for a certain instrument:

2025-06-20 10:04:53,891 WARNING Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='api.upstox.com', port=443): Read timed out. (read timeout=None)")': /v2/option/contract?instrument_key=BSE_INDEX%7CSENSEX

Error in Websocket:

2025-06-20 09:55:57 ERROR [swing_bot.py | on_error]: Stream error received: Connection timed out
2025-06-20 09:55:57 INFO [swing_bot.py | on_close]: Connection closed. Code: None, Reason: None
2025-06-20 09:55:57 INFO [swing_bot.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 1/10
2025-06-20 09:56:00 ERROR [swing_bot.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Fri, 20 Jun 2025 04:25:59 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-06-20 09:56:02 INFO [swing_bot.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 2/10
2025-06-20 09:56:06 ERROR [swing_bot.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Fri, 20 Jun 2025 04:26:05 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-06-20 09:56:08 INFO [swing_bot.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 3/10
2025-06-20 09:56:11 ERROR [swing_bot.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Fri, 20 Jun 2025 04:26:10 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-06-20 09:56:13 INFO [swing_bot.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 4/10
2025-06-20 09:56:19 ERROR [swing_bot.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Fri, 20 Jun 2025 04:26:16 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-06-20 09:56:21 INFO [swing_bot.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 5/10
2025-06-20 09:56:32 ERROR [swing_bot.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Fri, 20 Jun 2025 04:26:31 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-06-20 09:56:34 INFO [swing_bot.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 6/10
2025-06-20 09:56:36 ERROR [swing_bot.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Fri, 20 Jun 2025 04:26:35 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-06-20 09:56:38 INFO [swing_bot.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 7/10
2025-06-20 09:56:43 ERROR [swing_bot.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Fri, 20 Jun 2025 04:26:41 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-06-20 09:56:45 INFO [swing_bot.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 8/10
2025-06-20 09:56:49 ERROR [swing_bot.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Fri, 20 Jun 2025 04:26:47 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-06-20 09:56:51 INFO [swing_bot.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 9/10
2025-06-20 09:56:53 ERROR [swing_bot.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Fri, 20 Jun 2025 04:26:51 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-06-20 09:56:55 INFO [swing_bot.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 10/10
2025-06-20 09:56:58 ERROR [swing_bot.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Fri, 20 Jun 2025 04:26:56 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-06-20 09:56:58 CRITICAL [swing_bot.py | on_auto_reconnect_stopped]: Auto-reconnect stopped after retry limit reached with retryCount of 10 exhausted.

The same thing is happening with me today, this is from a local run, after i switched my network…i tried to run my program multiple times, each time it got me this error.

2025-06-23 09:21:36 INFO [swing_bot.py | start]: Starting streamer...
2025-06-23 09:21:36 INFO [swing_bot.py | connect]: Connecting to Upstox WebSocket...
2025-06-23 09:21:36 ERROR [swing_bot.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Mon, 23 Jun 2025 03:51:35 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-06-23 09:21:36 INFO [swing_bot.py | on_close]: Connection closed. Code: None, Reason: None
2025-06-23 09:21:36 INFO [swing_bot.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 1/10
2025-06-23 09:21:36 ERROR [swing_bot.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Mon, 23 Jun 2025 03:51:35 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-06-23 09:21:38 INFO [swing_bot.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 2/10
2025-06-23 09:21:38 ERROR [swing_bot.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Mon, 23 Jun 2025 03:51:38 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None

This Happened Again right now…even after restarting the applicaiton..it is still giving this error and this time on a AWS EC2 Instance with a proper internet connection.

I Have tried with a new access_token also but the erros doesnt seem to go away.. what can i do in this situation.
This is the initial error recieved on my first Websocket Fail.

2025-07-16 09:29:00 ERROR [swing_bot.py | on_error]: Stream error received: Connection to remote host was lost.
2025-07-16 09:29:00 INFO [swing_bot.py | on_close]: Connection closed. Code: None, Reason: None
2025-07-16 09:29:00 INFO [swing_bot.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 1/10
2025-07-16 09:29:00 ERROR [swing_bot.py | on_error]: Stream error received: Handshake status 502 Bad Gateway -+-+- {'server': 'awselb/2.0', 'date': 'Wed, 16 Jul 2025 03:59:00 GMT', 'content-type': 'text/html', 'content-length': '122', 'connection': 'keep-alive'} -+-+- b'<html>\r\n<head><title>502 Bad Gateway</title></head>\r\n<body>\r\n<center><h1>502 Bad Gateway</h1></center>\r\n</body>\r\n</html>\r\n'
2025-07-16 09:29:00 ERROR [second_data_recorder.py | on_error]: Stream error received: Connection to remote host was lost.
2025-07-16 09:29:00 INFO [second_data_recorder.py | on_close]: Connection closed. Code: None, Reason: None
2025-07-16 09:29:00 INFO [second_data_recorder.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 1/10
2025-07-16 09:29:00 ERROR [second_data_recorder.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Wed, 16 Jul 2025 03:59:00 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-07-16 09:29:02 INFO [second_data_recorder.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 2/10
2025-07-16 09:29:02 ERROR [second_data_recorder.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Wed, 16 Jul 2025 03:59:02 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-07-16 09:29:04 INFO [second_data_recorder.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 3/10
2025-07-16 09:29:05 ERROR [second_data_recorder.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Wed, 16 Jul 2025 03:59:05 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-07-16 09:29:07 INFO [second_data_recorder.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 4/10
2025-07-16 09:29:07 ERROR [second_data_recorder.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Wed, 16 Jul 2025 03:59:07 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-07-16 09:29:09 INFO [second_data_recorder.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 5/10
2025-07-16 09:29:09 ERROR [second_data_recorder.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Wed, 16 Jul 2025 03:59:09 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-07-16 09:29:11 INFO [second_data_recorder.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 6/10
2025-07-16 09:29:11 ERROR [second_data_recorder.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Wed, 16 Jul 2025 03:59:11 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-07-16 09:29:13 INFO [second_data_recorder.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 7/10
2025-07-16 09:29:13 ERROR [second_data_recorder.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Wed, 16 Jul 2025 03:59:13 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-07-16 09:29:15 INFO [second_data_recorder.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 8/10
2025-07-16 09:29:15 ERROR [second_data_recorder.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Wed, 16 Jul 2025 03:59:15 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-07-16 09:29:17 INFO [second_data_recorder.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 9/10
2025-07-16 09:29:17 ERROR [second_data_recorder.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Wed, 16 Jul 2025 03:59:17 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-07-16 09:29:19 INFO [second_data_recorder.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 10/10
2025-07-16 09:29:19 ERROR [second_data_recorder.py | on_error]: Stream error received: Handshake status 403 Forbidden -+-+- {'date': 'Wed, 16 Jul 2025 03:59:19 GMT', 'transfer-encoding': 'chunked', 'connection': 'keep-alive'} -+-+- None
2025-07-16 09:29:19 CRITICAL [second_data_recorder.py | on_auto_reconnect_stopped]: Auto-reconnect stopped after retry limit reached with retryCount of 10 exhausted.

Now I’m also seeing the same problem. @Pradeep_Jaiswar @Ketan

It used to work till 9:30, now I’m getting websocket connection to …failed

Hi @Raghav_35590666 , @LOKESH_VEN_1850972
Can you check the number of connections you have created for v3 websocket, seems like you are getting forbidden message because of connection more than allowes number of websocket connections.

Here are the limits Market Data Feed V3 | Upstox Developer API

Thanks!

Hi @Ketan, I am running two instances only, not more than that as the prescribed limit for my account. I think when a connection fail occurs that connection remains counted towards my limit and hence my reconnect attempt gives a forbidden error as it again is counted towards the given threshold. Can you check on this. Or is there any way i can purge my connections in a way that this doesn’t happen. Can we get some feature or functionality that lets us check our usage for at least the WebSocket or the api should purge the connection from server side when errors occur and if the rate limit can be reset properly. Any Clarity or suggestions on this would be very insightful.

My Application was running fine with two connections, the first error i got was a ‘502 Bad Gateway’, the reconnect attempt then started causing forbidden errors and hence after that both my instances started getting forbidden Errors. I have also re generated a new access token but the result remained the same, could it be as i said that after reconnect attempts fail, the connection is still counted towards the rate limit.

Thank You

Only one connection. I tried connecting with new access token.

disconnected

An error occurred: Error: Unexpected server response: 403

If there are more connections, it should through relavant error, right?

Is there any way to get the number of connections?

Hi @Raghav_35590666,
Currently, we do not have the functionality to purge connections from the user’s side or provide the number of active connections a user is connected to.

Ideally, when a disconnection occurs, the number of live connections should decrease on our servers. If that’s not happening, we will need to investigate further.

@LOKESH_VEN_1850972 @Raghav_35590666 can you share your 6 digit user id so we can investigate further.

Thanks!

@Ketan My User ID - 3PCRDD

AA0550 - MY USER id

same problem usedId - 3PCS32

@Ketan I am also getting same 403 forbidden error since last couple of days. And today also, I got the error. Can u help fixing the issue.

why its taking eternal time? bhau.

@Raghav_35590666
Two connections at 8:39 and 8:50 were successfully established. After that, all connection requests were rejected. We did not observe any disconnection on the server side. Please recheck your implementation of the connection process, as we received around 100 connection requests, all of which were rejected due to a connection limit breach.

Similar is the case for other users.

Thanks!

“websockets.exceptions.InvalidStatus: server rejected WebSocket connection: HTTP 403” my error.

My implementation is similiar to the examples and i use the various on_error and reconnecting function callbacks. I did log a disconnection and dont know how the server didnt log this, i am sharing a snapshot of the logs and the function level which logged this

2025-07-16 09:29:00,867 ERROR [swing_bot.py | on_error]: Stream error received: Connection to remote host was lost.
2025-07-16 09:29:00,868 INFO [swing_bot.py | on_close]: Connection closed. Code: None, Reason: None
2025-07-16 09:29:00,868 INFO [swing_bot.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 1/10
2025-07-16 09:29:00,915 ERROR [swing_bot.py | on_error]: Stream error received: Handshake status 502 Bad Gateway -+-+- {'server': 'awselb/2.0', 'date': 'Wed, 16 Jul 2025 03:59:00 GMT', 'content-type': 'text/html', 'content-length': '122', 'connection': 'keep-alive'} -+-+- b'<html>\r\n<head><title>502 Bad Gateway</title></head>\r\n<body>\r\n<center><h1>502 Bad Gateway</h1></center>\r\n</body>\r\n</html>\r\n'
2025-07-16 09:29:02,916 INFO [swing_bot.py | on_reconnecting]: Reconnecting... Auto reconnect attempt 2/10

My on_close function has been triggered by the streamer but as you said this disconnection hasnt been logged by the server.

As i said the connection is closed, i have also stopped my application, still any further attempts to connect has led to forbidden errors as also you pointed out.

@Ketan my case is same as @Raghav_35590666 . I have closed the connection and also have terminated app. But still when I try to run my script, I get forbidden error. It means that server is not logging the connection close and have still kept it open, even though the connection is closed from my end.

@Ketan, what’s the solution? I am using max 2 web socket connections. Can we please get expedited response. It is not a pet project, real money is involved here.