Why did my websocket connection randomly close?

Got this message: Websocket Error without any other provided context

[WinError 10054] An existing connection was forcibly closed by the remote host on_close

any idea what’s going on? this is the first time that this has happened to me running an open websocket connection.


10054 is a WebSocket protocol error that basically says for some reason the host closed the connection. This could be for a variety of reasons, so it would help if I could get some more information. Are there any additional logs you can show me, a timestamp you can give me, or (best of all) can you reproduce the error?

still having this issue – im wondering if its because I have an open websocket connection and am receiving notifications through the tradovate app on my smartphone at the same time, even though I don’t actually have the app open?

@Alexander I’ve noticed that my connection remains stable and open for many hours, after which it suddenly gets closed by the host. could getting pulse notifications at the same time as having an open websocket cause this? Reminder that I do not have the app open on my phone when this occurs

I get similar problem yesterday,I check the log ,It may be has 3 session in the same time ,It will be kick off the first session,today I run my program hold day with one client open.
and I catch the error of socket error,It will be reconnect after a error of socket,remember you need sub the quote or chat after reconnect marker data socket

I discovered that it is not the problem with having Pulse open or on my phone. @Alexander can you let me know the situations in which the host would terminate a connection like this?

There are any number of reasons that a connection could be dropped. But no matter what, your code will ultimately have to deal with the connection being dropped as this scenario will eventually present itself with a program that is intended to run indefinitely.

I’d suggest running code when you get the close frame (a ‘c’ frame), or simply when you encounter a 10054. You would just re-instantiate your WebSocket and re-perform the auth procedure, then set up your previous listeners. If you do that you should be able to just reconnect when the connection gets dropped.

To clarify the nature of 10054 errors - these are just generic errors for websocket servers that says ‘Hey, we dropped your connection, sorry.’ You can see this error in all sorts of websocket implementations from windows applications to video games. It doesn’t mean anything necessarily other than a long running connection was dropped (usually because of inactivity or connectivity issues, plus browsers throttle long running tasks like heartbeats which could lead to this error).

Unless you’re having issues with some part of the program before the connection was dropped, simply adding a routine to catch the dropped connection and re-instantiate it should fix this issue.

I’ve noticed that it normally happens in the situation of the websocket being open for a while, and then me executing a trade via a POST request simulateously. could this be the reason why this is occurring: having a websocket open and simultaneously sending a post request?

I don’t think that they should be related, but if you request a new access token or something in the course of making the request, it would void your old access token (it will begin a new user session for you, like when you log in to the Trader app). If you had your websocket authorized using the old token, it would be booted off as an older session when that request goes out. Make sure to store your access token so you don’t have to keep requesting it for HTTP requests, or send your HTTP requests through the WebSocket instead.