1

As a hobby / to learn I am building an app in JavaScript using Node.js where a component of it will take input from a client, send it to a server, and then broadcast it to other clients. For simplicity let's say that the data looks like: {"x_pos":0.4, "y_pos":0.2}, and specifies an avatar's (x,y) position on a map in a game. I want each user to have an avatar, and each avatar's (x,y) position shared.

Currently I am using Websocket (socket.io) to do this. I figured Websocket would be ideal because it is TCP, and will include an identifier of who each user is. However, the fact that communication is bidirectional seems to be sub-optimal. Additionally, I am emitting position data from all clients 30 times a second to the server, which then broadcasts it to all users. This works well for one user, but I do not know how it would scale.

However, I have also heard that UDP is ideal for games, but I understand that UDP is connectionless and doesn't track user connections etc. So then would this mean that I would not be able to keep track of who incoming (x,y) data belongs to? (I suppose I could change the data to be something like {"user":"id", "x_pos":0.4, "y_pos":0.2} and handle updates on the Client side that way). There is also WebRTC, that uses UDP, but peer to peer connections I doubt would scale well.

So I am curious what people think is the best protocol here. Am I on the right track by using Websocket to broadcast player position? Or should I be using something else?

I would like to note I am not building a commercial app in any way, and I anticipate the load to be no more than 6 people at once for this. But 6 people * 30 emits a second to the server + 6 * 30 emits to all clients means 360 socket.io emit() events a second, which seems maybe not what socket.io was built for here? That said, I hear that Websocket establish a data stream, where UDP does not, so maybe that means that UDP may be more overhead? I honestly do not know and cannot find this information readily online.

3
  • 1
    UPD is essentially a "fire and forget" transport mechanism for messages that fit in a single packet. UDP does not guarantee delivery or even delivery in the same order as messages were sent. How well can your game deal with those characteristics? Jan 8 '21 at 7:41
  • I think it can handle losing out a on a few packets since it's conveying absolute position and there's a decent amount of smoothing. With a normal internet connection, would UDP fail more on the order of milliseconds (e.g. 1 or 2 packets are lost a second) or seconds (e.g. 30+ packets are lost a second)? Jan 8 '21 at 21:31
  • The packet loss with UDP is the same as the retransmission rate of a TCP connection (which you notice in an increased latency). A bigger problem might be the out-of-order delivery. Jan 9 '21 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.