npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

danbi-simplewebrtc

v0.1.2

Published

World's easiest webrtc

Downloads

8

Readme

SimpleWebRTC - World's easiest WebRTC lib

Gitter

Want to see it in action? Check out the demo: https://simplewebrtc.com/demo.html

Want to run it locally? npm install && npm run test-page

It's so easy:

1. Some basic html

<!DOCTYPE html>
<html>
    <head>
        <script src="https://simplewebrtc.com/latest-v2.js"></script>
        <style>
            #remoteVideos video {
                height: 150px;
            }
            #localVideo {
                height: 150px;
            }
        </style>
    </head>
    <body>
        <video id="localVideo"></video>
        <div id="remoteVideos"></div>
    </body>
</html>

Installing through NPM

npm install --save simplewebrtc

# for yarn users
yarn add simplewebrtc

After that simply import simplewebrtc into your project

import SimpleWebRTC from 'simplewebrtc';

2. Create our WebRTC object

var webrtc = new SimpleWebRTC({
    // the id/element dom element that will hold "our" video
    localVideoEl: 'localVideo',
    // the id/element dom element that will hold remote videos
    remoteVideosEl: 'remoteVideos',
    // immediately ask for camera access
    autoRequestMedia: true
});

3. Tell it to join a room when ready

// we have to wait until it's ready
webrtc.on('readyToCall', function () {
    // you can name it anything
    webrtc.joinRoom('your awesome room name');
});

Available options

peerConnectionConfig - Set this to specify your own STUN and TURN servers. By default, SimpleWebRTC uses Google's public STUN server (stun.l.google.com:19302), which is intended for public use according to: https://twitter.com/HenrikJoreteg/status/354105684591251456

Note that you will most likely also need to run your own TURN servers. See http://www.html5rocks.com/en/tutorials/webrtc/infrastructure/ for a basic tutorial.

Filetransfer

Sending files between individual participants is supported. See http://simplewebrtc.com/filetransfer.html for a demo.

Note that this is not file sharing between a group which requires a completely different approach.

It's not always that simple...

Sometimes you need to do more advanced stuff. See http://simplewebrtc.com/notsosimple.html for some examples.

Got questions?

Join the SimpleWebRTC discussion list:

http://lists.andyet.com/mailman/listinfo/simplewebrtc

or the Gitter channel:

https://gitter.im/HenrikJoreteg/SimpleWebRTC

API

Constructor

new SimpleWebRTC(options)

  • object options - options object provided to constructor consisting of:
    • string url - required url for signaling server. Defaults to signaling server URL which can be used for development. You must use your own signaling server for production.
    • object socketio - optional object to be passed as options to the signaling server connection.
    • Connection connection - optional connection object for signaling. See Connection below. Defaults to a new SocketIoConnection
    • bool debug - optional flag to set the instance to debug mode
    • [string|DomElement] localVideoEl - ID or Element to contain the local video element
    • [string|DomElement] remoteVideosEl - ID or Element to contain the remote video elements
    • bool autoRequestMedia - optional(=false) option to automatically request user media. Use true to request automatically, or false to request media later with startLocalVideo
    • bool enableDataChannels optional(=true) option to enable/disable data channels (used for volume levels or direct messaging)
    • bool autoRemoveVideos - optional(=true) option to automatically remove video elements when streams are stopped.
    • bool adjustPeerVolume - optional(=false) option to reduce peer volume when the local participant is speaking
    • number peerVolumeWhenSpeaking - optional(=.0.25) value used in conjunction with adjustPeerVolume. Uses values between 0 and 1.
    • object media - media options to be passed to getUserMedia. Defaults to { video: true, audio: true }. Valid configurations described on MDN with official spec at w3c.
    • object receiveMedia - optional RTCPeerConnection options. Defaults to { offerToReceiveAudio: 1, offerToReceiveVideo: 1 }.
    • object localVideo - optional options for attaching the local video stream to the page. Defaults to
    {
        autoplay: true, // automatically play the video stream on the page
        mirror: true, // flip the local video to mirror mode (for UX)
        muted: true // mute local video stream to prevent echo
    }
    • object logger - optional alternate logger for the instance; any object that implements log, warn, and error methods.

