Websocket randomly closes

Im having an issue where any websocket to any glitch project will disconnect after between 10 and 300 seconds. I have tried it a bunch of times and it seems to be completely random. I have tried it on other peoples websocket projects and the same issue happens. I have tried different websocket libraries (uws, ws, and socket.io). When I run this code anywhere else (heroku, AWS, ovh) everything works and the socket doesn’t disconnect.

For example, try this in the browser console:

var myws = [ ];
for(var i=0;i<10;i++){
//create a websocket to a random glitch project (works with any glitch websocket project)
var x = new WebSocket(“wss://websocket-chat-example.glitch.me/”);
//warn user when its closed
x.onclose = function(){
console.warn(“connection closed”);
}
setInterval(function(){
// keep the websocket awake
if(x.readyState===1){
x.send("");
}
},500);
myws.push(x);
}

now hit enter and wait a while ( 5 min about). Now look at the console and at least some of the connections will have closed (the console will warn that the connection has closed). If given enough time all the websockets will disconnect, but at different times.

please help me fix this problem :smiley:

1 Like

Hey @Centipede5,

I see that you’re sending a “keep awake” message using setInterval, but I don’t see an interval time in the code you pasted. setInterval needs to know how often to send the message or it won’t do anything.

setInterval(function(){
  // keep the websocket awake
  if(x.readyState===1){
    x.send("");
  }
}, 10000); // send the keep-awake every 10 seconds

Hope that helps!

With no interval time parameter, it treats it the same as interval time of 0. And this means it sends as soon as the current code has finished, with other browser constraints, i.e. as soon as it can, so its spammy to send messages using a zero interval timer.

@potch would a lot of messages inside one socket connection (i.e. one request) be triggering a Glitch limit? Maybe it would drive up the server CPU usage a lot …

This makes sense if there are no other connections opened (requests) to the project, socket activity doesn’t defeat the 5-minute inactivity shutdown, which goes by requests.

i think it defaults to 0 so this shouldnt be a problem. I also tried it with different delays

Thanks for your response!

I have a few questions tho:

would a lot of messages inside one socket connection (i.e. one request) be triggering a Glitch limit? Maybe it would drive up the server CPU usage a lot …

it produces the same result with a 500 millisecond or 1 second delay

This makes sense if there are no other connections opened (requests) to the project, socket activity doesn’t defeat the 5-minute inactivity shutdown, which goes by requests.

I tested this and i believe it does keep the project awake. I also tried with a simple keep awake script using fetch() and the problem persists.

The main thing that these theories dont explain is why all the websockets disconnect at different times (if it was a server issue then they would all disconnect at the same time).

I’ve been encountering the same issue, even with the (official?) parcel bundler starter project: https://glitch.com/edit/#!/parcel

This has been somewhat bothersome, as it means that my front-end will suddenly stop live-updating after a minute or two, so I’m having to refresh manually, sort of defeating the purpose of HMR in the first place.

Hoping to see this resolved soon, as Glitch is otherwise the ideal platform, and I’ve had a wonderful experience using it so far!

1 Like