GitHub - LuKks/like-webrtc: Peer-to-peer WebRTC connections with built-in signaling and peer discovery.
Peer-to-peer WebRTC connections with built-in signaling and peer discovery. - LuKks/like-webrtc
Peer-to-peer WebRTC connections with built-in signaling and peer discovery. - LuKks/like-webrtc
Works across browsers, Node.js, and React Native.
npm i like-webrtc
Live demo: https://web.leet.ar (P2P Discord clone built in 48 hours)
Need support with WebRTC? Open a GitHub issue :)
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.
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)
// ...
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')
Single signaling server:
wss://party.leet.ar:41337It's a WebSocket server with a RPC protocol.
3 STUN servers:
stun:turn.leet.ar:3478stun:stun.l.google.com:19302stun:stun.cloudflare.com:34781 TURN server with multiple transports:
turn:turn.leet.ar:3478?transport=udpturn:turn.leet.ar:3478?transport=tcprtc = 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.isInitiatorIdentifies the client (true) or server (false) side.
peer.publicKeyThe local public key used for this connection as a buffer.
peer.addressThe local public key used for this connection as a Base58 string.
peer.remotePublicKeyThe 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.publicKeyReturns the server's public key as a buffer.
server.addressReturns 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.
await rtc.announce(topic[, options])Signs and sends the announcement with keyPair.
Options:
{
keyPair // Defaults to rtc.keyPair.
}
The announcement expires unless you refresh or remove it.
await rtc.unannounce(topic[, options])Signs and sends a request to remove the announcement with keyPair.
Options:
{
keyPair // Defaults to rtc.keyPair.
}
peers = await rtc.lookup(topic)Returns peers announced under topic:
[
{ publicKey }
]
An announcement can remain even after a peer abruptly disconnects, unless closed properly.
The peer can stay discoverable while reconnecting after a brief signaling drop or page refresh.
A lookup during this gap can return a peer that is still offline.
await rtc.close()Closes the RTC instance, its servers, its peers, and its signaling.
If you called rtc.announce() directly, call rtc.unannounce() to remove those announcements.
If you do not remove these manual announcements, they remain available until expiration.
keyPair = RTC.keyPair([seed])Creates a key pair. Pass seed to create the same key pair each time.
bytes = RTC.randomBytes(size)Returns random bytes.
stream = RTC.WebStream(dataChannelOrWebSocket)Wraps a WebRTC data channel or a WebSocket in a duplex stream.
Useful for interacting with common Node.js streams.
WhatsApp relays and stores messages for 30d if the recipient is offline.
Telegram uses WebRTC with double encryption to avoid trusting SRTP/DTLS.
Discord forwards WebRTC media with SFUs so peers don't connect together.
like-webrtc can do the same for a perfect P2P experience including the browser.
MIT. Copyright (c) Lucas Barrena
Texto de github.com tal como lo publicó, leído de su página y mostrado aquí con el formato de El Autómata. El original, en github.com · Volver a la noticia de El Autómata