Fields

capabilities - the webrtcSupport object that describes browser capabilities, for convenience

config - the configuration options extended from options passed to the constructor

connection - the socket (or alternate) signaling connection

webrtc - the underlying WebRTC session manager

Events

To set up event listeners, use the SimpleWebRTC instance created with the constructor. Example:

var webrtc = new SimpleWebRTC(options);
webrtc.on('connectionReady', function (sessionId) {
    // ...
})

'connectionReady', sessionId - emitted when the signaling connection emits the connect event, with the unique id for the session.

'createdPeer', peer - emitted three times:

  • when joining a room with existing peers, once for each peer

  • when a new peer joins a joined room

  • when sharing screen, once for each peer

  • peer - the object representing the peer and underlying peer connection

'stunservers', [...args] - emitted when the signaling connection emits the same event

'turnservers', [...args] - emitted when the signaling connection emits the same event

'localScreenAdded', el - emitted after triggering the start of screen sharing

  • el the element that contains the local screen stream

'leftRoom', roomName - emitted after successfully leaving the current room, ending all peers, and stopping the local screen stream

'videoAdded', videoEl, peer - emitted when a peer stream is added

  • videoEl - the video element associated with the stream that was added
  • peer - the peer associated with the stream that was added

'videoRemoved', videoEl, peer - emitted when a peer stream is removed

  • videoEl - the video element associated with the stream that was removed
  • peer - the peer associated with the stream that was removed

Methods

createRoom(name, callback) - emits the create event on the connection with name and (if provided) invokes callback on response

joinRoom(name, callback) - joins the conference in room name. Callback is invoked with callback(err, roomDescription) where roomDescription is yielded by the connection on the join event. See signalmaster for more details.

startLocalVideo() - starts the local media with the media options provided in the config passed to the constructor

testReadiness() - tests that the connection is ready and that (if media is enabled) streams have started

mute() - mutes the local audio stream for all peers (pauses sending audio)

unmute() - unmutes local audio stream for all peers (resumes sending audio)

pauseVideo() - pauses sending video to peers

resumeVideo() - resumes sending video to all peers

pause() - pauses sending audio and video to all peers

resume() - resumes sending audio and video to all peers

sendToAll(messageType, payload) - broadcasts a message to all peers in the room via the signaling channel (websocket)

  • string messageType - the key for the type of message being sent
  • object payload - an arbitrary value or object to send to peers

sendDirectlyToAll(channelLabel, messageType, payload) - broadcasts a message to all peers in the room via a dataChannel

  • string channelLabel - the label for the dataChannel to send on
  • string messageType - the key for the type of message being sent
  • object payload - an arbitrary value or object to send to peers

getPeers(sessionId, type) - returns all peers by sessionId and/or type

shareScreen(callback) - initiates screen capture request to browser, then adds the stream to the conference

getLocalScreen() - returns the local screen stream

stopScreenShare() - stops the screen share stream and removes it from the room

stopLocalVideo() - stops all local media streams

setVolumeForAll(volume) - used to set the volume level for all peers

  • volume - the volume level, between 0 and 1

leaveRoom() - leaves the currently joined room and stops local screen share

disconnect() - calls disconnect on the signaling connection and deletes it

handlePeerStreamAdded(peer) - used internally to attach media stream to the DOM and perform other setup

handlePeerStreamRemoved(peer) - used internally to remove the video container from the DOM and emit videoRemoved

getDomId(peer) - used internally to get the DOM id associated with a peer

getEl(idOrEl) - helper used internally to get an element where idOrEl is either an element, or an id of an element

getLocalVideoContainer() - used internally to get the container that will hold the local video element

getRemoteVideoContainer() - used internally to get the container that holds the remote video elements

Connection

By default, SimpleWebRTC uses a socket.io connection to communicate with the signaling server. However, you can provide an alternate connection object to use. All that your alternate connection need provide are four methods:

  • on(ev, fn) - A method to invoke fn when event ev is triggered
  • emit() - A method to send/emit arbitrary arguments on the connection
  • getSessionId() - A method to get a unique session Id for the connection
  • disconnect() - A method to disconnect the connection