[Solved] Getting disconnected from PubSub after 7 minutes

Hey, I wrote a little app in C# to see moderator actions via PubSub. The problem I’m running into is that I’m getting disconnected after exactly 7 minutes every single time. In those 7 minutes, everything is working fine. I receive topics and pings are being sent and received. I don’t get any errors when the disconnect happens thought.
I’m using WebSocket4Net to connect to PubSub.

private WebSocket _ws;

private void StartWS()
{
	_ws = new WebSocket("wss://pubsub-edge.twitch.tv");
	_ws.AutoSendPingInterval = 120; // tried 60 too
	_ws.EnableAutoSendPing = true;
	_ws.Proxy = null;
	_ws.AllowUnstrustedCertificate = true;
	_ws.MessageReceived += WsOnMessageReceived;
	_ws.Opened += WsOnOpened;
	_ws.Closed += WsOnClosed;
	_ws.Error += WsOnError;

	_ws.Open();
}

private void StopWS()
{
	if (_ws == null || _ws.State != WebSocketState.Open) return;
	
	_ws.Close();
	_ws.Dispose();
}

private void WsOnOpened(object sender, EventArgs eventArgs)
{
	Console.WriteLine($"{DateTime.Now.ToLongTimeString()} - Connected");

	var t = new TopicRequest();
	t.Type = "LISTEN";
	// t.Data.Topics.Add($"chat_moderator_actions.{Info.ChannelID}"); // OBSOLETE and soon removed
	t.Data.Topics.Add($"chat_moderator_actions.{Info.UserID}.{Info.ChannelID}");
	t.Data.AuthToken = Info.OAuth;

	Send(t);
}

private void WsOnClosed(object sender, EventArgs eventArgs)
{
	Console.WriteLine($"{DateTime.Now.ToLongTimeString()} - Disconnected");
}

private void WsOnMessageReceived(object sender, MessageReceivedEventArgs messageReceivedEventArgs)
{
	try
	{
		var jObject = JObject.Parse(messageReceivedEventArgs.Message);
		var typeValue = jObject.SelectToken("type").Value<string>();
		if (typeValue.Equals("RESPONSE"))
		{
			var msg = JsonConvert.DeserializeObject<TopicResponse>(messageReceivedEventArgs.Message);
			// Do things
		}
		else if (typeValue.Equals("MESSAGE"))
		{
			var msg = JsonConvert.DeserializeObject<GenericMessage>(messageReceivedEventArgs.Message);
			// Do other things
		}
		else
		{
			Console.WriteLine($"Unknown type: {messageReceivedEventArgs.Message}");
		}
	}
	catch (Exception e)
	{
		Console.WriteLine("Error: {e.Message}");
	}
}

private void WsOnError(object sender, ErrorEventArgs e)
{
	Console.WriteLine($"Error: {e.Exception}");
}

private void Send(object obj)
{
	if (_ws == null || _ws.State != WebSocketState.Open) return;

	_ws.Send(JsonConvert.SerializeObject(obj));
}

If you need more information let me know!

Regards
Freddy

When you send a PING are you sending something like:

ws.send(JSON.stringify({“type”: “PING”}));

(Javascript)

Or are you just

ws.send(‘PING’);

hmm I’m not sure to be honest because pings are handled by WebSocket4Net. I have to look into that.

Edit: yeah, that seems to be the problem.
WebSocket4Net neither sends

PING

nor

{“type”: “PING”}

but the date of the last ping request which is kinda weird

There is the Websocket PING (part of the protocol), and there is Twitch’s PubSub PING. They are not related.

Twitch has decided to not use the built-in PING/PONG but requires applications to send it themselves as a normal message.

To fix disable _ws.EnableAutoSendPing = true; and implement your own periodic ping as specified by the docs: https://github.com/justintv/Twitch-API/tree/master/PubSub#connection-management

1 Like

ah thank you for pointing that out. I must have missed that part in the documentation.
I Implemented my owning ping function and there are no disconnects anymore!

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