69

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.

mkkhedawat
  • 1,687
  • 2
  • 17
  • 34
  • 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 Apr 09 '15 at 07:20
  • 1
    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 server – Samir Das Apr 09 '15 at 07:27
  • 1
    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 Apr 09 '15 at 08:06
  • 3
    @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 Apr 09 '15 at 08:09
  • @dandavis , Actually but in Server Sent Events , Server - Client needs to be connected whole the time unlike UDP. – mkkhedawat Apr 09 '15 at 08:11
  • @Manish: ahh, ok, then the answer is just "no" – dandavis Apr 09 '15 at 08:28
  • I would suggest using a Python bridge. – Xofo Sep 27 '19 at 03:18
  • UDP can't be proxied through an HTTP/Socks proxy, which web browsers support. Allowing a web page to create an UDP socket would introduce a security issue, allowing a web page to reveal the client's IP address, which, in most cases, is not what you'd want. That's probably the reason why the current UDP support (WebRTC) requires some "negotiation" prior to allowing UDP communication. – Mladen B. Feb 21 '21 at 09:07
  • 1
    The security risks are no greater than websockets. NAT traversal is trivial by using a known server to establish connections. The main reason UDP would be prevented is likely to be business reasons: a fast, highly capable, lightweight client universal to all devices would destroy many business models including app stores. Same reason why Flash was destroyed by Jobs. – Jonathan Mar 02 '21 at 21:41
  • A browser is a fast, highly capable, lightweight client universal to all devices – rupweb Mar 12 '22 at 06:20

7 Answers7

45

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/

ctd
  • 1,693
  • 12
  • 27
vtortola
  • 34,709
  • 29
  • 161
  • 263
  • Thanks for you time. Will it need a web-server also ? If I am not wrong , I will need to use WebRTC protocol in my C++ application also, right ? – mkkhedawat Apr 09 '15 at 12:35
  • 6
    Yeah, WebRTC will need several things in order to work, since it is a peer-2-peer technology, you may need an intermediate server to do STUN/TURN/ICE in order to initiate communication between peers. It is not a trivial approach, so go through the docs before jumping into decisions :) WebRTC is not "the way" of using UDP in web, but it uses UDP as transport, so it may fit your requirements. – vtortola Apr 09 '15 at 14:23
  • WebRTC is the way to use UDP on the web today. There are some libraries that will help hide the complexities of WebRTC so you can focus on your UDP traffic. – Jonathan Mar 02 '21 at 21:48
  • I have tried to explain about UDP, TCP, and their usage with WebRtc. I hope that might help you guys: [How to send UDP packets with WebRtc](https://stackoverflow.com/a/70039639/10413749) – Muhammad Usman Bashir Nov 22 '21 at 08:32
  • How do browser fps games work if browsers don't allow udp connections? – theprogrammer Jun 05 '22 at 18:30
10

Chrome now seems to have something: https://developer.chrome.com/apps/sockets_udp

dee
  • 526
  • 4
  • 8
  • 8
    Came here from a different question - it's worth clarifying that this API is only accessible to Chrome apps/extensions. You can't use it from a web page. – Joe Clay Oct 28 '16 at 14:26
  • 5
    and Chrome apps are [to be discontinued](https://blog.chromium.org/2016/08/from-chrome-apps-to-web.html) – James Nov 30 '16 at 16:05
10

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.

Wajeemba
  • 101
  • 1
  • 3
3

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

Diagon
  • 473
  • 5
  • 16
0

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 .

0

After reading all the links and comments, we can conclude:

NO, YOU CAN'T SEND THE UPD PACKAGE FROM THE BROWSER.

And you probably won't, because adding such a feature would be a giant leap backwards in web security.

Alexander
  • 291
  • 4
  • 13
-2

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.

Craig Harp
  • 43
  • 3