[Solved] Connection with Twitch chat via Websocket gets unstable within 10 minutes

I’m making a general purpose streaming assist tool, and I’m trying to make it connect to TMI via Websocket. But I stop receiving messages after about 10 minutes (shortly after the second Ping message).

Here’s an example output: http://www.fluffyquack.com/temp/websocket/websocket2.png (there’s supposed to be more messages after the second ping, but the program never receives anything).

I made two small sample Websocket clients based on two different libraries.

Client based on easywclient (https://github.com/dhbaird/easywsclient)
Binary: http://www.fluffyquack.com/temp/websocket/websocket.exe
Source: http://www.fluffyquack.com/temp/websocket/websocket.rar (https://pastebin.com/0Pd1pjQ0)

Client based on websocketpp (https://github.com/zaphoyd/websocketpp)
Binary: http://www.fluffyquack.com/temp/websocket/websocket2.exe
Source: http://www.fluffyquack.com/temp/websocket/websocket2.rar (https://pastebin.com/isgJV479)

The behaviour in both clients is the same (I stop receiving messages after about 10 minutes). I sent a friend of mine one of the clients, and it worked fine for him on his connection (had it running for over 20 minutes and he received every message). I tried Anhkbot with my connection (I’m pretty sure Ankhbot also connects to chat via Websocket), and that seems to be working fine on my end. I don’t know what Ankhbot does differently to keep receiving messages (does it silently keep re-connecting?).

Any idea what could be wrong?

Superficially, make sure you are terminating all commands sent to the server with \r\n. Though not always required there have been issues in the past when it is omitted. You can also try proactively sending PINGs to TMI and see if that helps you keep the connection open.

Added the line break symbols and added a ping message sent every few minutes, but the behaviour is the same.

It’s always after about 10 minutes the connection starts to be unstable.

I’ve had the client running for a long time now, and it does actually eventually receive messages. After running it for about 15 minutes, it finally receives a ton of chat messages all at once (which were meant to be received several minutes ago). So the connection is still alive, just insanely unstable.

I’m wondering if I’m getting such bad routing to the Twitch chat server that I’m riddled with packet loss, but that doesn’t explain why I can read chat just fine directly on the Twitch page, or why Ankhbot is able to receive messages just fine.

Tried connecting to wss://irc-ws.chat.twitch.tv:443 but still same result.

I tried using this for testing and it seems to works fine (spent around 30 minutes without anything going wrong): https://chrome.google.com/webstore/detail/simple-websocket-client/pfdhoblngboilpfeibdedpjgfnlcodoo?hl=en

So yeah, I’m absolutely clueless what is going wrong. Some websocket clients are able to maintain a stable connection (like the google chrome extension I linked, and Ankhbot), and a friend of mine is able to maintain a stable connection using my code, but I’m having zero luck with my code. Unless someone has anything else I can try, I’ll give up on the websocket protocol and see how it goes connecting to Twitch chat using the IRC protocol.

We use WebSockets in the bot that we work on and we had a similar issue with certain VPS providers. I may have missed/glossed over everything you have tried, but, folks were losing their connections with our bot between 5 - 15 minutes out depending upon how busy chat was. The fix was we to provide an option to send the PING to Twitch more often. It turned out that some VPS providers, like Azure, were shutting down WebSocket connections within 3 minutes if not kept alive. I am not saying this is your issue, but it was something that killed us only with WebSockets.


I think that’s the same problem I got! I had my pings set to happen rather irregularly, and I’ve been testing with the chat being almost empty (just manually writing a test message now and then). I changed the pings to be sent every 30 seconds, and now the connection seems to be stable. I did several long tests and nothing went wrong. I’ll do even more tests to be completely sure it’s fine, but it’s looking like it’s fixed. Thanks for the tip!

1 Like

You bet! That issue I mention above took me a few days to figure out between Google and getting the user to tell me exactly what environment they were in. I stumbled upon some documentation that pointed me in the right direction. Very glad to hear that it appears to be working!

Side note: I believe that PINGs are counted in the maximum allowed messages within 30 seconds, so, keep that in mind if you are sending data to chat and need to watch for limits so you don’t get your chat ability banned for 30 minutes.


I had a user with the same issue, where the connection to chat would be cut after some time of inactivity, although not over websocket, but a raw TCP socket instead. I also solved it by sending a PING in a short interval, depending on the other activity on the connection.

My tool isn’t sending messages to Twitch chat, it’s only reading from it, so I won’t get close to the message limit. I previously had the ping set to be sent every 2 and a half minutes. I could try increasing it to 1 minute and see if that’s enough. But on the bright side, this fixed the issue entirely. I had a 4 hour test earlier, and the connection remained fine that entire time.

1 Like

Cool beans, I just didn’t want you accidentally tripping the limit and then wondering what went wrong!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.