API Reference
The MessagingPeer
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
Creates a new MessagingPeer instance.
Parameters:
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
Example:
Methods
connect
Connects to the signaling server and registers the peer.
Returns: Promise that resolves when connected and registered
Example:
disconnect
Disconnects from the signaling server.
Example:
register
Registers the peer with the signaling server.
Example:
registerAndWait
Registers the peer with the signaling server and waits for confirmation.
Returns: Promise that resolves when registration is confirmed
Example:
discoverPeers
Discovers all connected peers.
Returns: Array of peer IDs
Example:
sendMessage
Sends an encrypted message to a specific peer.
Parameters:
targetId
: ID of the target peermessage
: Message to send
Example:
requestPublicKey
Requests a peer's public key.
Parameters:
peerId
: ID of the peer whose public key to request
Returns: The peer's public key as a Uint8Array
Example:
respondToServer
Responds to a server question.
Parameters:
questionId
: ID of the question to respond toresponse
: Response to send back to the server
Example:
sendToServerAndWait
Sends a message to the server and waits for a specific response type.
Parameters:
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
Returns: The response payload
Example:
Event Handlers
onMessage
Registers a handler for incoming messages.
Parameters:
handler
: Function to call when a message is received
Example:
onServerQuestion
Registers a handler for server questions.
Parameters:
handler
: Function to call when a server question is received
Example:
onError
Registers a handler for errors.
Parameters:
handler
: Function to call when an error occurs
Example:
onPeerDisconnected
Registers a handler for peer disconnection events.
Parameters:
handler
: Function to call when a peer disconnects
Example:
onConnectionStateChange
Registers a handler for connection state changes.
Parameters:
handler
: Function to call when the connection state changes
Example:
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
Message Interface
MessagingPeerConfig Interface
Last updated