Node.js UDP for realtime multi-player game Node.js UDP for realtime multi-player game node.js node.js

Node.js UDP for realtime multi-player game


Generally, browsers do not support UDP connections. Specifically, some browsers do. Google Chrome has a socket api:

http://developer.chrome.com/trunk/apps/socket.html

[2012/10/29 Edited as socket is no longer experimental - PhilH]

You could possibly also use socket APIs from the native client interfaces as well (not sure, only guessing).

If you're going try to do anything real-time on browsers in the near future, Websockets is probably your best bet, but those are TCP only.

Regarding your comments on UDP versus TCP speed, UDP will always be faster. TCP offers ordering and delivery guarantees (this means possible retries and holding other packets back until a lost packet finally arrives at its destination), while UDP only promises to send it once, not caring what happens with it afterwards. UDP will only send it's packet once and you need to figure out whether it got lost. When/if you receive lots of UDP packets, if order matters, you need to encode this in your data payload to be able to figure it out.

In general, UDP will be great for packets where missing a few usually do not matter and where only the latest packet really matters. A game may typically use a TCP stream where ordering and guaranteed delivery matters (the important stuff), and UDP streams for object movements etc (where only the latest position really matters, and if one update got lost it does not matter as long as each package contain the full position [instead of a delta where all packets matter]).

For your own game, I suggest implementing it on TCP first, and then when you have some more experience, you can try move the time critical stuff (where order and lost packets matter less) into separate UDP streams. There are many projects that have failed because people started with UDP first, and then tried bolting on ordering and delivery guarantees on top of it, effectively trying to reimplement TCP. Which is why doing it the other way around makes more sense.


A realtime application is generally one that receives data updates of at least 30 Hz and with less than 10% jitter. TCP/IP is reliable but can’t send periodic updates at that rate without jitter going off the scale occasionally. This is because TCP is handshaking and acknowledging to ensure reliable transmission which gets in the way of sending fast periodically smooth updates. UDP is a simpler protocol where the socket data is fire and forget. This is itself a problem but that problem is easier to overcome than the poor jitter of TCP/IP. In my experience UDP is the only way forward and why real-time applications use it inside protocols such as RTP used in VoIP.

Web Sockets are false hope as well since that is a TCP-based protocol. I use custom UDP sockets where the sender and receiver maintain a sequence number and that can tell you if packets are lost, duplicated or out of sequence, all problems on WAN networks. Find ways to use UDP and instrument the inbound data packets to measure performance.


var server = require('http').createServer();require('dgram-browserify').listen(server);server.listen(8080);