I have a game I am working on and I heard that UDP is superior for real-time games. I know that socket.io uses TCP and was wondering if there is some way to switch it to UDP. I tried looking it up but only found posts from around 2012 which said UDP is only experimental in browsers.
3 Answers
From a standard browser, it is not possible.
From a browser client, socket.io
uses either the http
or the webSocket
transport. Both http
and webSocket
are TCP connections, not UDP connections. So the browser client socket.io does not use UDP - it uses TCP.
As best I know, there is no standard UDP support in browsers accessible from regular HTML page Javascript so you can't even really try to build your own layer that uses UDP.
Other references on the topic:
Why Can't I Send UDP Packets From a Browser
Reading from udp port in browser
Chrome Supports TCP and UDP Sockets
Write a chrome extension to support UDP in browser
How to send a UDP Packet with Web RTC - Javascript?
How to talk to UDP sockets with HTML5?
Reading from udp port in browser
UDP can be a desirable transport for some circumstances when you want the packet to be delivered as soon as possible, but if it can't be delivered immediately, then just drop it. This is sometimes useful in gaming or even video streaming where the next packet will just contain the next update so if the previous one didn't come through, then no big deal and you'd rather not have TCP try to retransmit the lost packet. But, browsers don't support using the UDP protocol from web page Javascript.
If you want to connect to a UDP device or server from a browser, you will have to use some sort of proxy so your browser code can talk to the proxy over TCP (either http or webSocket) and then the proxy can handle the actual UDP communication with the device.
It would be possible to use the socket.io library from node.js or some other non-browser platform and write your own UDP transport add-in for socket.io that is built on the native UDP support in your platform. I believe socket.io has a somewhat pluggable transport so you could try to make your own transport and then configure both client and server to use that transport. This is not possible from the browser (without a native code plug-in installed in the browser) because there's no underlying UDP support in the browser that you could build your transport on, but in non-browser environments like node.js, you could do that.

- 683,504
- 96
- 985
- 979
-
6Why another downvote? Downvoters, please explain. If you have a legit beef, I will correct my answer. Socket.io in a browser does NOT run on UDP. That simply doesn't exist because the browser does not support a UDP transport of any kind. If you're downvoting because you're being misled by Ramazan's answer, then please read the comments below that answer as that answer is very misleading and is not offering what this question is asking. – jfriend00 Jan 13 '18 at 01:29
It might be a good idea to use webRTC in this case which is UDP in nature.

- 49
- 3
-
1
-
3Whilst I agree that when I wrote this I was thinking of socket.io, I still appreciate @TensorVortex mentioning an alternative approach (assuming it works) given that based on other responses socket.io doesn't seem to support UDP at all. – Moo Feb 23 '19 at 21:32
Although the question is already answered, I want to point out that there are ways to implement socket.io with UDP. For example dgram does exactly what you are looking for.
This is a tutorial for socket.io + UDP with dgram.
UPDATE:
Alexandre Lacheze developed a node.js package that brings UDP to browser. It also supports socket.io. So the answer is somehow obsolete now.
UPDATE 2: It turn out it is just a simulated UDP. Not an actual UDP protocol running on browser.

- 7,111
- 1
- 48
- 76
-
6I think you may be confused about what is described here. Your first reference runs entirely on node.js and does not run in the browser. That's just sample code for the UDP support bulit into node.js. So, that has nothing to do with UDP in the browser. Your second reference is simulating a UDP-like interface, but running it over webSocket or socket.io transport (which are both TCP connections). So that is a simulated UDP interface that works in a browser, but the actual transport is TCP, not UDP. So, neither of these is running an actual UDP protocol in the browser. – jfriend00 Aug 05 '17 at 18:11
-
5The second example, when used with a server-based proxy that converts this simulated UDP running over socket.io to actual UDP can be used to communicate with a UDP service from a browser, but one should not be fooled into thinking that this is using UDP in a browser for the reasons one would typically want to use UDP for responsive, low-lag gaming - as it is not. It is clever and may have some interesting uses, but it is not what this question asked which is how to run socket.io over UDP. FYI, the name of the interface in your second option is `SimUDP`, simulated UDP. – jfriend00 Aug 05 '17 at 18:12
-
6So, to summarize. Nothing in your answer shows an implementation of socket.io running over UDP so that part of your answer is just completely unsupported. And, your assertion that Alexandre Lacheze's package brings UDP to the browser is also wrong. It brings a simulated UDP-like interface that is actually running over TCP to the browser. – jfriend00 Aug 05 '17 at 18:21