Nav apraksta

ericz ef4dc313bb update docs 12 gadi atpakaļ
bin ec5ec82e8e socket abstraction 12 gadi atpakaļ
demo b633066161 apikey -> key 12 gadi atpakaļ
deps 551acfddae deps submodules 12 gadi atpakaļ
dist ad95ce745d update dist 12 gadi atpakaļ
docs ef4dc313bb update docs 12 gadi atpakaļ
lib b43af07547 error handling 12 gadi atpakaļ
.gitignore ea9a3207cb submodules, gitignore, make 12 gadi atpakaļ
.gitmodules 551acfddae deps submodules 12 gadi atpakaļ
Makefile ea9a3207cb submodules, gitignore, make 12 gadi atpakaļ
README.md 8eba759061 next: xhr streaming 12 gadi atpakaļ
package.json a8ed5c84e4 actually use config 12 gadi atpakaļ

README.md

Peers: a node.js PeerConnection library

Chrome

  • Run Canary with --enable-data-channel

Firefox

Currently awaiting next Firefox WebRTC update.

  • Firefox flags: media.navigator.enabled: true media.navigator.permission.disabled: true media.peerconnection.enabled: true

Usage

Server

npm install peer

var PeerServer = require('peer').PeerServer({ port: 80, debug: true });

Client

<script type="text/javascript" src="/client/dist/peer.js"></script>

First peer

var connections = {};

p1 = new Peer({ host: 'localhost', port: '8000' });
p1.on('ready', function(id) {
  console.log(id); // => 'some_id_1'
});

p1.on('connection', function(connection) {
  // Sends a message to the other peer. This can even be a blob or JSON.
  connection.send('Hi there!');
  connection.send({ file: new Blob([1, 2, 3])});

  // Probably want to save the connection object.
  connections[connection.metadata.username] = connection;

  if (connection.metadata.username == 'spy') {
    connection.close();
  } else {
    // Add handler for connection data.
    connection.on('data', function(data) {
      console.log(data);
    }
  }
});

Second Peer

p2 = new Peer({ server: 'localhost' });
p2.on('ready', function(id) {
  console.log(id);

  p2.connect('some_id_1', { username: 'friend' }, function(err, connection) {
    connection.send('Hi, bye.');

    connection.close();
  });
});

Other events

  • Connection - close: Called when a peer disconnects.