Dshiz
Dshiz

Reputation: 3331

Stop socket.io from complaining in the browser when the server is down

I'm experimenting with socket.io.

Scenario:

I have several tabs open that are initially connected to the server. I stop the server because I want to do some work on the code. The browsers start logging:

polling-xhr.js:206 GET http://localhost:3000/socket.io/?EIO=4&transport=polling&t=NhdlRB- net::ERR_CONNECTION_REFUSED

Is there any way to suppress this error? It's using a lot of system resources in each browser tab when it happens, especially if the browser is running in that state for a long time.

Upvotes: 1

Views: 609

Answers (2)

AEQ
AEQ

Reputation: 1429

I revisited this topic in the hope that it has changed in 2024, tried listening to every event in the API as a Manager or usual client and still nothing, then I came across this again, sigh:

Please note that error logs such as:

- net::ERR_INTERNET_DISCONNECTED
- net::ERR_CONNECTION_REFUSED
- WebSocket is already in CLOSING or CLOSED state
- Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at xxx. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
- The connection to xxx was interrupted while the page was loading

are not emitted by the Socket.IO library but by the browser itself, and are therefore out of our control.

Docs Ref: https://socket.io/docs/v3/logging-and-debugging/#error-logs-in-the-browser-console

Github Issues Ref: https://github.com/socketio/socket.io-client/issues/1178

To conserve browser resources: Since we can't get rid of the error, we can try to reduce the amount of errors generated. You could handle the reconnection logic yourself by setting the reconnection option reconnection: false, then try to connect 3 times after the first fail for instance instead of infinity (default setting).

Upvotes: 0

Nikita Mazur
Nikita Mazur

Reputation: 1785

You can socket.disconnect(); on error, otherwise it will try to reconnect.

Upvotes: 1

Related Questions