Skip to main content

API Reference

Both classes are exported from @kynesyslabs/demosdk under the instantMessaging namespace:
There is no @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 port 3006) 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()

Opens the WebSocket, sends 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()

Stops automatic reconnection, closes the WebSocket, rejects all pending request-response promises with Disconnected, clears the local peer set, and notifies connection-state handlers with "disconnected".

Messaging

send(to, encrypted, messageHash)

Sends an already-encrypted message to a peer. The returned payload is one of:
  • { messageHash, l2psStatus: "submitted" | "failed" } — server frame message_sent
  • { messageHash, status: "queued" } — server frame message_queued (recipient was offline)
Throws Not registered. Call connect() first. if invoked before connect() completes.

history(peerKey, options)

Fetches conversation history with 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()

Returns the list of online peer public keys in the current L2PS network. Also updates the internal peer set (readable via peer.peers).

requestPublicKey(targetId)

Looks up a peer’s public key by identifier. Returns the hex string, or null if the server cannot resolve targetId.

Getters

Event handlers

All on* handlers can be registered multiple times; registration order is preserved.
Removal counterparts:

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, register is replayed automatically; pending send/history/discover requests are not replayed.

MessagingPeer (legacy)

The legacy signaling-server client (default port 3005). 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

Example:

Methods

Event handlers

Removal counterparts: removeMessageHandler, removeErrorHandler, removePeerDisconnectedHandler, removeConnectionStateHandler.

Message interface

Reconnection

Same shape as L2PSMessagingPeer: exponential backoff starting at 1000 ms, capped at 30 000 ms, up to 10 attempts. Queued outbound messages are flushed once the connection reopens.