socket.io-client: Socket.io client keeps disconnecting and reconnecting at a fixed interval because of ping timeout

Recently I’ve updated my socket.io server and client to 4.6.1 and ever since then my client keeps disconnectiong and reconnecting at a fixed interval. The disconnection reason on the client is ping timeout

It does not matter whether the tab is in the foreground or background. I cannot reproduce this issue on localhost.

I still have a client that uses 2.2.0 and it does not reconnect at all and seems to be working correctly.

Left is 2.2.0, right is 4.6.1. They both connect to the same server. Screenshot_601

And more importantly the 4.6.1 client receives the ping (2) and sends the pong (3) message to the server, but it does not seem to matter. image

Socket.IO server version: 4.6.1

Server

    this.io = new Server(httpServer, {
      allowEIO3: true,
      serveClient: false
    });

Socket.IO client version: 4.6.1

Client

    this.socket = io(url, {
      transports: ['websocket'],
      reconnection: true,
      reconnectionAttempts: 50,
      reconnectionDelay: 5000,
      reconnectionDelayMax: 25000,
      auth: {
        token: *****
      }
    });

Expected behavior I expect the 4.6.1 client not to reconnect at a fixed interval.

Platform: I can reproduce with:

  • Chrome Version 113.0.5672.127 (Official Build) (64-bit)
  • Firefox 113.0.1 (64-bit)
  • Microsoft Edge Version 113.0.1774.50 (Official build) (64-bit)

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 1
  • Comments: 19 (6 by maintainers)

Most upvoted comments

@nikhildigde thanks for the reproducible example ❤️

It’s because you are using reserved event names:

Those event names have a special meaning and cannot be used in your application.

Reference: https://socket.io/docs/v4/emit-cheatsheet/#reserved-events

The parser now includes a check: https://github.com/socketio/socket.io-parser/commit/d9db4737a3c8ce5f1f49ecc8d928a74f3da591f7 (included in version 4.6.x)

since the interval before disconnection is fairly stable (~1.2 minutes), I’d say something between your server and your client is closing the connection

Most likely yes, the interval is indeed really stable so quite suspicious…

Do you have any proxy (nginx, httpd) between them?

It’s an angular app, so it is served through angular cli but I don’t think it matters in this case.

As for the backend handling the websockets, it’s built using NestJS so maybe something on that side even though I’d question my own code first before questioning NestJS code 🙃.

since the interval before disconnection is fairly stable (~1.2 minutes), I’d say something between your server and your client is closing the connection. Do you have any proxy (nginx, httpd) between them?

There’s nothing for the ping / pong that we need to implement ourselves right? It’s built into the lib I’d guess?

Yes it is. The server sends a ping every pingInterval (25s by default), and expects a pong from the client before pingTimeout (20s by default).

Are you able to reproduce the issue in local? In that case, could you please provide some code?

I have tried pretty hard to repro in 3 different configs:

  • Pure node js and socket IO
  • Pure node js and socket IO + redis (as I suspected initially it may have been related)
  • NestJS app with socket IO and redis

Unfortunately I still haven’t managed to repro even though the code looks similar…

@darrachequesne - Thanks for the prompt response! Will change accordingly.

Yes it works. Thank you very very very much 😃

@darrachequesne @yakirs57 @valentin-at-edlio The error can be seen clearly here on the tweaked version of your fork.

The test scenarios described in the README

These is a null value in the packets, but I think you will also be able to see directly for better understanding.

Hope this is helpful and if you have anything further do let me know.