2

I am coding a multiplayer game and use a Websocket Server to send messages between clients for the game to work. During random times in the game, there is a massive, absolutely massive delay in the message time between the messages ONLY sent from the server to the client. The delay from the client to the server does not increase during this. This only happens periodically, most of the time the game works fine.

I built a ping meter to measure the time between the client sends a message and the server receives it and vica versa. In the server to client, during random periods, the ping skyrockets. I'm talking like 20+ seconds. It doesn't immediately jump. It increases steadily.

I can't put all the code for the game here(over 10,000 lines), but here is how the game works:

Server send a hostupdate to all sockets connected to the room.

  • It's a JSON message. I don't exactly know how big the message size is, but there is a considerable amount of objects (a few hundred).
  • Server sends this message every 30 ms. I don't know if I am straining the sockets by sending it too fast? I kind of need a good frame rate sent to players.
  • This is the message that is being delayed sent to clients.

Client sends a joinerupdate to server:

  • Every 30 ms
  • Sends updates like change in x and y

I am using the WS npm module to run my WebSocket Server and using the default browser built in websocket to connect to my server.

1 Answer 1

2

SOLUTION:

On the WS npm package site I found an example that gives better performance for sending messages. I no longer have a problem :D

import WebSocket, { WebSocketServer } from 'ws';

const wss = new WebSocketServer({
  port: 8080,
  perMessageDeflate: {
    zlibDeflateOptions: {
      // See zlib defaults.
      chunkSize: 1024,
      memLevel: 7,
      level: 3
    },
    zlibInflateOptions: {
      chunkSize: 10 * 1024
    },
    // Other options settable:
    clientNoContextTakeover: true, // Defaults to negotiated value.
    serverNoContextTakeover: true, // Defaults to negotiated value.
    serverMaxWindowBits: 10, // Defaults to negotiated value.
    // Below options specified as default values.
    concurrencyLimit: 10, // Limits zlib concurrency for perf.
    threshold: 1024 // Size (in bytes) below which messages
    // should not be compressed if context takeover is disabled.
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

What did you do previously?
Just the default of const wss = new WebSocketServer({ port: 8080, }); The extra stuff help improve performance insanely

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.