Skip to content

Add options for custom WebSocket constructor and dynamic URL selection for emulated POSIX TCP sockets - #28226

Open
ading2210 wants to merge 1 commit into
emscripten-core:mainfrom
MercuryWorkshop:feature-websocket-contructor
Open

ading2210 wants to merge 1 commit into
emscripten-core:mainfrom
MercuryWorkshop:feature-websocket-contructor

Conversation

@ading2210

@ading2210 ading2210 commented Nov 11, 2026 •

Copy link
Copy Markdown

This PR adds new features to the POSIX socket emulation over WebSockets. This is a very small change that allows for a great deal of flexibility in Emscripten's networking.

It adds the ability to change the WebSocket constructor to any custom class/function, by changing Module['websocket']['constructor'];. For example:

Module['websocket']['constructor'] = CustomWebSocket;

Or if we want to control the arguments passed into the constructor:

const custom_net_options = {...};
Module['websocket']['constructor'] = function() {
  return new CustomWebSocket(custom_net_options);
}

This lets us change the underlying network transport for the socket to whatever we want, so long as a WebSocket-compatible class with the same API surface is used. For instance, you could create a wrapper for WebTransport data channels, WebRTC data channels, or direct sockets.

This PR also adds the ability to make Module['websocket']['url'] a function rather than a string, which maps the hostname and port to a URL. For instance, you could use:

Module['websocket']['url'] = (host, port) => `wss://example.com/proxy/${host}:${port}`;

In this example, it would cause every TCP socket opened to create a new WebSocket with the hostname and port in the URL path.

I'm making this change because I've encountered a lot of uses cases where I'd want to write a custom network transport for Emscripten programs. I'd usually have to intercept the global window.WebSocket object or patch Emscripten's emitted JS code. I could avoid all of that if the features described in this PR are merged.

@ading2210 ading2210 changed the title Add options for custom WebSocket constructor and dynamic URL selection Add options for custom WebSocket constructor and dynamic URL selection for emulated POSIX TCP sockets Nov 11, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds support for dynamically resolving the WebSocket URL via a function and customizing the WebSocket constructor at runtime, updating both the implementation in libsockfs.js and the documentation. The review comments point out a critical issue where runtime changes to Module['websocket'] may not be detected if it was not defined at startup due to decoupling from SOCKFS.websocketArgs. It is recommended to use Module['websocket'] || SOCKFS.websocketArgs to correctly resolve these properties at runtime.

Comment thread src/lib/libsockfs.js Resolved
Comment thread src/lib/libsockfs.js Resolved
@ading2210
ading2210 force-pushed the feature-websocket-contructor branch from ae47e50 to 0c33c0f Compare November 11, 2026 01:19
@ading2210
ading2210 force-pushed the feature-websocket-contructor branch from 0c33c0f to 0aab669 Compare November 11, 2026 01:21
@ading2210

Copy link
Copy Markdown
Author

All the Gemini comments have been resolved.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant