6

I'm writing a multilayer card game (like hearthstone) with Nodejs back-end and an angular front-end.

I tried to connect the two with Socket.IO, but it turned out that if I send a JSON object over about 8000char(the gameState object), then the client just keeps disconnecting and reconnecting.

If I only send the substring of 6000 char of the object everything is fine, but it crashes over 8000(I need about 20 000)

I have only tried in localhost

Here is the backend:

    constructor() {
    this.app = express();
    this.port = process.env.PORT || ChatServer.PORT;
    this.server = createServer(this.app);
    this.io = socketIo(this.server);
    this.gameService = new GameService();
    this.listen();
}

listen(): void {
    this.server.listen(this.port, () => {
        console.log('Running server on port %s', this.port);
    });

    this.io.on('connect', (socket: any) => {
        console.log('Connected client on port %s.', this.port);
        socket.on('message', (m: string) => {
            console.log('[server](message): %s', JSON.stringify(m));
            this.io.emit('message', JSON.stringify(this.gameService.getGameState()).substring(1, 6000));
        });

        socket.on('disconnect', () => {
            console.log('Client disconnected');
        });
    });
}

Edit:It works perfectly well with ajax, but I would need sockets

Ez Az
  • 73
  • 4
  • Maybe try chunking as suggested in this [socket.io GitHub issue](https://github.com/socketio/socket.io/issues/3135). – zero298 Jun 27 '18 at 18:42
  • Yeah but my object is like 4-5 mb, not 300 – Ez Az Jun 27 '18 at 18:44
  • When you send a substring are you actually just doing `gameStateJSON.slice(0, 6000)`? I'm wondering if you have invalid JSON somewhere after that 8000th character? – Cory Danielson Jun 27 '18 at 19:00
  • Normally I send the whole object, I just used the substring thing to test how much data I can send over the socket. If I send the object trough http, it works perfetly well on the front-end – Ez Az Jun 27 '18 at 19:03
  • It could be a timeout issue? You can adjust pingTimeout and pingInterval in socketio. You'll probably have to get more logging to know exactly what's going wrong – Cory Danielson Jun 27 '18 at 19:05
  • It disconnects and reconnects every 1 second, so I dont think the time is the issue. – Ez Az Jun 27 '18 at 19:14
  • 1
    @EzAz - Periodic reconnect can sometimes be caused by a mismatched client and server version of socket.io. Are you 100% sure you exactly the same versions of socket.io on client and server? – jfriend00 Jun 28 '18 at 06:12

1 Answers1

6

Socket.io has a default value of 1 MB (v3 onwards) as the maximum data size that it can receive. For v2 and before, this value used to be 100 MB.

You can use maxHttpBufferSize to increase the maximum size of data packet that you want to receive when creating the server.

const io = require("socket.io")(httpServer, {
    maxHttpBufferSize: 1e8    // 100 MB
});

According to the migration docs:

  • the client is disconnected when sending a big payload (> 1MB)

This is probably due to the fact that the default value of maxHttpBufferSize is now 1MB. When receiving a packet that is larger than this, the server disconnects the client, in order to prevent malicious clients from overloading the server.

You can adjust the value when creating the server: