Creates a new Peer instance.
Optionaloptions: PeerOptionsPeer configuration options.
Indicates whether the peer is currently active (joined a room).
Attachable extensions.
Configured local data channels indexed by channel label.
Active remote peers indexed by remote peer id.
Unique identifier for the local peer. Empty until join() is called.
Optional metadata announced to other peers in signaling messages. Undefined until join() is called.
Current room name. Empty until join() is called.
Configured local streams indexed by application-level stream label.
Attaches an addon/extension to the peer instance.
Addon instance to attach.
Detaches a previously attached addon/extension from the peer instance.
Addon instance to detach.
Emits one or more events. Usually you would not call this method directly.
Event name or list of event names.
Event payload.
Joins a room and starts listening for incoming connections.
Optionaloptions: string | PeerJoinOptionsRoom name or join options.
Removes a previously registered event listener.
Event name or list of event names.
Optionalhandler: (...args: PeerEvents[K]) => voidEvent handler to remove. If omitted, all handlers for the given event(s) will be removed.
Subscribes to one or more peer events.
Subscribes to an event and auto-unsubscribes after first invocation.
Event name or list of event names.
Event handler.
Opens a data channel with the given label and options to all remote peers. If a channel with the same label already exists, it will be reused.
You can open a channel with the same label on both local and remote peers or only on one side. In any case, only one channel will be created for each label. You can send data through the channel in both directions.
Channel options or channel label.
Sends a message through data channels to all connected remote peers.
If options is a string, it is treated as the channel label. If a label
is not provided, it uses the default channel.
The send method works only with open channels that have no protocol, are
ordered (reliable), and match the specified label.
Message payload to send.
Optionaloptions: string | SendOptionsSend options or channel label.
A ReadableStream of aggregated transfer progress across all connections. The returned value also implements Promise, which resolves when all transfers complete or error.
// send a message to default channel
await peer.send("Hello, all peers!");
// send large data with a progress handler
const file = new File([new Uint8Array(1024 * 1024)], "example.dat");
const transfer = peer.send(file, {
label: "chat", // channel label
info: { filename: file.name }, // metadata
signal: AbortSignal.timeout(10000), // abort signal
});
// optionally handle the progress
for await (const progress of transfer) {
const { id, label, current, total } = progress;
const percent = Math.round((current / total) * 100);
console.log(`[${id}:${label}] Sending... ${percent}%`);
}
Shares a new media stream or updates an existing one for all remote peers including new ones that join later.
If you pass a MediaStream instance directly, it will be shared under a label equal to the stream id. Otherwise, you can specify an explicit label in the options object. If a stream with the same label already exists, it will be updated and its tracks will be added/removed as needed to minimize renegotiations.
If the stream is shared with the managed option, its tracks will be
automatically stopped when the stream is unshared or replaced with
a new stream.
Stream descriptor or MediaStream instance.
The shared MediaStream instance if successful, or undefined.
Serializes the peer to a JSON-compatible object.
A serializable representation of the peer.
Stops sharing a previously shared media stream with the given label and removes it from all remote peers.
If you pass a MediaStream instance directly, it will be unshared using its id as the label. Otherwise, you can specify the label in the options object or pass it directly as a string.
If the stream was shared with the managed option, its tracks will be
stopped automatically.
A stream label, MediaStream instance, or an object containing a label.
The unshared MediaStream instance, or undefined.
Manages WebRTC peer connections, media streams, and data channels.
Example