Home » Nodejs » Node.js UDP for realtime multi-player game

Node.js UDP for realtime multi-player game

Posted by: admin November 29, 2017 Leave a comment


I am building a real-time multi-player browser-based game using node.js. At the moment I have the client send user input to the game logic server via socket.io and a snapshot of the game world sent back to the client for rendering.

Below is a simplified version of the code. Is it possible to use UDP to send data from the browser-based client to the server and vice-versa? I know Node.js has a UDP module but I am unsure how to implement it in this way.

Any help would be appreciated. Thank you.


var server = http.createServer(handler).listen(8888);
var iosocket = io.listen(server);

// request/response handler
function handler(req, res){


iosocket.sockets.on('connection', function(socket){
    console.log("[x] Socket listener started");

    socket.on('msg', function(data){
        console.log( " [-] incoming message);


iosocket.sockets.emit("message", msg);


<!DOCTYPE html>
        <script type="text/javascript" src="/socket.io/socket.io.js"></script>
        <script type="text/javascript">
            socket.on('connect', function(){
             console.log("connected to server");

            socket.on('message', function(message){
            console.log('received message');


        <canvas id='canvas' width="400" height="400">Canvas</canvas>


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


[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();