55
votes

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.

6
chrome apps have something like that in an API, i think. can you use something besides udp to push from C++ , like SSE for example?dandavis
Using html5 answer is still 'NO'. Websocket is always tcp. Webrtc support TCP/UDP depends on firewall but basically it is for pear to pear communication. If you want to use webrtc, you will have to implement webrtc on your serverSamir Das
Short answer No. I would simply close this as a duplicate of the many times this has been asked here before, but the answers elsewhere are surprisingly poor. The security problems are rather over-stated - it would be trivial to prevent amplification attacks that have proved troublesome with DNS and NTP. The reasons I am aware of are that 1) its not needed for most of what a browser does currently 2) its very difficult to implement across the internet due to NAT.symcbean
@symcbean But this is not duplicate. I am looking for alternatives also and workaround. Making every similar question duplicate doesn't really make sense.mkkhedawat
@dandavis , Actually but in Server Sent Events , Server - Client needs to be connected whole the time unlike UDP.mkkhedawat

6 Answers

38
votes

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/

10
votes
6
votes

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.

3
votes

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

-2
votes

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.

-2
votes

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 .