0.4.3 • Published 4 years ago
@andrewrivers/simple_webrtc v0.4.3
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'
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 withoffer
, second step
const answer: string = await peer.answer(offer)
open
establishes a connection withanswer
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 returnsnull
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 returnsnull
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 returnsnull
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 returnsnull
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, followingPeer.open()
peer.on('open', () => {
/* connection established, can transceive data */
})
message
data has been received, either byPeer.broadcast()
orDataChannel.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 viamedia
[MDN]
peer.on('track', ({ streams } : { streams: MediaStream[] }) => ... )
error
handled byconfig.debug
peer.on('error', ( method: string, message: string ) => ... )
log
handled byconfig.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