These bindings are low-level interfaces. For most browser development, use the xmtp-js SDK instead, which provides a higher-level API built on top of these bindings.
Installation
The bindings will be published to npm as@xmtp/wasm-bindings:
Requirements
- Modern browser with WebAssembly support
- ES2020+ JavaScript environment
- Web Worker support (for OPFS and background operations)
Architecture
The WASM bindings compile Rust code to WebAssembly using wasm-bindgen.Key Technologies
- wasm-bindgen: Rust/WASM/JS interop framework
- wasm-pack: Build tool for WASM modules
- OPFS: Origin Private File System for encrypted local storage
- Web Workers: Background execution for crypto operations
- Emscripten: Toolchain for compiling Rust to WASM
Browser Storage
The WASM bindings support multiple storage backends:- OPFS (Recommended)
- Ephemeral
Origin Private File System - Modern, performant file storage
- Persistent encrypted SQLite database
- Runs in Web Worker for better performance
- Requires modern browser support
- No storage limits (quota-based)
Code Organization
The WASM bindings share most code with Node.js bindings but with platform-specific implementations:Basic Usage
Initialization
WASM modules must be initialized before use:Creating a Client
Working with Conversations
Streaming Conversations
Development
Prerequisites
For development, you need:- Rust toolchain with
wasm32-unknown-unknowntarget - Emscripten for WASM compilation
- LLVM (for emscripten dependency)
- Node.js and Yarn
Setup
Build Commands
- macOS
- Just (Recommended)
- Standard
Linting and Formatting
Testing
Unit Tests (Rust)
Run Rust tests compiled to WASM:Integration Tests (TypeScript)
Run browser-based integration tests:Test Structure
Integration tests intest/:
client.test.ts- Client creation and managementConversations.test.ts- Conversation operationsEnrichedMessage.test.ts- Content typesRemoteAttachmentEncryption.test.ts- Attachment handlingopfs.test.ts- OPFS storage testserrorCodes.test.ts- Error handling
Example Test
Advanced Patterns
OPFS Storage
For persistent storage, use OPFS in a Web Worker:Error Handling
Streaming with ReadableStream
Authentication Callbacks
Performance Considerations
Bundle Size
WASM bindings are compiled to a binary module:- Base WASM module: ~2-4 MB (compressed)
- Code splitting recommended for large apps
- Use dynamic imports to lazy-load bindings
Web Workers
For better performance, run WASM in a Web Worker:- Keeps crypto operations off main thread
- Required for OPFS storage
- Enables true background processing
Memory Management
- WASM memory grows dynamically
- Objects are garbage collected
- Streams should be properly closed
Browser Compatibility
Required Features
- WebAssembly (all modern browsers)
- BigInt support (Chrome 67+, Firefox 68+, Safari 14+)
- ES2020+ (async/await, optional chaining)
Optional Features
- OPFS (Chrome 102+, Edge 102+) - for persistent storage
- Web Workers (all modern browsers) - for background operations
Polyfills
For older browsers, you may need:Troubleshooting
WASM Module Failed to Load
Ensure your bundler serves.wasm files correctly:
Memory Errors
If you see “out of memory” errors:OPFS Not Available
OPFS requires a secure context (HTTPS):Publishing
To release a new version:- Update version in
package.json - Merge to main branch
- Manually trigger “Release WASM Bindings” workflow
- Build WASM bindings for all platforms
- Run tests
- Publish to npm
Resources
wasm-bindgen Book
Learn about Rust/WASM/JS interop
Source Code
View the bindings source code
XMTP-JS SDK
Use the high-level JavaScript SDK
OPFS Documentation
Learn about browser file storage
