WASM bindings

As I’m working through developing a Python SDK for XMTP, I’ve gotten to the point that I’m needing to interface with libxmtp. I see there are already WASM bindings available and obviously being able to just use something like wasmtime to run the necessary WASM modules in Python is (in theory at least) far preferable to writing and maintaining new bindings.

My question is, how stable is that part of the repo? I see that it’s still labelled as in progress but obviously “in progress” can mean a lot of different things. Thanks!

3 Likes

Hey @c-georgen ! Great Question

We strive to have first class support for the browser via our WebAssembly bindings. Meaning our WebAssembly bindings should receive the same attention and support as Android, iOS & NodeJS Bindings. The bindings currently maintain feature-parity with android/ios/NodeJS save for an encrypted at-rest database on the web, which has its own challenges.

I love the thought of using wasmtime to access libxmtp features from languages not officially supported by the XMTP Sdks and Ephemera team. The discussion has come up a couple times of using wasmtime to interface with the SDK (for our use cases from Go), although no work has been done to make that possible.

Our bindings are currently targeting wasm32-unknown-unknown, a target which was chosen to work well for the web. Because of this target choice, the libxmtp library needs to make some adjustments (like avoiding multithreaded work and using a sqlite wasm build - GitHub - Spxg/sqlite-wasm-rs: Provide sqlite solution for wasm32-unknown-unknown target.)

I haven’t done any kind of spikes or experiments with wasmtime and libxmtp to give you a great answer on the kind of work needed to get it working with that VM properly. However, I can give you a rough outline of what it would take:

There’s probably two paths to take: create new bindings for wasm32-wasip2 or attempt to re-use wasm32-unknown in wasmtime.

for wasip2:
the xmtp_mls library would need to be compiled for wasm32-wasip2. I’m not sure what kind of support wasmtime has for the web standard and wasm32-unknown-unknown. AFAIK wasmtime is built around the WASI standard, which differs from wasm environments on the web. For best perf/support, it would probably be ideal to create xmtp_mls bindings specifically for wasip2.

The biggest challenge for compiling to wasip2 is probably getting sqlite and diesel to compile for wasip2. However, I’m not sure it will be as much of a challenge as wasm32-unknown, since rusqlite already supports compilation to the wasi webassembly target, and what seems like a wasi vfs. So there should be no need to alter the core xmtp_mls libraries

For Web:
the current wasm bindings depend on some specific browser apis to manage networking and the filesystem (mostly OPFS and POST requests/streams) via the web-sys crate. In order to re-use wasm32-unknown-unknown, These apis would need to either exist in wasmtime already or some kind of shim would need to be created to access them.