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

Enable NATS Client to Run in WebAssembly #1098

Open
andrew-goldie opened this issue Aug 22, 2023 · 9 comments
Open

Enable NATS Client to Run in WebAssembly #1098

andrew-goldie opened this issue Aug 22, 2023 · 9 comments
Labels
enhancement Enhancement to existing functionality

Comments

@andrew-goldie
Copy link

Use case

Deploying lightweight NATS-based microservices on edge devices is a compelling architecture for many applications and probably represents the most significant architecture of the near future. Some immediate benefit is possible using Docker or natively-compiled apps, however a compile-once-run-anywhere approach would be, well... awesome. The promise of server-side WebAssembly combined with NATS would give the necessary building blocks.

Currently nats-async will not compile to WASM and this feature request is for it to do so.

Does the current state of WASI allow a review of nats.rs to compile to WASM, or is it just not there yet? Timeframe and dependencies aside, it would be great for this feature to be road-mapped.

Proposed change

Review code and dependencies to target WASM/WASI runtime.

Who benefits from the change(s)?

Programmers wanting to deploy lightweight NATS nodes across a range of hosts with a common compilation target.

Alternative Approaches

Solutions such as WasmCloud provide a way of connecting WASM targets with NATS, but the NATS providers sit outside the assembly and require the proprietary centralised architecture to construct solutions (setting aside the high-friction developer experience). I'm not aware of other innovations in this space.

@andrew-goldie andrew-goldie added the enhancement Enhancement to existing functionality label Aug 22, 2023
@Jarema
Copy link
Member

Jarema commented Aug 23, 2023

Hey!
Thanks for filling in the issue.

There is some context in this discussion: #1045

Not impossible, but not trivial right now with the current state of WASI.

@andrew-goldie
Copy link
Author

Thanks, @Jarema. Understand the state of play with WASI not being implemented broadly nor even mature enough as a spec to do all the things we dream of. We are investing a lot in NATS-with-Rust (most enjoyable!) on existing platforms and will watch the emergence of WASM/WASI runtimes with interest.

@Jarema
Copy link
Member

Jarema commented Aug 31, 2023

We also keep our eye on that topic, as it definately opens many possibilities.

@ahirner
Copy link

ahirner commented Jan 16, 2024

context in this discussion: https://github.com/nats-io/nats.rs/discussions/1045nats over websocket

States that websockets would be more amendable across runtimes.

Not impossible, but not trivial

I'd suggest to expose a trait that handles NATS transport. I haven't yet looked into this client, but there is precedence in python for such abstraction: nats-io/nats.py#356

After this trait, the current impl becomes the default impl based on a default feature flag. If this feature was turned off, the crate would compile for wasm32-unknown by itself. Then some WASI based client, websocket clients etc. may require their own depencencies. Those other impls could live in this repo or outside, however maintainers desire.

The trait doesn't need to be 100% stable anyway (cf. async traits), but it would be nice if it turns out comparibly small and well designed.

How does that sound?

@Jarema
Copy link
Member

Jarema commented Jan 16, 2024

Websockets is already our plan, so that could be a opportunity to work on the transport trait.
That should be probably design together with more flexibility with picking TLS implementation to enable ESM32 usage and others.

@wbaileyisla
Copy link

Isn't QUIC or a custom quic implementation like QUINN able to cover the majority of these needs + fallback?

@Jarema
Copy link
Member

Jarema commented Apr 6, 2024

@wbaileyisla
First of all, QUIC runs over UDP, so it does not change much in the WebAssembly story.

Secondly, NATS is based on TCP and does not (yet) support QUIC.

And most importantly NATS can also run over WebSockets, which are doable (if you for some reason want to do IO through WebAssembly) in WebAssembly.

Thats why WebSocket transport support is the most straightforward way here ;).

@wbaileyisla
Copy link

https://developer.mozilla.org/en-US/docs/Web/API/WebTransport_API

Can we at least have nice things and support the webtransport? I saw you on the other quic thread some time ago, but I'm not sure I saw web transport ever talked about? Hope you're well!

@Jarema
Copy link
Member

Jarema commented Apr 6, 2024

@wbaileyisla we're discussing here how to run nats.rs client in WebAssembly. And we can do it without any changes to nats-server itself, by just supporting WebSockets, which are also suported by nats-server.

Supporting WebTransport protocol is a whole different discussion, which probably is of scope of NATS v3, with new protocol, new tranport layer, etc. I doubt it makes sense.

However Quic does make sense and we do consider it. That, however, requires server support first and there is no proof so far that it would be quicker (beyond tls handshake).

Hopw youre doing well too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement to existing functionality
Projects
None yet
Development

No branches or pull requests

4 participants