0.2.2 • Published 6 years ago

webrtc-datachannel v0.2.2

Weekly downloads
5
License
MIT
Repository
-
Last release
6 years ago

webrtc-datachannel

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!

Install

yarn add webrtc-datachannel

or

npm i webrtc-datachannel

How to

The first, make an instance:

import WebRtcDataChannel from 'webrtc-datachannel'

const rtc = new WebRtcDataChannel()

Using simple API

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.' })
    }
  })

Using extended API

Please, see examples folder

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago