API Reference
TheMessagingPeer class is the core component of the Instant Messaging Protocol. It handles connection to the signaling server, peer discovery, message exchange, and encryption/decryption.
Constructor
config: Configuration object containing:serverUrl: WebSocket URL of the signaling server (the Demos Node managing the messenger instance)clientId: Unique identifier for this peerpublicKey: Public key for encryption/decryption
Methods
connecttargetId: ID of the target peermessage: Message to send
peerId: ID of the peer whose public key to request
questionId: ID of the question to respond toresponse: Response to send back to the server
message: Message to sendexpectedResponseType: Type of response to wait foroptions: Additional options:timeout: Timeout in milliseconds (default: 10000)errorHandler: Custom error handlerretryCount: Number of retry attemptsfilterFn: Function to filter messages by additional criteria
Event Handlers
onMessagehandler: Function to call when a message is received
handler: Function to call when a server question is received
handler: Function to call when an error occurs
handler: Function to call when a peer disconnects
handler: Function to call when the connection state changes
Message Types
The Instant Messaging Protocol supports various message types:- “register”: Register a new peer with the server
- “discover”: Request a list of all connected peers
- “message”: Encrypted peer-to-peer messages
- “peer_disconnected”: Notification when a peer disconnects
- “request_public_key”: Request a peer’s public key
- “public_key_response”: Response containing a peer’s public key
- “server_question”: Question from the server to a peer
- “peer_response”: Response from a peer to a server question
- “debug_question”: Debug message to trigger a server question
- “error”: Error notification with details