Is it possible to use UDP with socket.io?
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.
It might be a good idea to use webRTC in this case which is UDP in nature.
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.