uNetworking / uWebSockets.js

μWebSockets for Node.js back-ends :metal:
Apache License 2.0
8.08k stars 574 forks source link

How to optimize for multiplayer games #799

Closed starlite-developer closed 2 years ago

starlite-developer commented 2 years ago

I have a few questions.

I have created a multiplayer game and everything works fine. But when I try playing with a trash computer client, it can't really keep up with the data sent. I have used entity culling and other things to make it as smooth as possible and it has greatly improved, but there are a few more things I think I can do to make it run as optimally as possible.

1.) Is there a way to ONLY send the most recent message? Sometimes the trash computer lags so bad its like watching a movie of what happened the delay is so bad. 2.) Is there a build it way to measure ping. I have created my own, but it may not be as accurate. 3.) Any other tips or tricks to make uWebSockets.js work better for multiplayer games?

uNetworkingAB commented 2 years ago

What is your maxBackpressure and how are you sending?

starlite-developer commented 2 years ago

maxBackpressure is 0 (i don't know if that's an invalid value or not)

I am sending updates about 60 times a second if an update is necessary. Its a JSON response, fairly small.

uNetworkingAB commented 2 years ago

I would begin by setting maxBackpressure to 1

starlite-developer commented 2 years ago

No change.

uNetworkingAB commented 2 years ago

There is definitely a change with maxBackpressure 1 instead of 0. Your sending had infinite backpressure before, now 1 byte.

If you want even less, you need to lower your system send buffer size to something like 4kb. See SO_SNDBUF

starlite-developer commented 2 years ago

By no change I mean it's not sending the most up-to-date message from the server. It still plays a delayed "movie" at times. I have noticed overall smoothness is improved.

starlite-developer commented 2 years ago

Oh sorry, when I was testing my websocket disconnected. I tried it again and it works.

uNetworkingAB commented 2 years ago

So the slow computer is showing more like a stuttery slide show rather than a smooth movie now?

youfeed commented 8 months ago

Doing this can reduce latency. Will subscribers prioritize pulling the latest audio and video streams?