Git Product home page Git Product logo

simple-signal's Introduction

simple-signal

Build Status JavaScript Style Guide

Easy signalling for simple-peer using socket.io.

Features

  • Streamlines WebRTC signaling without losing any flexibility.
  • Exposes the entire simple-peer API.
  • Useful for managing multiple connections.
  • Uses modern ES6 async/await.

Install

Server:

npm install simple-signal-server --save

Client (with Browserify):

npm install simple-signal-client --save

Without Browserify, use simple-signal-client.min.js.

Example

A common signaling scheme is to connect two clients by having one client "call" the ID of another.

Server:

const signalServer = require('simple-signal-server')(io)
const allIDs = new Set()

signalServer.on('discover', (request) => {
  const clientID = request.socket.id // you can use any kind of identity, here we use socket.id
  allIDs.add(clientID) // keep track of all connected peers
  request.discover(clientID, Array.from(allIDs)) // respond with id and list of other peers
})

signalServer.on('disconnect', (socket) => {
  const clientID = socket.id
  allIDs.delete(clientID)
})

signalServer.on('request', (request) => {
  request.forward() // forward all requests to connect
})

Client:

var signalClient = new SimpleSignalClient(socket) // Uses an existing socket.io-client instance

signalClient.on('discover', async (allIDs) => {
  const id = await promptUserForID(allIDs) // Have the user choose an ID to connect to
  const { peer } = await signalClient.connect(id) // connect to target client
  peer // this is a fully-signaled simple-peer object (initiator side)
})

signalClient.on('request', async (request) => {
  const { peer } = await request.accept() // Accept the incoming request
  peer // this is a fully-signaled simple-peer object (non-initiator side)
})

Client API

signalClient = new SignalClient(socket)

Create a new signalling client.

Required socket is a socket.io-client instance.

signalClient.id

The identifying string for this client's socket. null until discovery completes.

signalClient.discover(discoveryData)

Initiate discovery.

discoveryData is any discovery data to be sent to the server.

{ peer, metadata } = await signalClient.connect(id, [metadata], [peerOptions])

Request to connect to another client. Returns a Promise.

id is the signalClient.id of the other client.

Optional metadata is any serializable object to be passed along with the request.

Optional peerOptions are the options to be passed to the SimplePeer constructor.

signalClient.on('discover', function (discoveryData) {})

Fired when the client has connected to the server and done discovery.

discoveryData is any additional data that has been passed by the server.

signalClient.on('request', function (request) {})

Fired on receiving a request to connect from another client.

request.initiator

The id of the remote client's socket.

request.metadata

Any additional metadata passed by the requesting client or server.

{ peer, metadata } = await request.accept([metadata], [peerOptions])

Accept the request to connect. Not calling this method will ignore the request. Returns a Promise.

metadata is any serializable object to be passed along with the answer.

peerOptions are the options to be passed to the SimplePeer constructor.

Promise will reject if the other side calls reject().

request.reject([metadata])

Rejects the request to connect. Not calling this method will ignore the request.

metadata is any serializable object to be passed along with the rejection.

signalClient.peers()

List all currently connecting/connected peers. Returns an array of SimplePeer objects.

Server API

signalServer = require('simple-signal-server')(io)

Create a new signalling server.

Required io is a socket.io instance.

signalServer.on('discover', function (request) {})

Optional listener allows you to return additional discovery data when a new client connects or rediscovers.

request.socket

The socket.io socket used by the client initiating discovery.

request.discoveryData

Any additional data passed by the discovering client. A good place for credentials.

request.discover([id], [discoveryData])

Allow discovery to continue. Listening to "discover" and not calling this method will block discovery.

Optional id is the client ID that will be used to identify the client. Default is socket.id.

Optional discoveryData is any serializable object to be passed along with the discovery response. A good place for credentials.

signalServer.on('request', function (request) {})

Optional listener allows you to filter connection requests on the server.

request.initiator

id of the socket used by the client initiating the request.

request.target

id of the socket used by the client that will receive the request.

request.metadata

Any additional metadata passed by the requesting client.

request.forward([id], [metadata])

Allow the request to continue. Listening to "request" and not calling this method will block the request.

Optional id is the receiver of the request, allowing you to reroute requests to different clients.

Optional metadata is any serializable object to be passed along with the request.

signalServer.on('disconnect', function (socket) {})

Listens to disconnected sockets. Similar to the socket.io disconnect event, but only fires for clients that completed discovery.

simple-signal's People

Contributors

t-mullen avatar rationalcoding avatar d-asensio avatar mlsgrnt avatar tinchoz49 avatar ungoldman avatar

Watchers

Masse Fierro avatar  avatar Diego avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.