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.
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.
Related
What's wrong with having plain old TCP / UDP sockets in browsers' API?
I know that there's such thing like WebSockets (that is built via TCP under-the-hood) but hey, what's wrong with plain sockets? Is it due some security issues? If so, how WebSockets help to prevent them?
I have been going through this websockets article.
Can a web-socket communicate with a TCP/UDP Socket and viceversa?
Websockets use TCP sockets.
Websockets are a higher level technology relying on TCP sockets, exactly the same way HTTP is transported over TCP sockets.
In fact, Websockets are a special extension of HTTP. The client issues a special HTTP request that leaves the underlying TCP socket open, which then allows both the client and the server to push data on the connection.
What you essentially need is a WebSockets client library in your application. This will utilise TCP sockets on a lower level, and talk to your webserver using the WebSockets protocol.
The benefit of this approach is that your application code only has to deal with the library's higher-level API as well, instead of the nitty-gritty sockets API, where you'll have to implement HTTP and/or WebSockets again.
I need to connect and send data packets to a UDP server (connection less/datagram socket) from within a browser.
What are my options?
Does HTML5 allow connection-less sockets?
Would I be able to connect to a UDP server (connection-less socket) using a WebSocket?
Your options are very limited. Browsers which support WebSockets expect the server to speak WebSocket to them (which involves participating in a 2-way handshake). Communicating in raw UDP is an entirely different kettle of fish.
Chrome has experimental support for raw UDP, but only for Chrome extensions.
I don't know of any other browser with this in the works, or for Chrome to make it available for websites.
Your best best would be to change your endpoint to talk WebSocket, or to use a middleman server (NodeJS would be great for this) to handle the Websocket <-> raw UDP conversion.
I have an application (essentially a game) that is broadcasting game state data via UDP to many connected clients on a private LAN.
UDP works fine for broadcasting game state. Not having to configure the clients is important for this app. The client just read the UDP datagram stream and build up state as it goes.
But now I need the clients to reliably download a few pieces large data payload from the server. TCP is way better then UDP for that.
But we still rather not have to configure each and every clients with the host info.
It would be better to just embed an service advertisement in the broadcast UDP stream and then have each client see the advertisement and connect to the TCP host with no extra configuration on the endpoints.
Is there an standard way, or better, example code of advertising a TCP service via UDP. Preferably in C++.
The client needs to know the IP and port of the TCP server, that is all. If you can embed that info into your protocol it will work.
Actually, the UDP clients probably know the IP already because the UDP packets have a sender IP. Maybe this fact can help you.
One of the options here (maybe not for just a game but for some "enterprise" service) is setting up SRV records in local DNS.
Is there an easy way to open a TCP socket to connect a TCP server? Should I use socketjs, jsocket or something else?
I try to develop a cross-platform application to connect to a TCP server and pull data from it.
Thanks
No, we do not have an API for that at present. But you can use WebSockets on iOS, and libraries like socket.io which fall back to long-polling on Android.
If you email us at support#trigger.io we can advise on your specific requirements and see if it could make sense to offer a native API.
I'm the author of trigger.io-tcp, an android-only trigger.io native module that allows you to open TCP sockets for sending/receiving data.
You can use it if you need sockets only for Android or fork it and write the iOS part.