Coder Social home page Coder Social logo

p2p's Introduction

WebRTC peer-to-peer

This is a browser JS library that makes it easy to manage RTC peer connections, streams and data channels. It's currently used in emscripten to provide data transport for the posix sockets implementation.

Requirements

You will need either Firefox Nightly, or Chrome Canary. You can also use Chrome Dev Channel.

What it does

  • Firefox (nightly) and Chrome (dev/canary) supported
  • Binary transport using arraybuffers (Firefox only!)
  • Multiple connections
  • Broker service (on nodejitsu), or run your own
  • Connection timeouts

What it doesn't do (yet!)

  • Interoperability between Firefox and Chrome
  • Peer brokering for establishing new connections through existing peer-to-peer

Quick start

Setting up a peer is easy. The code below will create a new peer and listen for incoming connections. The onconnection handler is called each time a new connection is ready.

// Create a new Peer
var peer = new Peer(
  'http://webrtcb.jit.su:80', // You can use this broker if you don't want to set one up
  {
    binaryType: 'arraybuffer',
    video: false,
    audio: false
  }
);

// Listen for incoming connections
peer.listen();

var connections = {};

// Handle new connections
peer.onconnection = function(connection) {
  // Store connections here so we can use them later
  connections[connection.id] = connection; // Each connection has a unique ID

  connection.ondisconnect = function(reason) {
    delete connections[connection.id];
  };
  
  connection.onerror = function(error) {
    console.error(error);
  };
  
  // Handle messages from this channel
  // The label will be 'reliable' or 'unreliable', depending on how it was received
  connection.onmessage = function(label, message) {
    console.log(label, message);
  };
  
  // Sends a message to the other peer using the reliable data channel
  connection.send('reliable', 'hi!'); 
  
  // The connection exposes the underlying media streams
  // You can attach them to DOM elements to get video/audio, if available
  console.log(connection.streams.local, connection.streams.remote);
  
  // Closes the connection
  // This will cause `ondisconnect` to fire
  connection.close();
};

// Print our route when it's available
peer.onroute = function(route) {
  // This is our routing address from the broker
  // It's used by peers who wish to connect with us
  console.log('route:', route);
};

peer.onerror = function(error) {
  console.log(error);
};

Another peer can connect easily to the one we made above by calling connect() on its routing address.

peer.connect(route);

Demo

There are some files in the demo directory that offer an example. You can load it here and open the connect URL in another window. For this example, the route is added to the URL query string so that the other peer can parse it and connect when the page loads, so all you need to share is the URL.

p2p's People

Contributors

luser avatar

Watchers

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