API Methods
Constructor
Lifecycle Methods
| Method | Returns | Description |
|---|---|---|
start() | Promise<void> | Start the protocol. Auto-initializes MLS if encryption is enabled, starts BLE scanning/advertising |
stop() | Promise<void> | Stop the protocol and disconnect all peers |
pause() | Promise<void> | Pause for background mode |
resume() | Promise<void> | Resume from paused state |
destroy() | Promise<void> | Clean up all resources |
getState() | Promise<ProtocolState> | Get current state (Stopped, Running, Paused) |
emitTestEvent() | Promise<void> | Emit a test event to verify event system is working |
Messaging
| Method | Returns | Description |
|---|---|---|
sendMessage(params) | Promise<string> | Send message, returns message ID |
receiveMessage() | Promise<MessageReceivedEvent | null> | Poll for next received message |
forwardMessage(params) | Promise<string> | Forward message with original sender attribution |
Connection Requests
| Method | Returns | Description |
|---|---|---|
sendConnectionRequest(params) | Promise<string> | Send a connection request to a peer |
acceptConnectionRequest(params) | Promise<string> | Accept a connection request |
rejectConnectionRequest(params) | Promise<string> | Reject a connection request |
cancelConnectionRequest(params) | Promise<string> | Cancel a previously sent request |
Media & File Transfer
| Method | Returns | Description |
|---|---|---|
sendMedia(params) | Promise<string> | Send media (image, video, audio, file) — returns file ID |
sendFile(params) | Promise<string> | Send a generic file (convenience wrapper) |
sendImage(recipient, fileData, fileName, metadata?) | Promise<string> | Send an image |
sendVoiceNote(recipient, fileData, fileName, metadata?) | Promise<string> | Send a voice note |
sendVideoNote(recipient, fileData, fileName, metadata?) | Promise<string> | Send a video note |
sendVideo(recipient, fileData, fileName, metadata?) | Promise<string> | Send a video |
getFileProgress(fileId) | Promise<FileProgress | null> | Get file transfer progress |
cancelFileTransfer(fileId) | Promise<boolean> | Cancel an active transfer |
processFileChunk(...) | Promise<void> | Process a file chunk (custom handling) |
finalizeFile(fileId) | Promise<void> | Finalize a file transfer after all chunks |
Transport Management
| Method | Returns | Description |
|---|---|---|
getActiveTransports() | Promise<TransportType[]> | Get list of active transports |
enableTransport(type, config?) | Promise<void> | Enable a transport |
disableTransport(type) | Promise<void> | Disable a transport |
forceTransport(type) | Promise<void> | Force specific transport (override DORS) |
releaseTransportLock() | Promise<void> | Release forced transport, let DORS decide |
getTransportMetrics(type) | Promise<TransportMetrics | null> | Get transport statistics |
Bluetooth
| Method | Returns | Description |
|---|---|---|
isBluetoothEnabled() | Promise<boolean> | Check if Bluetooth is enabled |
requestEnableBluetooth() | Promise<boolean> | Request to enable Bluetooth (Android only) |
getBLePeerCount() | Promise<number> | Get number of discovered BLE peers |
Network Topology
| Method | Returns | Description |
|---|---|---|
getTopology() | Promise<NetworkTopology> | Get network topology snapshot |
getMessageStats() | Promise<MessageDeliveryStats[]> | Get message delivery statistics |
getDeliverySuccessRate() | Promise<number> | Get delivery success rate (0-1) |
getMedianLatency() | Promise<number | null> | Get median latency in ms |
getMedianHops() | Promise<number | null> | Get median hop count |
Battery & Relay
| Method | Returns | Description |
|---|---|---|
setBatteryLevel(level) | Promise<void> | Set battery level (0-100) for mesh decisions |
getBatteryLevel() | Promise<number | null> | Get current battery level |
setRelayPriority(priority) | Promise<void> | Set relay priority ('low', 'medium', 'high') |
getRelayPriority() | Promise<string> | Get current relay priority |
isRelay() | Promise<boolean> | Check if device is acting as a relay |
DORS Configuration
| Method | Returns | Description |
|---|---|---|
updateDorsConfig(config) | Promise<void> | Update DORS settings at runtime |
getDorsConfig() | Promise<DorsConfig> | Get current DORS configuration |
shouldEscalateToWifi() | Promise<boolean> | Check if DORS recommends WiFi escalation |
Reliability Configuration
| Method | Returns | Description |
|---|---|---|
updateAckConfig(config) | Promise<void> | Update ACK settings |
updateRetryConfig(config) | Promise<void> | Update retry settings |
updateDedupConfig(config) | Promise<void> | Update deduplication settings |
getDedupStats() | Promise<DedupStats> | Get deduplication statistics |
getPendingAckCount() | Promise<number> | Get pending ACK count |
getRetryQueueSize() | Promise<number> | Get retry queue size |
Gradient Routing
| Method | Returns | Description |
|---|---|---|
learnRoute(destination, nextHop, hopCount, quality, sequenceNumber?) | Promise<void> | Learn a route from incoming message |
getBestRoute(destination) | Promise<RouteEntry | null> | Get best route to destination |
getAllRoutes(destination) | Promise<RouteEntry[]> | Get all routes to destination |
hasRoute(destination) | Promise<boolean> | Check if route exists |
removeNeighborRoutes(neighborId) | Promise<void> | Remove routes through neighbor |
cleanupExpiredRoutes() | Promise<void> | Clean up expired routes |
getRoutingStats() | Promise<RoutingStats> | Get routing table statistics |
updateRoutingConfig(config) | Promise<void> | Update routing configuration |
End-to-End Encryption (MLS)
MLS is automatically initialized whenstart() is called (if encryption.enabled is true). These methods provide manual control over encryption operations.
Session Management
| Method | Returns | Description |
|---|---|---|
initializeMlsWithSecureStorage() | Promise<void> | Init MLS with platform secure storage (auto-called by start()) |
isMlsInitialized() | Promise<boolean> | Check if MLS is ready |
establishSecureSession(peerId) | Promise<MlsWelcome | null> | High-level: establish session with peer (recommended) |
mlsHasSession(otherUserId) | Promise<boolean> | Check if session exists |
hasPendingKeyPackage(peerId) | Promise<boolean> | Check if peer’s key package is available |
getEstablishmentState(peerId) | Promise<EstablishmentState> | Get session establishment state |
mlsListSessions() | Promise<string[]> | List all active session user IDs |
mlsDeleteSession(otherUserId) | Promise<void> | Delete a session |
Key Package Management
| Method | Returns | Description |
|---|---|---|
mlsGenerateKeyPackage() | Promise<MlsKeyPackage> | Generate a new key package |
mlsGetOrCreateKeyPackage() | Promise<MlsKeyPackage> | Get existing or create new key package |
mlsGetPendingKeyPackages() | Promise<MlsKeyPackage[]> | Get unsynced key packages |
mlsMarkKeyPackageSynced(packageId) | Promise<void> | Mark key package as synced |
mlsImportKeyPackage(userId, data) | Promise<void> | Import a peer’s key package |
Low-Level Session Operations
| Method | Returns | Description |
|---|---|---|
mlsCreateSession(otherUserId) | Promise<MlsWelcome> | Create session (requires imported key package) |
mlsJoinSession(welcome) | Promise<MlsSessionInfo> | Join session from Welcome message |
mlsEncryptForUser(otherUserId, plaintext) | Promise<MlsEncryptedMessage> | Encrypt message for user |
mlsDecryptFromUser(encrypted) | Promise<number[] | null> | Decrypt message from user |
mlsDecrypt(encrypted) | Promise<number[] | null> | Decrypt any MLS message (1:1 or group) |
mlsProcessWelcome(welcome) | Promise<MlsSessionInfo | MlsGroupInfo> | Process Welcome (auto-detects type) |
Identity & Signing
| Method | Returns | Description |
|---|---|---|
getIdentityPublicKey() | Promise<number[]> | Get Ed25519 public key (32 bytes) |
deriveUserIdFromPublicKey(publicKey) | Promise<string> | Derive user ID from public key (base58) |
signData(data) | Promise<number[]> | Sign data with identity key (64 byte signature) |
verifySignature(publicKey, data, signature) | Promise<boolean> | Verify an Ed25519 signature |
Trust
| Method | Returns | Description |
|---|---|---|
resetTofuForPeer(peerId) | Promise<boolean> | Reset TOFU trust pin for a peer |
Group Messaging
Groups use MLS for end-to-end encryption with mesh transport for delivery.| Method | Returns | Description |
|---|---|---|
meshCreateGroup(groupName) | Promise<MlsGroupInfo> | Create a new encrypted group |
meshInviteToGroup(groupId, inviteeUserId) | Promise<void> | Invite a user to a group |
meshSendGroupMessage(groupId, content, priority?, replyToMsg?) | Promise<string[]> | Send message to all members (returns per-member IDs) |
meshForwardMessageToGroup(params) | Promise<string[]> | Forward message to group with attribution |
meshRemoveFromGroup(groupId, memberId) | Promise<void> | Remove a member from a group |
meshLeaveGroup(groupId) | Promise<void> | Leave a group |
meshListGroups() | Promise<string[]> | List all group IDs |
meshGetGroupInfo(groupId) | Promise<MlsGroupInfo | null> | Get group info |
meshSetMemberRole(groupId, userId, role) | Promise<void> | Set member role (admin only) |
meshGetMemberRole(groupId, userId) | Promise<string> | Get a member’s role |
meshGetGroupRoles(groupId) | Promise<Record<string, string>> | Get all member roles |
meshRenameGroup(groupId, newName) | Promise<void> | Rename a group (admin only) |
Presence, Typing & Read Receipts
| Method | Returns | Description |
|---|---|---|
sendPresenceUpdate(recipient, status) | Promise<string> | Send presence status ('online', 'away', 'offline') |
sendTypingIndicator(recipient, conversationId, isTyping) | Promise<string> | Send typing indicator |
sendReadReceipt(recipient, messageIds) | Promise<string> | Send read receipt for message IDs |
User Blocking
| Method | Returns | Description |
|---|---|---|
blockUser(userId) | Promise<void> | Block a user (messages silently dropped) |
unblockUser(userId) | Promise<void> | Unblock a user |
getBlockedUsers() | Promise<string[]> | Get list of blocked user IDs |
isUserBlocked(userId) | Promise<boolean> | Check if a user is blocked |
Service Discovery & RPC (MeshServices)
Service Discovery turns the mesh into an offline internet — a network where every device can be both a client and a server. Devices register capabilities, others discover them through multi-hop routing, and invoke them with a request/response pattern. No server, no DNS, no infrastructure. On the traditional internet, you resolve a hostname, connect to a server, and make a request. Service Discovery follows the same pattern, but everything runs over the mesh. The devices around you are the infrastructure. While messaging moves data between known peers, Service Discovery lets devices find and interact with unknown peers based on what they can do.How It Works
- A device registers a service with an ID, version, and capability metadata
- Other devices broadcast discovery queries that propagate across the mesh
- Providers respond with
service_discoveredevents including their peer ID, version, and capabilities - The consumer sends a request to a specific provider, which receives it as a
service_request_receivedevent - The provider processes the request and sends a response back
Setup
API
| Method | Returns | Description |
|---|---|---|
registerService(serviceId, version, capabilities?) | Promise<void> | Register a local service with optional key-value capabilities |
unregisterService(serviceId) | Promise<boolean> | Unregister a service (returns true if found) |
discoverServices(serviceId?) | Promise<string> | Broadcast discovery query — pass null to discover all services (returns query ID) |
sendServiceRequest(provider, serviceId, method, body) | Promise<string> | Send a request to a specific provider (returns request ID for correlating responses) |
respondToServiceRequest(requestId, requester, serviceId, status, body) | Promise<string> | Respond to an incoming request with a status and body |
Provider Example
Consumer Example
Use Cases
Local AI Inference — A device with a loaded ML model registers an inference service. Nearby devices discover it and send classification or generation requests without needing cloud access. Sensor Data Feeds — IoT sensors register as data providers (temperature, air quality, GPS). Nearby devices discover and query them in real time, creating ad-hoc sensor networks with zero infrastructure. Decentralized Content Delivery — Devices that have cached content (maps, articles, firmware updates) register as content providers. Others discover and fetch what they need from the nearest available peer — a CDN that runs on the devices around you. Emergency Services — When infrastructure is down, devices register capabilities (medical supplies, shelter availability, communication relay). Rescue teams discover what’s available in their vicinity. The mesh becomes the network that disaster couldn’t take down. Collaborative Computing — Split a computation across multiple devices. One device registers a “compute” service, others discover it, send work units, and collect results — a mesh-native MapReduce. Event & Venue Services — Devices at a conference, stadium, or festival register services they offer (live polling, file drops, local chat rooms, schedule lookup). Attendees discover and use them without any centralized app server or internet connection.Discovery Across the Mesh
Discovery queries are not limited to directly connected peers. They propagate through the mesh using the same multi-hop routing as regular messages. This means:- A service registered on a device 5 hops away can still be discovered
- The
hop_countfield inservice_discoveredtells you how far away the provider is - You can use hop count to prefer closer providers for latency-sensitive operations
- Multiple providers for the same service ID may respond — your app decides which to use
Event Listeners
| Method | Returns | Description |
|---|---|---|
on(eventType, listener) | this | Register event listener |
off(eventType, listener) | this | Remove event listener |
once(eventType, listener) | this | Register one-time listener |
removeAllListeners(eventType?) | this | Remove all listeners |
protocol.on('all', listener).
See the Events Reference for all available event types and their payloads.
Next: Events Reference
See all event types and their payloads.