Question

I am using socket.io and the dgram node library to send UDP messages from one program to a browser via node.
The code looks just like the socket.io example

var dgram = require("dgram");
var dServer = dgram.createSocket("udp4");
dServer.bind(12345, '0.0.0.0');
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
  dServer.on("message", function (msg) {
     socket.send('message', msg);
  });
});

My question is at what speed are the datagrams ultimately being sent to the browser? Are they being sent at TCP speeds, web socket speeds (which I understand to be slower) or UDP speeds (which I understand are quicker when it comes to real time communication).

Also, in this example is io perpetually listening on port 80 (meaning it can only receive things at http / tcp speeds) or does it just listen to establish the socket connection and then can stop listening (and let the socket connection take over)

Was it helpful?

Solution

The speed of UDP, TCP and WebSockets "Packets" are the same (e.g. wire speed), but they differ in overhead and reliability.

  • WebSockets are a tunnel within an existing HTTP connection, so in effect TCP with more overhead. But they use existing ways in tunneling through NAT router and firewalls.
  • TCP has reliability guarantees, e.g. no packets get lost and no duplicates. TCP needs an initial 3-way handshake, but this is once for a connection and not for each data chunk.
  • UDP is fire and forget and you need to implement you own reliability on top if you need it. Apart from that, I'm not sure if the browser will even natively accept your UDP packet, at least outside RTP context (e.g. WebRTC). It might work with Java and Flash.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top