0.4.3 • Published 6 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_webrtcor..
npm i --save @andrewrivers/simple_webrtcinstallation (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
offercreates an offer, this is the first step in establishing a connection
const offer: string = await peer.offer()answercreates an answer withoffer, second step
const answer: string = await peer.answer(offer)openestablishes a connection withanswer
await peer.open(answer)send data
broadcastsends data to all peers
const data: object | string | number = { message: 'Hello World!' }
Broadcast(data)peer.sendsends 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.microphoneadds 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.cameraadds 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.screenprompts 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.customuses 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.
opena connection has been established, followingPeer.open()
peer.on('open', () => {
/* connection established, can transceive data */
})messagedata 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 */
})icecandidateICE candidate has been found, handled internally and can be ignored
peer.on('icecandidate', ( candidate: RTCIceCandidate ) => ... )tracktrack has been added viamedia[MDN]
peer.on('track', ({ streams } : { streams: MediaStream[] }) => ... )errorhandled byconfig.debug
peer.on('error', ( method: string, message: string ) => ... )loghandled 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