This is a modern and simple library of webrtc data channels for comfortable using in node js and browsers.
This library doesn't include a signal server!
yarn add webrtc-datachannel
or
npm i webrtc-datachannel
The first, make an instance:
import WebRtcDataChannel from 'webrtc-datachannel'
const rtc = new WebRtcDataChannel()
Initiator:
const request = await rtc.initiateConnect()
// Transfer the `request` to recipient via signaller (for example web-server or qr code)
const answer = await getAnswer(request)
// Then pass an `answer` here
await rtc.setAnswer(answer)
// Connection is opened!
const response = await rtc.send({ question: 'How are you?' })
console.log(response) // { answer: 'I am fine.' }
Recipient:
// Get `request` from initiator by signaller
const answer = await rtc.initByRequest(request)
// Then pass the `answer` back to initiator
sendAnswer(answer)
// Just wait for open channel
await rtc.channelOpened()
// Connection is opened!
// We can listen for messages or send messages here
rtc.on('message', async (message) => {
if (message.question === 'How are you?') {
rtc.send({ answer: 'I am fine.' })
}
})
Please, see examples folder