Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch ERR_CONNECTION_REFUSED thrown by browser if server goes down?

I think this question is pretty straightforward, but I haven't found an answer yet in the official Socket.io docs.

I have my Socket.io server and client communicating successfully, and when I shut down the server, the client will attempt to reconnect with the server unsuccessfully. This is shown in the Chrome developer's console here:

enter image description here

Is there any way to detect this event in the client-side Javscript and react to it some way? I'm thinking I would display a dialog on the page, such as "Sorry, server is under heavy load", etc.

like image 567
Matt Vukas Avatar asked Jul 10 '14 18:07

Matt Vukas


People also ask

What does it mean when it says refused to connect?

Most of the times, “err connection refused” error occurs when Chrome is unable to load the website. Check whether the same website is opening on another server or in mobile. Check whether other websites are opening in Chrome browser or not.


1 Answers

The correct way to handle this, as of Socket 1.x, is to use the Manager object for a given socket. It throws a connect_error event that can be handled as shown:

socket.io.on('connect_error', function(err) {   // handle server error here   console.log('Error connecting to server'); }); 
like image 148
Matt Vukas Avatar answered Sep 18 '22 21:09

Matt Vukas