Giter Club home page Giter Club logo

socket.io-p2p's Introduction

This module provides an easy and reliable way to setup a WebRTC connection between peers and communicate using events (the socket.io-protocol).

Socket.IO is used to transport signalling data and as a fallback for clients where WebRTC PeerConnection is not supported.

How to use

Create a socket connection, pass it to P2P. On the Client:

var P2P = require('socket.io-p2p');
var io = require('socket.io-client');
var socket = io();

var p2p = new P2P(socket);

p2p.on('ready', function(){
  p2p.usePeerConnection = true;
  p2p.emit('peer-obj', { peerId: peerId });
})

// this event will be triggered over the socket transport
// until `usePeerConnection` is set to `true`
p2p.on('peer-msg', function(data){
  console.log(data);
});

If you're not using browserify, then use the included standalone file socketiop2p.min.js. This exports a P2P constructor on window.

On the server, use the socket.io-p2p-server to take care of signalling. All clients who support WebRTC data connections will exchange signalling data via the default / namespace.

var server = require('http').createServer();
var io = require('socket.io')(server);
var p2p = require('socket.io-p2p-server').Server;
io.use(p2p);
server.listen(3030);

WebRTC Peer connections can also be established by exchanging signalling data within a socket.io room. Do this by calling the p2p server within the connection callback:

var server = require('http').createServer();
var io = require('socket.io')(server);
var p2p = require('socket.io-p2p-server').Server;
server.listen(3030);

io.on('connection', function(socket){
  clients[socket.id] = socket;
  socket.join(roomName);
  p2p(socket, null, room);
});

See chat app for full example.

API

p2psocket = new P2P(socket, opts, cb)

Create a new socket.io-p2p connection.

The opts object can include options for setup of the overall socket.io-p2p connection as well as options for the individual peers - specified using peerOpts.

  • numClients - max number of peers each client can connect to; 5 by default.
  • autoUpgrade - upgrade to a p2p connection (from s.io one) when peers are ready; true by default
  • peerOpts - object of options to be passed to underlying peers. See here for currently supported options. See here for an example.

cb is an optional callback. Called when connection is upgraded to a WebRTC connection.

p2psocket.on('upgrade', function (data) {})

Triggered when P2P connection is converted to a WebRTC one.

p2psocket.on('peer-error', function (data) {})

Triggered when a PeerConnection object errors during signalling.

Roadmap of development

  • Support for packets containing multiple binary blobs - packets can only contain one blob in this version
  • Allow a peer to act as a relay between peers that don't support PeerConnection and those that do.

PRs and issue reports are most welcome.

socket.io-p2p's People

Contributors

tomcartwrightuk avatar rauchg avatar hharnisc avatar neopostmodern avatar warbrett avatar gerhardberger avatar mluby avatar sprusr avatar

Watchers

James Cloos avatar Daniel Seeley 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.