These bindings are not intended to be used directly. Use the xmtp-js SDK instead, which provides a higher-level API built on top of these bindings.
Installation
The bindings are published to npm as@xmtp/node-bindings:
Requirements
- Node.js >= 22
- Platform-specific native binaries are automatically downloaded during installation
Architecture
The Node.js bindings use NAPI-RS to create high-performance native Node.js addons from Rust code.Key Technologies
- NAPI-RS: Rust framework for building Node.js addons
- Native Modules: Platform-specific
.nodefiles containing compiled Rust code - Type Definitions: Auto-generated TypeScript
.d.tsfiles - Tokio Integration: Async Rust code integrates with Node.js event loop
Code Organization
The Rust source code follows a modular structure organized by domain:Error Handling
Rust errors are converted to JavaScript errors using theErrorWrapper utility:
NAPI Attributes
The bindings use NAPI-RS procedural macros to expose Rust to JavaScript:#[napi]on struct/impl - exports to JavaScript as a class#[napi(object)]- exports as a plain JavaScript object#[napi(getter)]- exports as a property getter#[napi]on method - exports as a class method
Basic Usage
Here’s a basic example of creating a client and sending a message:Development
Prerequisites
For development, you need:- Rust toolchain (managed via Nix or rustup)
- Node.js >= 22
- Yarn package manager
Setup
From the repository root:Build Commands
Linting and Formatting
Testing
Running Tests
Tests are written in TypeScript using Vitest:Test Structure
Tests are located in thetest/ directory:
Client.test.ts- Client creation and managementConversations.test.ts- Listing and creating conversationsConversation.test.ts- Sending messages, managing membersEnrichedMessage.test.ts- Message content typesRemoteAttachmentEncryption.test.ts- Attachment handling
Example Test
Package Information
Frompackage.json:
Key Dependencies
- Runtime
- Development
napi/napi-derive- Node.js FFI bindingsxmtp_mls- Core MLS implementationxmtp_db- Database layer with encrypted SQLitexmtp_proto- Protocol buffer definitionsxmtp_api_grpc- gRPC API client
Advanced Patterns
BigInt for Timestamps
Nanosecond timestamps use JavaScript BigInt to avoid precision loss:Stream Handling
Streams return async iterables:Content Types
The bindings support various content types:Performance Considerations
Zero-Copy Data Transfer
NAPI-RS enables zero-copy transfer for:- Buffer/Uint8Array data
- Large strings
- Binary protocol messages
Async Performance
Rust async operations integrate with Node.js event loop:- No blocking of JavaScript thread
- Efficient multi-threaded execution in Rust
- Automatic backpressure handling for streams
Database Performance
The bindings use encrypted SQLite with:- Connection pooling
- Prepared statement caching
- Write-ahead logging (WAL) mode
Troubleshooting
Module Not Found
If you see “Cannot find module” errors:Platform-Specific Issues
Native binaries are platform-specific. Ensure you’re using the correct binary for your OS:bindings_node.darwin-arm64.node- macOS Apple Siliconbindings_node.darwin-x64.node- macOS Intelbindings_node.linux-x64-gnu.node- Linux x64bindings_node.win32-x64-msvc.node- Windows x64
Database Errors
If you encounter database errors:Resources
NAPI-RS Documentation
Learn about the NAPI-RS framework
Source Code
View the bindings source code
XMTP-JS SDK
Use the high-level JavaScript SDK
Example Tests
See real usage examples
