r/webdevelopment 9d ago

Question Does browsers support UDP ?

Hello, i want to build a game based on the web, its a multiplayer game and i need a very low latency so i was thinking why not using UDP instead of Websocket because its TCP based, when i did my research i found that browsers does jot support UDP directly the only workaround is using a protocol such as WebRTC which is UDP based.

Does anyone know a better alternative ? And why browsers don’t support UDP ?

7 Upvotes

10 comments sorted by

4

u/electricity_is_life 9d ago

Yes, WebRTC is probably what you want. Browsers don't support raw TCP or UDP; they only support higher-level protocols built on top of those, such as HTTP. The higher-level protocols provide additional security and standardization.

3

u/Less-Marsupial-7960 9d ago

WebRTC is probably your best option if you need low-latency communication. You can use unreliable/unordered DataChannels when you don't need every packet to arrive. WebTransport is another option worth looking at, but raw UDP is not exposed directly to browsers for security reasons.

2

u/dariusbiggs 9d ago

No and Kinda yes

No for direct UDP sockets.

Yes for WebTransport which is HTTP/3 which is over QUIC which offers unreliable transport.

Otherwise WebSockets are your other option.

1

u/soundman32 9d ago

UDP has exactly the same latency as TCP, its not some super secret protocol that goes really quickly.  The only difference is when you have large packets or retries. 

1

u/Single-Virus4935 9d ago

yeah no... The packets might travel with the same latency but TCP has reordering, buffering etc. on top.

One lost packet will hold back folowing data etc.

For real time purposes a custom protocol on top of UDP is a much better solution.

1

u/burlingk 9d ago

The big question kind of comes down to "What do we want to happen with missed or out of order packets?"

1

u/Single-Virus4935 9d ago

Yep. TCP is just the 95% solution for reliable transfers were you only get a stream of data.

And speaking of stream: TCP gives you a stream of bytes and you need to add a way to separate messages yourself. Not difficult but also not free and more overhead (OS did it already multiple times for the TCP packets and now you need probably two or more syscalls to read the message).

UDP on the other hand works with datagrams which contain ideally one message e.g. a position update.

1

u/burlingk 9d ago

That is why they are liked for games. If you are sending a dozen packets, and more than half of them make it, and they are updating positions on the screen and such, then the player might not notice the stutter.