Skip to content
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

[RFC] WASM #2191

Closed
wants to merge 4 commits into from
Closed

[RFC] WASM #2191

wants to merge 4 commits into from

Conversation

Jorropo
Copy link
Contributor

@Jorropo Jorropo commented Mar 16, 2023

I would like to run code in the browser however I hate JS and JS hates me.

It would be nice if go-libp2p would magically work in the browser (with gopherjs and or GOOS=js GOARCH=wasm).

IO would be provided in a degrated mode supporting websocket. (https://pkg.go.dev/nhooyr.io/websocket#hdr-Wasm)
We could also do webtransport and webrtc but we would need to write the syscall/js wrapping code ourselves which might be more or less complex.

One potential issue which makes this less straight forward than it may seems, is that even if the big required elements like WS are reachable, the options that can be applied are way less diverse making some low level config impossible.


Current status of this PR:

  • Todo:
    • Websocket implementation brought back from the dead.
      • I don't like it, the code isn't good because the websocket API really sucks (no backpressure, callback based).
        It's probably worth removing it and relying exclusively on webtransport as long as https://developer.chrome.com/articles/websocketstream/ is not a thing. (we could also use a JS polyfill mapping websocket to the streaming API that webtransport is also using).
    • New Webtransport syscall/js implementation
      • Tests
    • WebRTC
  • Examples:
    • Chat example !
    • Kad DHT
      • It compiles and runs on my machine but very quickly crashes chrome but the DHT doesn't work properly due to the lack of publicly announced webtransport addresses in the wild + a limit of 6 concurrent webtransport connections imposed by the browser.
  • Browser support:

@p-shahi
Copy link
Member

p-shahi commented Mar 16, 2023

Supporting Wasm hasn't come up in any discussions thusfar, but why go-libp2p vs the Wasm support that rust-libp2p provides?

@Jorropo
Copy link
Contributor Author

Jorropo commented Mar 16, 2023

Because I'm bad at rust (/ want to use it with the rest of my go code, like go-libipfs).

@Jorropo
Copy link
Contributor Author

Jorropo commented Mar 16, 2023

The FX issue is fixed in master just unreleased: uber-go/fx@5e90c38

@Wondertan
Copy link
Contributor

Celestia would support these efforts. We have a notion of Light Nodes that can access the chain state in trust minimized manner, and we want to experiment with running them in the browser over WebTransport/RTC. We don't have a rust implementation of the protocol and do not plan to have it soon, so Golang would be appreciated, even considering how inefficient it is.

@MarcoPolo
Copy link
Collaborator

@Jorropo how much of go-libipfs compiles to wasm? What about kad and pubsub?

I have some thoughts I've been drafting about a need to be able to compile protocols into a wasm module. Then have plumbing to allow every implementation to use the same wasm protocol module. I'll link them here when they're ready.

As for this, it's definitely cool but I'm expecting not practical besides for demos. If you do get it to compile, my guess is that the bundled size will be pretty big (I hope I'm wrong!). So I don't expect core go-libp2p maintainers to spend effort maintaining this anytime soon.

@Jorropo
Copy link
Contributor Author

Jorropo commented Mar 16, 2023

how much of go-libipfs compiles to wasm? What about kad and pubsub?

I would guess they work out of the box, given they don't call any io themselves.

In general, anything compiles to wasm, the language is fully implemented, however the API you have access to are pretty limited.
In theory if we fix direct io based stuff (transports, local interfaces discovery, ...) then any libp2p would work out of the box.

the bundled size will be pretty big

You are probably right, however I don't think the bundle size maters much for let's say a browser application that implements a gateway, you download it once and then keep it cached.

@Jorropo Jorropo force-pushed the wasm branch 6 times, most recently from 9e16381 to 94ef3b6 Compare May 20, 2023 11:50
This was surpsingly easy there is a high chance I messed something up.

This is a revert of 2d53c8f and then a
bunch of fixes to bring it up to speed with the latest implementation.

The fx change is related to this: uber-go/fx#1022
@Jorropo Jorropo force-pushed the wasm branch 2 times, most recently from 6fdaeba to accd448 Compare May 21, 2023 00:38
@MarcoPolo
Copy link
Collaborator

Closing the PR as part of some spring cleaning. Still excited for this though :)

@MarcoPolo MarcoPolo closed this Jun 3, 2024
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.

4 participants