How to talk to UDP sockets with HTML5?
JavascriptHtmlSocketsWebsocketJavascript Problem Overview
What I have : A C++ application server running, Ready to send data to client which is supposed to a HTML5 page or app.
What I want : Is there any way to communicate using udp port with HTML5 given both c++ server and HTML5 app are local to system ?
What I know :
- Because of Security Concern, JS doesn't allow UDP port communication from browser.
- Have read in many places, Answer is no. But answers are old.
Is the answer still 'NO' ?
Is there any work-around possible ?
Any lead is appreciated.
Javascript Solutions
Solution 1 - Javascript
Yes, the answer is still 'no'. Websockets are TCP based. Note that a WebSocket is not a plain TCP connection, there is HTTP negotiation and a framing protocol in place. So you also cannot create a plain TCP connection in Javascript.
WebRTC is based on UDP, it may cover your use cases: http://www.html5rocks.com/en/tutorials/webrtc/datachannels/
Solution 2 - Javascript
Chrome now seems to have something: https://developer.chrome.com/apps/sockets_udp
Solution 3 - Javascript
It looks like UDP for web is still an active area of development and potential standards creation. Posting this answer to record some new info current as of May 2020.
The following whitepaper has outlined a potential path forward that satisfies the security needs for an "unreliable-unordered" protocol: https://gafferongames.com/post/why_cant_i_send_udp_packets_from_a_browser/
There are extensions to desktop Chrome and desktop Firefox that are in active development. https://github.com/RedpointGames/netcode.io-browser The way mobile browsers are designed prevents this kind of modification from being added at present (good security reasons again) but could be added down the road.
Solution 4 - Javascript
This is a major issue for gamers. See that link for a discussion of websockets, webrtc, quic (in chrome), and the author's netcode.io
Solution 5 - Javascript
You could alternatively create an additional python local server for bridging the data between your C++ application and webpage.
The html5 webpage connects to a local port that allows a web socket connection (use Flask/tornado).
The C++ application connects to a UDP listener on a different port. See https://wiki.python.org/moin/UdpCommunication to setup.
The python server basically forms a transparent data bridge between UDP port to websocket connection .
Solution 6 - Javascript
You could possibly use a work around, design a program/script/server(I would use PHP, being a html client) to get the UDP gram from the server, if you would like I could help, I have worked on something similar.