API Reference
Both classes are exported from@kynesyslabs/demosdk under the instantMessaging namespace:
@kynesyslabs/demosdk/instant_messaging subpath — the package root is the only canonical entry point.
L2PSMessagingPeer
The production client. Connects to the L2PS messaging server (default port3006) and provides registered, E2E-encrypted, persisted messaging with pagination, offline delivery, and L2PS pipeline tracking.
Constructor
signFn is invoked by the SDK whenever a frame requires an ed25519 proof — currently for register (register:{publicKey}:{timestamp}) and history (history:{peerKey}:{timestamp}). It must return a hex-encoded ed25519 signature of the input string.
Example:
Lifecycle
connect()
register with a fresh ed25519 proof, and resolves once the server replies with the registered frame. Times out after 10 seconds. The returned payload includes the list of currently online peers in the same L2PS network.
disconnect()
Disconnected, clears the local peer set, and notifies connection-state handlers with "disconnected".
Messaging
send(to, encrypted, messageHash)
{ messageHash, l2psStatus: "submitted" | "failed" }— server framemessage_sent{ messageHash, status: "queued" }— server framemessage_queued(recipient was offline)
Not registered. Call connect() first. if invoked before connect() completes.
history(peerKey, options)
peerKey. before is a millisecond timestamp for backward pagination; limit caps the page size. The SDK signs history:{peerKey}:{timestamp} with signFn and includes the proof in the request.
discover()
peer.peers).
requestPublicKey(targetId)
null if the server cannot resolve targetId.
Getters
| Getter | Type | Meaning |
|---|---|---|
isConnected | boolean | WebSocket open. |
isRegistered | boolean | Server accepted the register frame. |
peers | string[] | Snapshot of currently online peer public keys. |
Event handlers
Allon* handlers can be registered multiple times; registration order is preserved.
| Method | Fires when |
|---|---|
onMessage(handler) | Server delivers a message frame. Handler receives { from, encrypted, messageHash, offline? }. |
onError(handler) | Server sends an error frame, or a local protocol error occurs. Handler receives { code, message, details? }. |
onPeerJoined(handler) | A peer joined the L2PS network (peer_joined frame). |
onPeerLeft(handler) | A peer left the L2PS network (peer_left frame). |
onConnectionStateChange(handler) | WebSocket transitions between "connected", "disconnected", and "reconnecting". |
Reconnection
On unexpected socket close,L2PSMessagingPeer retries with exponential backoff:
- Base delay 1000 ms, doubled per attempt.
- Capped at 30 000 ms.
- Up to 10 attempts.
- After the socket re-opens,
registeris replayed automatically; pendingsend/history/discoverrequests are not replayed.
MessagingPeer (legacy)
The legacy signaling-server client (default port3005). Provides peer registration, discovery, and per-message relay with the same ml-kem-aes encryption applied client-side. No persistence, no history API, no offline queue, no server-side delivery acknowledgement.
Constructor
Methods
| Method | Signature | Description |
|---|---|---|
connect | async connect(): Promise<void> | Connect and register. |
disconnect | disconnect(): void | Close the WebSocket. |
register | register(): void | Send the register frame (no wait). |
registerAndWait | async registerAndWait(): Promise<void> | Register and await confirmation; signs the public key with ml-dsa for binding. |
discoverPeers | async discoverPeers(): Promise<string[]> | Returns the list of connected peer client IDs. |
sendMessage | async sendMessage(targetId: string, message: string): Promise<void> | Encrypts message with the recipient’s public key (auto-fetched) and relays through the signaling server. |
requestPublicKey | async requestPublicKey(peerId: string): Promise<Uint8Array> | Returns the peer’s public key bytes. |
respondToServer | respondToServer(questionId: string, response: any): void | Replies to a server_question with a peer_response. |
sendToServerAndWait | async sendToServerAndWait<T>(message: Message, expectedResponseType: Message["type"], options?: { timeout?: number; errorHandler?: (e: any) => void; retryCount?: number; filterFn?: (m: Message) => boolean }): Promise<T> | Generic request-response helper with optional retry, timeout, custom error handling, and message filtering. |
Event handlers
| Method | Handler signature |
|---|---|
onMessage(handler) | (message: any, fromId: string) => void |
onError(handler) | (error: { type: string; details: string }) => void |
onPeerDisconnected(handler) | (peerId: string) => void |
onConnectionStateChange(handler) | (state: "connected" | "disconnected" | "reconnecting") => void |
onServerQuestion(handler) | (question: any, questionId: string) => void |
removeMessageHandler, removeErrorHandler, removePeerDisconnectedHandler, removeConnectionStateHandler.
Message interface
Reconnection
Same shape asL2PSMessagingPeer: exponential backoff starting at 1000 ms, capped at 30 000 ms, up to 10 attempts. Queued outbound messages are flushed once the connection reopens.