Creates a new WebRTC peer channel.
Unique identifier for this peer
Optional configuration for WebRTC connection
Sends a type-safe, validated message using a message definition.
This is the preferred way to send messages as it provides:
The message definition type
Message definition created with defineMessage()
Message data that matches the definition's schema
const userMsg = defineMessage('user', z.object({
name: z.string(),
age: z.number().min(0)
}));
// Type-safe and validated
await channel.sendMessage(userMsg, {
name: 'Alice',
age: 25
});
// TypeScript error: missing required field
await channel.sendMessage(userMsg, { name: 'Bob' }); // Error!
// Runtime error: validation fails
await channel.sendMessage(userMsg, { name: 'Charlie', age: -1 }); // Throws!
Registers a message handler for a specific message type (by string ID).
String identifier for the message type
Function to handle incoming messages
Registers a type-safe message handler using a message definition. This is the preferred method as it provides full type safety.
Message definition created with defineMessage()
Type-safe handler function
Removes a message handler for a specific message type (by string ID).
String identifier for the message type
The exact handler function to remove
Removes a type-safe message handler using a message definition.
Message definition created with defineMessage()
The exact handler function to remove
Sets the signaling handler for exchanging WebRTC signaling messages. This should connect to your signaling mechanism (WebSocket, Socket.IO, etc.)
The handler will be called whenever this peer needs to send signaling messages to remote peers during connection establishment.
Function to handle outgoing signaling messages
Creates a WebRTC offer to initiate connection with a remote peer. Call this method when you want to start a connection to another peer.
The ID of the peer you want to connect to
A WebRTC offer that should be sent to the remote peer via signaling
Handles an incoming WebRTC offer and creates an answer. Call this when you receive an offer from another peer via signaling.
The WebRTC offer received from remote peer
The ID of the peer sending the offer
A WebRTC answer that should be sent back to the offering peer
Handles an incoming WebRTC answer from a remote peer. Call this when you receive an answer to your offer via signaling.
The WebRTC answer received from remote peer
Handles an incoming ICE candidate from a remote peer. ICE candidates are exchanged during connection establishment to find the best network path between peers.
The ICE candidate received from remote peer
Establishes connection to the specified endpoint. Implementation varies by transport (WebSocket URL, WebRTC signaling, etc.)
Closes the connection and cleans up resources.
Sends raw data over the channel. Use sendMessage() for type-safe validated sending instead.
Message type identifier
Raw message data
Get the peer ID of this channel
Get the peer ID of the remote peer (if connected)
Get the current WebRTC connection state
Get the current data channel state
Example
Basic peer-to-peer setup:
Example
Handle incoming signaling messages:
Benefits of WebRTC P2P