My socket.io server start disconnecting clients randomly (for a “ping timeout” reason) when many clients are connected

拟墨画扇 提交于 2021-02-05 09:28:05

问题


I'm building a website, where my client communicates with the server over web sockets. I'm using nodejs on the backend and hence, the famous socket.io library for web sockets communication.

The Problem

Everything works fine with 1 to ~ 40 clients, after that the server starts disconnecting clients randomly. at the start I thought it is a scaling problem, maybe memory or something like that. However, after an extensive debugging session, I noticed that the reason for disconnection is ping timeout.

What I have tried

I searched on the internet and some people recommended increasing the pingtimeout and pingInterval options when creating the server and others recommended using the transports: ['websocket'] option. So I tried both approaches. Here is what it looks like:

const io = require("socket.io");
const socketServer = io(port, {
      transports: ['websocket'],
      allowUpgrades: false,
      pingTimeout: 600000,
      pingInterval: 300000
    });

This worked fine for a while. I noticed that my server can handle up to ~80 clients with these configurations. However, a weird error appear when ~80 or 90 concurrent clients are connected:

WebSocket connection to 'ws://localhost:3000/socket.io/?EIO=3&transport=websocket' failed: WebSocket is closed before the connection is established.

I'm assuming this error occur because I'm setting transports: ["websockets"] but I'm not sure.

My Goal

I want my server to handle >=1000 clients. I mean 30, 40 or even 100 clients should be not that much for a node server to handle, am I right? Besides, the socket.io library is very famous and recommended from other developers, so I guess that is not the problem.

I appreciate any help if someone know something about these issues and how to solve them. I have been stuck on this for two weeks now and my server can't scale.

来源:https://stackoverflow.com/questions/65934110/my-socket-io-server-start-disconnecting-clients-randomly-for-a-ping-timeout-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!