Skip to main content
The WebAssembly (WASM) bindings provide native XMTP functionality for browser-based applications. These bindings compile the core LibXMTP Rust library to WebAssembly, enabling secure messaging directly in the browser without a backend server.
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:

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-unknown target
  • Emscripten for WASM compilation
  • LLVM (for emscripten dependency)
  • Node.js and Yarn

Setup

Build Commands

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 in test/:
  • client.test.ts - Client creation and management
  • Conversations.test.ts - Conversation operations
  • EnrichedMessage.test.ts - Content types
  • RemoteAttachmentEncryption.test.ts - Attachment handling
  • opfs.test.ts - OPFS storage tests
  • errorCodes.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:
  1. Update version in package.json
  2. Merge to main branch
  3. Manually trigger “Release WASM Bindings” workflow
The workflow will:
  • 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