Design: does support for socket(udp and tcp)?

Created on 24 Nov 2018  ·  12Comments  ·  Source: WebAssembly/design

does support for socket(udp and tcp)? if not, has a plan? thanks.

Most helpful comment

I think WebAssembly or even WebAPIs should support lower-layer protocols like TCP and UDP etc. Either WebRTC or WebSocket is an upper layer protocol, which has been wrapped with so many unecessary things and limitations. For example, we couldn't make QUIC connection polyfills or use customized TCP transmission control for some cases.

If the security should be concerned, some rules we could make. For example, we could make a preflight request for CORS policies.

All 12 comments

It can be archived by passing functions from the host.

i want port my network program that use udp, but don't find appropriate functions.

@csy2002 If you are using C or C++, you can try using Emscripten.

It compiles C/C++ programs to WebAssembly, and it provides WebAssembly wrappers for the C stdlib, the C++ stdlib, SDL, OpenGL, and more.

In many cases C/C++ programs just work with Emscripten, without any modifications.

@Pauan thank you for your reply. my native code is using c or c++, and i want to using udp to transport media stream (video or audio data), but don't find the appropriate functions.
because some reasons, i can't use webrtc for media, so i justly finding another solution.

@csy2002 I don't know C or C++, but my understanding is that Emscripten supports (basically) the entire C/C++ stdlib, so existing C/C++ functions should work just fine. So your existing code should work.

However, even though you can use existing C/C++ APIs, it will internally use WebSockets for the connection, so your server must accept WebSockets. And WebSockets are TCP only.

This is not a limitation in WebAssembly, it is a limitation in the web browser itself: the browser only allows you to use HTTP(S), WebSockets, or WebRTC. It has no support for raw sockets (UDP or TCP).

If you're in a browser and you need the non-reliable communication of UDP, then your only standard option is to use WebRTC. WebRTC is usually for peer-to-peer connections between users, but you could make your server be a WebRTC "peer" so that the user opens a WebRTC connection to the server. This isn't well-established territory as far as I know though, so I don't know of any full examples of this to point anyone to. (PeerJS is a simple example of WebRTC data channels being used on the client-side.) Also, not all browsers (like Edge) support WebRTC data channels, so unless you're just using WebRTC's audio+video support, then you would need a WebSocket (TCP) fallback if you want to support those browsers.

I think WebAssembly or even WebAPIs should support lower-layer protocols like TCP and UDP etc. Either WebRTC or WebSocket is an upper layer protocol, which has been wrapped with so many unecessary things and limitations. For example, we couldn't make QUIC connection polyfills or use customized TCP transmission control for some cases.

If the security should be concerned, some rules we could make. For example, we could make a preflight request for CORS policies.

For example, we could make a preflight request for CORS policies.

Many public servers won't add support for these preflight requests. The absence of response could be treated as "there is no restriction on this server". But why don't we create a special permission? I don't think these kind of connections should be made without explicit user approval.

Many public servers won't add support for these preflight requests. The absence of response could be treated as "there is no restriction on this server". But why don't we create a special permission? I don't think these kind of connections should be made without explicit user approval.

I don't think whether the public servers support preflight requests or not is the problem, since the security check is only done by client (eg. browser). If the public servers don't respond with correct preflight response, the client can block the subsequent invocations of the lower-level APIs.

@Pauan any idea why this issue is not closed? Based on your comment

This is not a limitation in WebAssembly, it is a limitation in the web browser itself: the browser only allows you to use HTTP(S), WebSockets, or WebRTC. It has no support for raw sockets (UDP or TCP).

I don't think this will be ever supported. Any comments?

@ondrejtomcik I'm not a part of the WebAssembly or W3C working groups, so I can't comment on that.

But in my personal opinion this feature should go into the browser itself, not Wasm, so it would be better to open a new issue with the W3C.

As mentioned above, if you are targeting the Web (via Emscripten), you likely have to port your networking logic to use WebSockets or try out WebTransport.

If you are targeting server environments, take a look at WASI, it is not implemented yet but there are feature requests (https://github.com/bytecodealliance/wasmtime/issues/70).

This is not something that goes into WebAssembly itself: it is provided by the host/embedder.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mfateev picture mfateev  ·  5Comments

dpw picture dpw  ·  3Comments

chicoxyzzy picture chicoxyzzy  ·  5Comments

artem-v-shamsutdinov picture artem-v-shamsutdinov  ·  6Comments

bobOnGitHub picture bobOnGitHub  ·  6Comments