Socket.io: How to limit the size of emitted data from client to the websocket server

后端 未结 5 944

I have a node.js server with socket.io. My clients use socket.io to connect to the node.js server.

Data is transmitted from clients to server in the following way:

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-11 14:25

    EDIT: Because the question is about "how to handle server overload" You should check load balancing with gninx http://nginx.com/blog/nginx-nodejs-websockets-socketio/ - you could have additional servers in case one client is creating a bottleneck. The other servers would be available. Even if you solve this problem, there are still other problems, like client sending several small packets and so on.

    The Socket.io -library seems to be a bit problematic, managing too big messages is not available at the websockets layer, there was a pull -request three years ago, which gives an idea how it might be solved:

    https://github.com/Automattic/socket.io/issues/886

    However, because WebSockets -protocol does have finite packet size it would allow you to stop processing of the packets if certain size has been achieved. The most effective way of doing this would be before the packet is stransformed to JavaScript heap. This means that you should handle the WebSocket transform manually - this is what the socket.io is doing for you but it does not take into account the packet size.

    If you want to implement you own websocket layer, using this WebSocket -node implementation might be useful:

    https://github.com/theturtle32/WebSocket-Node

    If you do not need to support older browsers, using this pure websockets -approach might be suitable solution.

提交回复
热议问题