Hey there, might be an issue on my end, but I have no idea what I am doing wrong here.
To be clear, I am able to connect successfuly to the EventSub websocket and receive the events, the problem only arises when trying to reconnect after receiving the “session_reconnect” message.
More precisely when establishing the connection using the provided url, awating the welcome message, then closing the old connection.
Following the documentation here: https://dev.twitch.tv/docs/eventsub/handling-websocket-events/#reconnect-message , my untrained eye tells me my implementation meets the requirements.
Implementation (relevant code):
import WebSocket from "ws";
enum MESSAGE_TYPE {
WELCOME = "session_welcome",
RECONNECT = "session_reconnect",
// ...
}
class TwitchEventSub {
// ...
handleEventSubMessage(data: WebSocket.RawData, _is_bin: any) {
let obj = JSON.parse(data.toString());
const msg_type = obj["metadata"]["message_type"];
switch (msg_type) {
case MESSAGE_TYPE.WELCOME: {
if (this.old_websocket !== undefined) {
IO.quietPrint("Closing old EventSub WebSocket.");
this.old_websocket.removeAllListeners();
this.old_websocket.close();
this.old_websocket = undefined;
}
IO.debug(
"Successfully connected to Twitch EventSub WebSocket."
);
this.websocket_session_id = obj["payload"]["session"]["id"];
return;
}
case MESSAGE_TYPE.RECONNECT: {
IO.warn(
"Received reconnection message from Twich EventSub WebSocket."
);
const reconnect_url = obj["payload"]["reconnect_url"];
this.old_websocket = this.websocket!;
this.reconnectTwitchEventSubWebsocket(reconnect_url);
return;
}
// ...
}
}
reconnectTwitchEventSubWebsocket(new_url?: string) {
this.createTwitchEventSubWebsocket(new_url);
}
createTwitchEventSubWebsocket(url?: string): Promise<void> {
return new Promise((resolve) => {
this.websocket = new WebSocket(
url === undefined ? "wss://eventsub.wss.twitch.tv/ws" : url
);
let ws = this.websocket;
ws.on("open", () => {
ws.on("message", (data, _) =>
this.handleEventSubMessage(data, _)
);
// ...
ws.on("close", (code: number, reason: Buffer) => {
IO.print(
`Closed Twitch Events WebSocket with message: ${code} ${reason.toString(
"utf8"
)}`
);
});
resolve();
});
});
}
// ...
}
I can provide more of the code if it doesn’t show enough.
Here is what I see with the logging:
[19:17:28] Received reconnection message from Twich EventSub WebSocket.
[19:17:28] Closing old EventSub WebSocket.
[19:17:28] Successfully connected to Twitch EventSub WebSocket.
[19:17:38] Closed Twitch Events WebSocket with message: 4003 connection unused
As said before I am most likely missing something obvious or the timing is wrong somewhere.
Please help!