4

I've been hunting down a bug in Chatception for quite a while now: sometimes messages would simply stop appearing, and I would have to refresh to make Chatception start working again.

Yesterday I finally got tired of it and decided to start logging every single WebSocket event (message, error, close) to see if any of that data could help in tracking down the bug.

Today the bug happened, so I immediately opened up my console and found:

close event, gasp!

Why are my chat WebSockets occasionally being closed (without a reason, even)? I haven't yet found a way to reliably reproduce this, but it happens at least a few times per day if I just leave a chat tab open in the background.

Here's how I'm opening the WebSocket, in case that's relevant (apologies for the terrible code):

function getSock() {
    return new WebSocket(JSON.parse($.ajax({
            type: 'POST',
            url: 'http://' + location.host + '/ws-auth',
            data: {roomid: CHAT.CURRENT_ROOM_ID,
            fkey: fkey().fkey
        }, async: false}).responseText)['url'] +
        '?l=' +
        JSON.parse($.ajax({
            type: 'POST',
            url: 'http://' + location.host + '/chats/' + CHAT.CURRENT_ROOM_ID + '/events',
            data: {fkey: fkey().fkey},
            async: false
        }).responseText)['time']);
}
7
  • So you expect the websocket to stay alive forever? Is that a reasonable assumption? Are you sending hartbeats as well? – rene Nov 15 '15 at 15:36
  • @rene I was under the impression that it should stay alive forever... at least, I can keep a chat window open for an indeterminate amount of time and still see new messages in realtime (perhaps the chat code itself is reopening the sockets on close, though). As far as I'm aware, chat doesn't require you to send heartbeats (this was "discovered" through the empirical evidence of watching Chrome DevTools for a while :P). – Doorknob Nov 15 '15 at 15:38
  • It will close. I recall seeing recover code in the CHAT.js, when it falls back to XHR posts on an setInterval to get events until a working Websocket is connected and operational again. – rene Nov 15 '15 at 15:46
  • @rene Oh, huh. It appears you're right, after digging through the ChatExchange code (used by Smokey). Mind posting an answer to that effect, so I can accept it? – Doorknob Nov 15 '15 at 15:51
  • For the use which this appears to be written for, in-browser notification of chat events, you should probably see: How do I get notified of events in SE chat?, which describes how to add a callback to SE chat from within a SE chat page which gets called every time the page receives an event. – Makyen Jun 9 at 16:16
  • For the general issue which you've described: wanting a WebSocket which remains active, I've had good success using ReconnectingWebSocket, which is released under an MIT license. I have no affiliation with the project, other than that I've used it in multiple scripts/projects to solve the general issue mentioned here. That particular one hasn't been maintained in a while, but is still working. It appears there are some forks which have been updated/maintained. There are also other such packages. – Makyen Jun 9 at 16:24
  • @Makyen that's very useful. I've also learned how to connect the wbsocket btw. THanks – Vikas Jun 9 at 17:07
8

Websockets are not meant to or expected to stay-alive forever. Several reasons can occur that the sockets die, either invoked by the client, the server or somewhere in the middle.

In chat.js there is code to reopen a closed connection in the onclose event but also code to fallback to polling, basically an AJAX XHR post to /events. It looks like it keeps doing that until it re-establish a working websocket connection.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .