Peer-to-peer (P2P) data channel over WebRTC and a signaling channel on the browser
npm install p2p-data-channel
p2p-data-channel is a TypeScript library that simplifies the process of creating a peer-to-peer (P2P) data channel using WebRTC for the main data channel and PeerJS as a signaling channel. With this library, developers can easily add peer-to-peer communication to their web applications without having to worry about the underlying details of WebRTC or PeerJS.
``sh`
npm install p2p-data-channel
`typescript
import P2PDataChannel from 'p2p-data-channel'
// default config values
const config = {
debug: false, // output every method call to console
dataChannel: 'default', // name of the data channel to be open
connectionTimeout: 5000, // timeout to consider connection failure on init
pingInterval: 4000, // interval for each ping/pong
pingTimeout: 8000, // timeout to consider disconnection on ping/pong
}
const dataChannel = new P2PDataChannel('your-peer-id', config) // config is optional
// Receive a message
dataChannel.onMessage((message) => {
console.log(Received message from ${message.sender}: ${message.payload})
})
// Connect to a peer
dataChannel.connect('peer-id-to-connect').then(() => {
// Send a message
dataChannel.send('peer-id-to-connect', 'Hello, world!')
})
// Disconnect from the peer
dataChannel.disconnect('peer-id-to-disconnect')
`
Connects to the peer identified by remotePeerId.
Example:
`typescriptError connecting to remote peer: ${err}
dataChannel.connect('remote-peer-id')
.then(() => {
console.log('Connected to remote peer')
})
.catch((err) => {
console.error()`
})
Disconnects from the peer identified by remotePeerId.
Example:
`typescript`
dataChannel.disconnect('remote-peer-id')
Registers a callback to be called when a message is received from any peer.
Example:
`typescriptReceived message from ${message.sender}: ${message.payload}
dataChannel.onMessage((message) => {
console.log();`
})
Registers a callback to be called when a peer is connected.
Example:
`typescript${remotePeerId} is connected!
dataChannel.onConnected((remotePeerId) => {
console.log();`
})
Registers a callback to be called when a peer is disconnected.
Example:
`typescript${remotePeerId} disconnected!
dataChannel.onDisconnected((remotePeerId) => {
console.log();`
})
Sends a message to the peer identified by remotePeerId.
throws ConnectionNotEstablished if not connected
Example:
`typescript`
dataChannel.send('remote-peer-id', 'Hello, world!')
Broadcasts a message to all connected peers.
Example:
`typescript``
dataChannel.broadcast('A new user has joined the chat!')
If you find a bug or have a feature request, please open an issue on the GitHub repository. Pull requests are also welcome!
This project is licensed under the MIT License - see the LICENSE file for details.
Special thanks to ChatGPT for being an excellent copilot throughout the development of this library.