0.4.3 • Published 3 years ago

@andrewrivers/simple_webrtc v0.4.3

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

Simple WebRTC

A WebRTC wrapper that aims to be the as short and concise as possible.

installation (npm)

yarn add @andrewrivers/simple_webrtc

or..

npm i --save @andrewrivers/simple_webrtc

installation (es6)

import SimpleWebRTC from './simple_webrtc.js'

Standalone Release

usage

import SimpleWebRTC from '@andrewrivers/simple_webrtc'

const { Peer, Broadcast } = SimpleWebRTC()

const peer = Peer('local')
const offer = await peer.offer( )
const answer = await peer.answer( offer )

peer.open(answer)

peer.on('open', ( ) => ...)
peer.on('message', ( message ) => ...)
peer.on('error', ( error ) => ...)
peer.on('close', ( ) => ...)

peer.send({ abc: "def" })

Broadcast("hello world")

const camera = Peer.camera( )
const microphone = Peer.microphone( )
const screenshare = Peer.screen( )

peer.on('track', ({ streams }) => ...)

methods

connection

  • offer creates an offer, this is the first step in establishing a connection
const offer: string = await peer.offer()
  • answer creates an answer with offer, second step
const answer: string = await peer.answer(offer)
  • open establishes a connection with answer
await peer.open(answer)

send data

  • broadcast sends data to all peers
const data: object | string | number = { message: 'Hello World!' }
Broadcast(data)
  • peer.send sends data to a single peer
const data: object | string | number = { message: 'Hello World!' }
peer.send(data)

media

The UserMedia class supports callbacks as well as asynchronous functions. Each has an allow and block parameter, which are invoked depending on a user's prompt result. [MDN]

Peer.microphone( 
    ( stream: MediaStream ) => ... , // allow
    ( ) => ... // block
)
  • Peer.microphone adds the user's microphone's audio to a peer and returns its MediaStream, if unavailable or blocked returns null
const stream: MediaStream | void = await Peer.microphone()
  • Peer.camera adds the user's cameras's video to a peer and returns its MediaStream, if unavailable or blocked returns null
const stream: MediaStream | void = await Peer.camera()
  • Peer.screen prompts the user for a display/window/tab (with audio when available) and adds the user's choice to a peer and returns its MediaStream, if unavailable or blocked returns null
const stream: MediaStream | void = await Peer.screen()
  • Peer.custom uses custom constraints [MDN] to add the resulting MediaStream to a peer and returns its MediaStream, if unavailable or blocked returns null
const stream: MediaStream | void = await Peer.custom({
   video: true,  // default `false`
   audio: false, // default `false`
   screen: false // default `false`
})

callback syntax for Peer.custom:

Peer.custom( constraints, allow, block )

events

All events return async functions, so all events can be used in combination with await.

await peer.on('open')

Note that many of these events have no practical reason to be used this way, it is only for consistency and flexibility that it is provided.

  • open a connection has been established, following Peer.open()
peer.on('open', () => {
    /* connection established, can transceive data */
})
  • message data has been received, either by Peer.broadcast() or DataChannel.send()
peer.on('message', ( data: string | number | object ) => {
    /* if config.json is true, data will be parsed already */
})
  • icecandidate ICE candidate has been found, handled internally and can be ignored
peer.on('icecandidate', ( candidate: RTCIceCandidate ) => ... )
  • track track has been added via media [MDN]
peer.on('track', ({ streams } : { streams: MediaStream[] }) => ... )
  • error handled by config.debug
peer.on('error', ( method: string, message: string ) => ... )
  • log handled by config.log
peer.on('log', ( message: string ) => ... )

If you have any questions or you're having trouble getting set up, feel free to message me.

E-Mail: andrewarivers@gmail.com Twitter: @Andr3wRiv3rs Discord: PoisonApple#9351

0.4.3

3 years ago

0.4.2

3 years ago

0.3.0

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago