0

I have in plan to write a real time "game" for hobby purposes. My idea is to use websockets and canvas and I want to imitate same behaviour as slither.io has, so I would like to have a feature that when user moves, other users can see his move in the same time.

First think that I had in my mind, was to use WebSockets and requestAnimationFrame, so I could for each frame emit event from the client to the server via socket and give information about my coordinates to other sockets.

My problem is that I'm troubling that this will not efficient and would kill my server with the events.

So is it good way to design real time communication in browser game or is there a better way to do this?

Stwosch
  • 602
  • 1
  • 7
  • 20
  • 1
    Not really sure if this is what you're looking for. But, if RAF at least does ~60fps, you still wouldn't want or need to perform websocket operation for 60 times per second. I am assuming that you just need a listener and broadcaster for each player, and perform the necessary operation. In case of movement above, you can probably send the player movements data in a chunk for once every n second and animate it for other players. – choz Oct 04 '18 at 06:05
  • Yes of course, but take an example that 2 users move without break, so it should probably perform operation 60 times per second via websocket – Stwosch Oct 04 '18 at 06:07
  • 1
    Depends on what movement logic is behind the scene. But, it's also possible to send those frame data in a chunk (e.g. once every second), and store them as a series of animation to be played. However, there'll be eventually split seconds *breaks* and I think that might be inevitable in most of RTC. – choz Oct 04 '18 at 06:13

1 Answers1

1

You can certainly use WebSockets, and there are some great resources at the question here: Are WebSockets suitable for real-time multiplayer games?

You should consider a slower tick rate than just doing it once for every animation frame. Making a state update every video frame, which might be happening up to 60 times per second, is probably overkill. Overwatch, which is a moderate-paced game, does one just over every 20 seconds, for comparison.

To do the state update independently, for example in socket.io, you could use setInterval

var dataTickRate = 30;

setInterval(function() {
  socket.emit('playerState', playerState);
}, 1000 / dataTickRate);
Ruzihm
  • 19,749
  • 5
  • 36
  • 48