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

communication

v1.0.1

Published

Utilize WebRTC PeerConnection API

Downloads

36

Readme

CommunicationJS

Utilize WebRTC PeerConnection API

Hide session establishing process, handle stream persistency

Usage


import Communication from 'communication'
import webrtc from 'communication-adaptor-webrtc'

if (!webrtc.support) {
  throw new Error('This browser does not support WebRTC API')
}

let comm = Communication(webrtc)

comm.on('message', msg => {
  mySignalChannel.send(msg)
})

mySignalChannel.on('message', msg => {
  comm.write(msg)
})

comm.open()

let streamId = null

comm.on('streamAdded', (stream, id, meta) => {
  if (streamId != null) return
  streamId = id
  myDescription.textContent = meta.description
  myMedia.src = URL.createObjectURL(stream)
})

comm.on('streamUpdated', (stream, id) => {
  if (id !== streamId) return
  myMedia.src = URL.createObjectURL(stream)
})

comm.on('streamRemoved', id => {
  if (id !== streamID) return
  streamId = null
  myMedia.src = null
})

let localStream = getStreamSomehow()

comm.addStream(localStream, {
  description: 'my nice stream'
})

Method

state(): String

Get current Communication state

will return one of following values

  • 'waiting': Communication is not established
  • 'running': Communication is established and usable
  • 'closed': Communication is closed

open(): Communication

Start establishing connection

This method MUST be called AFTER message handling, BEFORE any other operation

write(message: String): Communication

Receive message from signalling channel

addStream(stream: MediaStream, meta: Object): Communication

Add local MediaStream to the Communication

Metadata object will be sent to the remote Communication object

See streamAdded event

removeStream(stream: MediaStream|String): Communication

Remove local MediaStream from the Communication

getLocalStreams(): Array

Return an array of MediaStreams that Communication streaming to remote peer

getRemoteStreams(): Array

Return an array of MediaStreams that Communication streaming from remote peer

getStreamById(id: String): MediaStream

Return an MediaStream from given id

Note: if stream is sent from remote peer, stream.id may not be same as given id

close(): void

Close this communication permanently

Event

message(msg: String)

Communication has a new message to send to remote peer

running()

Connection to remote peer is established

This Communication is now workable

streamAdded(stream: MediaStream, id: String, meta: Object)

Remote peer added another stream to Communication

See addStream method

streamUpdated(stream: MediaStream, id: String)

Existing stream is re-established for some reason(ex) internet connection is unstable)

For stream sender, newly arrived stream is just same stream as previous stream with same id

Just replace previous stream with this one is enough to handle stream persistency

streamRemoved(id: String)

Remote peer removed stream from Communication

closed(fromRemote: Boolean, reason: Any)

This Communication is closed permanently