What response on disconnect twitch IRC

Hi all I am just cleaning up a few bits with the bot I created. I was just wondering how to check for disconnect so I can reconnect with backoff? I just dont know what response to check for? Any pointers here would be great.

Thanks in advance

Your socket actually gets disconnected…

You may get sent a RECONNECT message which is a instruction that Twitch Chat is restarting and you need to reconnect.

You can also use a Ping/Pong to test the connection and/or see if the connection has died due to lack of PINGs from the server, or replies to PINGs you send yourself, or both together.

1 Like

Perfect that works so if I PING the server how long to wait for PONG?

<1 second usually.

It’ll only be slower if your connection is really bad, and if thats the case you should reconnect anyway to find a better route

1 Like

Okay thanks Barry that should do nicely

One quick one Barry. I’m using Python, so if the connection to the socket is disconnected can I simply catch the exception for trying to read or write to it and then simply set up a new socket with the same variables and exponentially back off until I either connect or wait for some amount of time…ooooorrrrr PING/PONG?


1 Like

Hi, am I allowed to pop my code up to get feedback and advice on my connection method? I am new to programming twitch bots and although I pretty much live in the docs at the moment it would be nice to make sure I am proceeding correctly from a learned eye.


Hey turds, what lib are you using to connect to the IRC? I’m using aiohttp and I check for aiohttp.WSMsgType.CLOSED to reconnect, when it comes to reconnecting Twitch expects you to wait exponentially for the reconnect to go through so I am using await asyncio.sleep(2 ** self.reconnections) to await before reconnecting.

Also not sure if this helps but the PONG reply to twitch PING should be sent as TEXT - at least it failed whenever I tried to use the aiohttp.websocket.pong.

Hope this helps

Hi Fabio,

I am importing socket and connecting. It all seems to be working at the moment but I am going to do some unit testing next week. I havent used aiohttp and will have a look later so thank you for that. I am sending the PING PONG as bytes array with str.encode and all seems fine…It will prob crash somewhere for me at some point :smiley: …At the moment the function recursively backsoff from the server if something goes wrong…Again seems fine at the moment but more testing.

Thanks -Turds

Ah awesome, glad you have everything working :smiley: I’m using aiohttp because the project that I’m adding the Twitch connector is an async library :smiley: if you need any help I’d be more than happy to give a hand if you have your code on github :+1:

1 Like