WASM (browser) <-> desktop not working

Hello everyone, I’m trying to setup a connection between a browser (WASM) and a desktop client using websockets as the transport, this is what I have so far.

With all this in place, when the browser client tries to connect to the desktop client I start getting errors in the browser console:

Uncaught (in promise) TypeError: Illegal invocation
    at __wbg_clearTimeout_5b4145302d77e5f3 (app.js:921:128)
    at gloo_timers::callback::clear_timeout::he3ba6cd2af4169c5 (504de782943406928503.module.wasm:0x602c2c)
    at <gloo_timers::callback::Timeout as core::ops::drop::Drop>::drop::h8acab6ff95da9e30 (504de782943406928503.module.wasm:0x5b0f89)
    at core::ptr::drop_in_place<gloo_timers::callback::Timeout>::ha8c67498ae8bdbd9 (504de782943406928503.module.wasm:0x5fc61a)
    at core::ptr::drop_in_place<gloo_timers::future::TimeoutFuture>::h6b5b1c7dd8b208d5 (504de782943406928503.module.wasm:0x5fc58d)
    at core::ptr::drop_in_place<alloc::boxed::Box<gloo_timers::future::TimeoutFuture>>::h02b74368ff432dac (504de782943406928503.module.wasm:0x5fac58)
    at <send_wrapper::SendWrapper<T> as core::ops::drop::Drop>::drop::h37d540aba2b1854b (504de782943406928503.module.wasm:0x55b01d)
    at core::ptr::drop_in_place<send_wrapper::SendWrapper<gloo_timers::future::TimeoutFuture>>::hd6e1468839135512 (504de782943406928503.module.wasm:0x614555)
    at core::ptr::drop_in_place<futures_timer::wasm::Delay>::hb662a28372717eda (504de782943406928503.module.wasm:0x614521)
    at core::ptr::drop_in_place<libp2p_ping::handler::Handler>::h1674f59f62d20874 (504de782943406928503.module.wasm:0x5aa5e6)

This seems to be related to the usage of futures_timer::Delay and instant::Instant in libp2p-ping, replacing them with the wasm_timer equivalents these errors are fixed. There are other errors related to timeouts and instant.now in other crates, but again replacing with wasm_timer equivalents fixes them.

I’ve seen that in this PR wasm-timer support was removed and as I understand it all should be working fine without it. So, what am I missing, how can I make this simple example work with the current code without reverting to use wasm_timer? Any help is more than welcome :slight_smile: thanks!

To give more context (I only could add 2 links in the first post), the repo I’m working on is based on this one, but bumping libp2p to the latest version (with all the required changes for this, SECIO → noise, etc… and using the ping behaviour instead of gossipsub). With all this in place I get the error above.

These are the changes needed to make the example work (browser dials to desktop, browser and desktop start using ping behaviour), basically reverting the removal of wasm_timer in all the code that is used by the example app.

Forgot to mention, this refers to libp2p’s rust implementation. My team relies on this functionality to work, and we would not like to have to maintain a patch. I was thinking about proposing the changes that we have verified that make things work (ie. using wasm_timer functionality when the target is wasm32-unknown-unknown) and putting the changes behind a feature flag so that no changes are applied unless this feature flag is enabled.

But I’m not completely sure if this behaviour (I mean, the errors running on the browser) is expected, if reverting to use wasm_timer in the browser is ok… Could anyone please advice about this?

Solved here how to run libp2p in the browser · Discussion #2981 · libp2p/rust-libp2p · GitHub