like-webrtc Library Bundles Signaling and Peer Discovery into WebRTC
like-webrtc is an open-source npm package that bundles WebRTC signaling, default STUN/TURN configuration and topic-based peer discovery, letting developers establish peer-to-peer connections across browsers, Node.js and React Native without running their own signaling server. It uses shareable cryptographic addresses instead of IPs and exposes data channels, audio/video tracks and transceivers.
Related Entities
Full Report
Contents23 sections
like-webrtc
Peer-to-peer WebRTC connections with built-in signaling and peer discovery.
Works across browsers, Node.js, and React Native.
npm i like-webrtc- P2P servers. Listen for peer connections anywhere, even in the browser.
- Direct connections. Connect to peers across devices and networks.
- Built-in signaling. Automatic SDP/ICE exchange and negotiation.
- Identities. Shareable cryptographic addresses instead of IP addresses.
- Peer discovery. Announce and find peers using shared topics.
- WebRTC API. Use data channels, audio/video tracks and transceivers.
- Ready to use. Signaling, STUN and TURN configured by default.
Live demo: https://web.leet.ar (P2P Discord clone built in 48 hours)
Need support with WebRTC? Open a GitHub issue :)
Usage
Direct peer-to-peer connections
A peer can create a server listening on a public key as address.
Another peer can use that address to connect to the server.
import RTC from 'like-webrtc'
const rtc = new RTC()
const server = rtc.createServer(peer => {
const channel = peer.createDataChannel('wire', { negotiated: true, id: 0 })
channel.addEventListener('open', () => channel.send('Hello from the server'))
channel.addEventListener('message', e => console.log('Server received:', e.data))
})
const keyPair = RTC.keyPair()
await server.listen(keyPair)const rtc2 = new RTC()
const peer = rtc2.connect(server.publicKey)
const channel = peer.createDataChannel('wire', { negotiated: true, id: 0 })
channel.addEventListener('open', () => channel.send('Hello from the client'))
channel.addEventListener('message', e => console.log('Client received:', e.data))A familiar API like Node.js http, but peer-to-peer and running directly in your browser.
Group peers
<details> <summary>Click to see example</summary>Peers can create servers with random key pairs.
You can announce or lookup any server under the same shared topic.
import RTC from 'like-webrtc'
const rtc = new RTC()
const server = rtc.createServer(peer => {
console.log('New peer connection')
// ...
})
await server.listen()
await server.announce('random-group')const rtc2 = new RTC()
const results = await rtc2.lookup('random-group')
const peer = rtc2.connect(results[0].publicKey)
// ...Swarm peers
<details> <summary>Click to see example</summary>Each peer runs the same code to join the swarm.
Other peers will find you.
import RTC from 'like-webrtc'
const rtc = new RTC()
const server = rtc.createServer(peer => {
console.log('New swarm connection')
// ...
})
await server.listen()
await server.join('random-swarm')SIGNALING
Single signaling server:
wss://party.leet.ar:41337
It's a WebSocket server with a RPC protocol.
- E2E noise encryption: meaning HTTPS/WSS is redundant.
- Announce / Unannounce: act as a server e.g. listen/close.
- Lookup: act as a client by finding peers.
- Relay: bidirectional data for WebRTC SDP/ICE/ACK.
ICE
3 STUN servers:
stun:turn.leet.ar:3478stun:stun.l.google.com:19302stun:stun.cloudflare.com:3478
1 TURN server with multiple transports:
turn:turn.leet.ar:3478?transport=udpturn:turn.leet.ar:3478?transport=tcp
API
rtc = new RTC([options])
Creates a WebRTC instance.
Options:
{
signaling, // Defaults to 'wss://party.leet.ar:41337'.
keyPair, // Defaults to a key pair generated from seed or random bytes.
seed, // Seed used to generate the key pair.
iceServers, // Defaults to the package's STUN and TURN servers.
iceTransportPolicy, // Defaults to 'all'.
trickle, // Defaults to true.
RTCPeerConnection, // Optional WebRTC implementation override.
RTCIceCandidate // Optional ICE candidate implementation override.
}peer = rtc.connect(publicKey[, options])
Creates a P2P connection and returns a RTCPeerConnection.
Add a data channel, track, or transceiver to begin WebRTC negotiation.
Options:
{
keyPair // Defaults to rtc.keyPair.
}peer.isInitiator
Identifies the client (true) or server (false) side.
peer.publicKey
The local public key used for this connection as a buffer.
peer.address
The local public key used for this connection as a Base58 string.
peer.remotePublicKey
The remote peer's public key as a buffer.
server = rtc.createServer([options][, handler])
Creates a P2P server that accepts WebRTC connections.
The handler receives each incoming peer connection.
Options:
{
keyPair, // Defaults to rtc.keyPair.
firewall // Function or array of allowed public keys. Allows all peers by default.
}server.on('connection', peer)
Emitted on each incoming RTCPeerConnection, same as the handler.
server.publicKey
Returns the server's public key as a buffer.
server.address
Returns the server's public key as a Base58 string.
await server.listen([keyPair])
Starts listening for connections under the server's public key so peers can connect.
await server.announce(topic)
Announces the listening server under topic and refreshes the announcement while the server listens.
The server removes the announcement when it closes.
await server.unannounce(topic)
Removes the server's announcement for topic.
The server keeps listening for direct connections.
If the server has joined topic, it will keep looking up peers.
The topic stays unannounced until you call server.announce(topic) or leave and join again.
await server.join(topic)
Finds peers that announce topic and connects to them.
If the server is listening, it also announces itself under that topic.
The server emits a connection event for each discovered connection.
Closed connections are retried after 1 second, 5 seconds, 15 seconds, then every 10 minutes.
await server.leave(topic)
Stops finding peers under topic.
If only server.join(topic) announced the topic, it also removes that announcement.
An announcement made from server.announce(topic) stays active.
Existing peer connections stay open.
Cancels retries unless the peer was also discovered under another joined topic.
await server.close()
Closes the server's peer connections, removes its topic announcements, and closes its signaling connection.
(注:更多技术实现细节与完整 API 文档请查阅原项目 README)