Regards Emscripten Socket Server Creation

69 views
Skip to first unread message

Aitha Tarun

unread,
Oct 8, 2022, 11:00:21 AM10/8/22
to emscripten-discuss
Could anyone provide a sample code for creating a socket server code that can also work from web browsers and accept incoming connections?

Floh

unread,
Oct 10, 2022, 7:22:14 AM10/10/22
to emscripten-discuss
I might be wrong but I don't think that's possible. First, you can't expose a TCP-compatible socket in browsers (Emscripten's TCP API is an emulation wrapper over WebSockets, which are not protocol-compatible with 'raw' TCP).

WebSocket in the browser can only implement a client interface (e.g. it cannot accept incoming connections, only connect to a 'native' WebSocket server).

WebRTC data channels might allow to implement something that looks like a server running in a browser (because it's a peer-to-peer connection system), but you couldn't connect with a regular TCP or WebSocket client, only with a client that also uses the WebRTC data channel protocol.

Floh

unread,
Oct 10, 2022, 7:22:54 AM10/10/22
to emscripten-discuss
typo: "Emscripten's TCP API" should better be "Emscripten socket API".

Sam Clegg

unread,
Oct 10, 2022, 10:39:52 AM10/10/22
to emscripte...@googlegroups.com
On Mon, Oct 10, 2022 at 4:22 AM Floh <flo...@gmail.com> wrote:
I might be wrong but I don't think that's possible. First, you can't expose a TCP-compatible socket in browsers (Emscripten's TCP API is an emulation wrapper over WebSockets, which are not protocol-compatible with 'raw' TCP).

I believe it can be done, but it requires running a proxy server on your webserver.  See  https://emscripten.org/docs/porting/networking.html#full-posix-sockets-over-websocket-proxy-server.

Aside from those docs, you might want to look at the test code that we have for `-sPROXY_POSIX_SOCKETS`.     See `test_posix_proxy_sockets` in `test/test_sockets.py`.  

A word of warning: I'm not sure how much testing we have for this mode in terms of actual usage in production, so be prepared for some rough edges, and perhaps then need to write some patches.




WebSocket in the browser can only implement a client interface (e.g. it cannot accept incoming connections, only connect to a 'native' WebSocket server).

WebRTC data channels might allow to implement something that looks like a server running in a browser (because it's a peer-to-peer connection system), but you couldn't connect with a regular TCP or WebSocket client, only with a client that also uses the WebRTC data channel protocol.

On Saturday, 8 October 2022 at 17:00:21 UTC+2 aitha...@gmail.com wrote:
Could anyone provide a sample code for creating a socket server code that can also work from web browsers and accept incoming connections?

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/9c6ce49b-f7f7-47d0-89b2-3ccc9bfb3de6n%40googlegroups.com.
Message has been deleted

Aitha Tarun

unread,
Oct 10, 2022, 11:04:12 AM10/10/22
to emscripten-discuss
Ok thanks for the reply
Reply all
Reply to author
Forward
0 new messages