Websocket reconnection question

Main Question:
How do I have no downtime for the bot?

When a token expires and I have to refresh it, I seem to also need to close the websocket connection and reopen it with the new oAuth token.

Generally my flow is

  • connect websocket with oAuth token
  • wait for 401 / ping pong failure
  • close websocket connection
  • get new oAuth token using refresh_token
  • reopen websocket connection

Which, depending on internet connection, ranges from about 1-5 seconds of downtime, about every 4 hours.

This is rarely an issue, as 5 seconds of chat loss isn’t detrimental… but it does annoy me that I can’t figure out a solution to keep the connection open indefinitely.

… So yeah. am I missing something extremely simple to keep the connection open?

That shouldn’t be required.

A token to read/write to chat only needs to be valid at the moment you connect to chat. So you can keep going for days with the original token being dead.

You’ll just need the new token for API calls (such as banning users)

When you refresh the token you do not need to reconnect to chat

1 Like

I see. I must be doing something fundamentally wrong then.

my current scopes are like this

    private List<string> scopeList = new List<string>()
    {
        "chat:edit",
        "chat:read",
        "moderator:read:followers",
        "moderator:manage:shoutouts"
    };

Would I need to create 2 separate oAuth tokens? 1 with chat read/edit and another with all required scopes?
I’m unsure how I would do that.

Two tokens are not required.

When you token dies from the 4 hour rule (assuming code flow token) then your bot will remain connected reading/writing chat.

1 Like
Attempting to get bot name..
Connected as: Xikini
2023-11-22 11:37:47 AM Token validation successful.
2023-11-22 11:37:48 AM Connected to Twitch
2023-11-22 11:37:52 AM Hourly Validation succeeded.
2023-11-22 12:37:52 PM Hourly Validation succeeded.
2023-11-22 1:37:52 PM Hourly Validation succeeded.
2023-11-22 2:37:52 PM Hourly Validation succeeded.
2023-11-22 3:37:52 PM Hourly Validation Failed. Invalid OAuth token.
Attempting to get bot name..
Connected as: Xikini
2023-11-22 3:38:02 PM No PONG response from twitch. Resetting connection..
2023-11-22 3:38:02 PM Closing Twitch WebSocket connection..
2023-11-22 3:38:02 PM Connection closed
Attempting to get bot name..
Connected as: Xikini
2023-11-22 3:38:05 PM Token validation successful.
2023-11-22 3:38:05 PM Connected to Twitch
2023-11-22 4:37:56 PM Hourly Validation succeeded.

There was an error in my code. I was resetting the pingpong status when the hourly validation failed, which made me believe the websocket was failing.

Thank you. <3

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