90

I'm writing a JavaScript Application that has to receive a huge amount of data from other users. It is not important if some of this data gets lost. Is there some way of using JavaScript WebSockets with UDP instead of TCP?

  • 2
    It can be done over webRTC. https://geckosio.github.io/ game server uses this technique. – Fost Aug 10 '20 at 14:36

8 Answers8

65

It sounds like what you're waiting for is WebRTC which is working it's way through the standards process. WebSockets, as other people have pointed out, run over TCP as a result of initiating with an HTTP Upgrade.

Chad
  • 2,064
  • 1
  • 22
  • 29
  • 6
    WebRTC looks almost great, but as far as I can tell you have to use RTCDataChannel for data communication, and that uses SCTP which doesn't support unreliable deliveries (necessary for games). Edit: Cancel that, it does support an unreliable mode. [Good article here](http://www.html5rocks.com/en/tutorials/webrtc/datachannels/) – Timmmm Apr 06 '14 at 11:12
  • Not only that, but it's Peer 2 Peer, not great for an authoritarian server. – NiCk Newman Nov 19 '15 at 06:32
  • 14
    There's no reason one of the "peers" can't be your server. – Chad Apr 20 '16 at 21:24
55

No, it's not possible to have UDP communication within JavaScript. Sorry.

Derk Jan Speelman
  • 11,291
  • 4
  • 29
  • 45
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • 2
    @TimeMachine They won't WebSockets built on top of TCP, the initial handshake is also made so that it is recognized as an HTTP upgrade request by web servers. You can read more about the protocol in the latest draft: http://tools.ietf.org/html/draft-abarth-thewebsocketprotocol-00 – Ivo Wetzel Jan 11 '11 at 22:17
  • 3
    @IvoWetzel Why does that mean they won't? I can think of several ways to implement UDP Websockets securely. – Timmmm Apr 06 '14 at 10:41
  • 41
    Answer is not up-to-date anymore: WebRTC http://www.webrtc.org/ offers (experimental) UDP in the browser and nodejs can access udp sockets on the server. – Mirko Sep 13 '15 at 11:53
  • Here's a [discussion of this issue][1] in relation to gaming. There he covers websockets, webrtc (a possible choice for UDP, but complicated), quic (only in google chrome), and his [netcode.io][2]. [1]: https://gafferongames.com/post/why_cant_i_send_udp_packets_from_a_browser/ [2]: https://github.com/RedpointGames/netcode.io-browser – Diagon Sep 26 '17 at 10:31
  • [UDP/Datagram Sockets](https://nodejs.org/api/dgram.html) connection with node.js! The dgram module provides an implementation of UDP Datagram sockets. – Derk Jan Speelman Feb 19 '20 at 12:43
  • @ Derk Jan Speelman - I'm not sure if he wants a UDP implementation that works with browser JavaScript. I don't see any indication that it works with browsers. I think WebRTC is still the best solution. – Benargee Aug 25 '20 at 01:33
8

Sounds like the question is meant for client-side UDP, but since I ended up here...

You can do UDP in JavaScript on the server using the node.js dgram package.

Nick Benes
  • 1,334
  • 15
  • 14
4

The WebSockets protocol is over TCP only as currently defined.

You could do UDP with Flash if you are willing to use a RTMFP (Real Time Messaging Flow Protocol) server.

Community
  • 1
  • 1
kanaka
  • 70,845
  • 23
  • 144
  • 140
1

If this question is still pending: I found a project called JNEXT and there is an example using UDP. The project itself seems to be paused but at least in Firefox it works (it doesn't work with Chrome and Opera). May be it is worth to look for it.

Klaus
  • 19
  • 1
1

As of HTTP/3 Release, there is a new Protocol for the browser called WebTransport,WebTransport_API and it supports the UDP protocol, Similar to WebSockets but with support for multiple streams, till then the only way to use UDP in the browser was to use WebRTC streams

caniuse.com

Isaac Weingarten
  • 977
  • 10
  • 15
-5

I think you can leverage Silverliht 4 technology. You can create a Silverlight 4 application to communicate with server and then enbamdded it to html page. Then your JavaScript can build TCP connections via Silverlight 4 application!

John
  • 23
  • 1
-9

You could write a WebSocket server to serve as proxy/bridge between TCP/UDP.

Tim R.
  • 7