New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
websocket_websys
does not work in NodeJS environments
#5024
Comments
Shiming with |
I continued digging and found that the "polyfill" worked. This is no real solution, but it seems not as hard to support as expected. // @ts-ignore
global.WebSocket = require('isomorphic-ws');
const globalSetInterval = global.setInterval;
// @ts-ignore
global.setInterval = (...args) => {
// @ts-ignore
let timeout = globalSetInterval(...args);
return timeout[Symbol.toPrimitive]();
}
// @ts-ignore
global.WorkerGlobalScope = global; Setting
|
Thank you for opening this. We don't currently have or test support for NodeJS and I am not entirely sure we want to support it in a first-class way. How do other npm packages solve this problem? Do they detect that they are within NodeJS and use the appropriate APIs available? it is been a while that I did JS development but I'd see the responsibility on the NodeJS side to offer Web-compatible APIs. In other words, go with the polyfill method that you've already found to be working. |
In multi-context environments, packages usually use I have no problem adding that to the global scope, but the other hacks to make In other words, I think what would need to happen is:
Usually, I'd agree with you here, and this would be something I'd expect as well (figuring this out surprised me), but this is the typical problem with JavaScript, the language (ECMAScript), and JavaScript's implementation. Still, the usage of NodeJS in the JS ecosystem warrants some special casing, especially if the changes required are relatively small. |
Yes, I think we've done something similar in the past actually. I can't find the piece of code now but I thought that we somehow managed to carry the interval handle as an opaque object. We might have to define our own bindings so that we can say it returns a |
Perhaps even easier than the above, if we can reliably detect NodeJS, then we can just do the "right" thing here: rust-libp2p/transports/websocket-websys/src/web_context.rs Lines 34 to 56 in b6bb02b
|
Thanks for the input. I have already created a PR, which should solve the problem using an approach similar to your suggested one! |
Summary
I am currently trying to write a module that relies on
libp2p
with a websocket connection between the client (uses WASM) and server (uses normal Rust), while doing so I have realized that thewebsocket_websys
package seems to be incompatible with the nodejs target, considering thatWebSocket
is not natively available. I receive the errorOutgoingConnectionError { connection_id: ConnectionId(1), peer_id: None, error: Transport([("/ip4/127.0.0.1/tcp/4088/ws", Other(Custom { kind: Other, error: Other(Right(Left(Left(Error { msg: "Invalid websocket url: ws://127.0.0.1:4088/" })))) }))]) }
. I believe this is because the API simply doesn't exist.Trying to crudely shim API does not help. Any help would be appreciated (or alternative nodejs compatible transports)
Expected behavior
Able to connect via websockets between server and client.
Actual behavior
Error out when trying to import with WASM seemingly crashing (?)
Relevant log output
No response
Possible Solution
Referencing the
ws
crate instead ofWebSocket
would be a possibility (orisomorphic-ws
), but that would require external dependencies.Version
0.53.2
Would you like to work on fixing this bug ?
Yes
The text was updated successfully, but these errors were encountered: