Skip to content

Instantly share code, notes, and snippets.

@adammw
Last active Jan 1, 2021
Embed
What would you like to do?
simple-peer / socket.io test
var Peer = require('simple-peer');
var io = require('socket.io-client');
var debug = require('debug')('client');
var socket = io.connect();
var peers = {};
var useTrickle = true;
socket.on('connect', function() {
debug('Connected to signalling server, Peer ID: %s', socket.id);
});
socket.on('peer', function(data) {
var peerId = data.peerId;
var peer = new Peer({ initiator: data.initiator, trickle: useTrickle });
debug('Peer available for connection discovered from signalling server, Peer ID: %s', peerId);
socket.on('signal', function(data) {
if (data.peerId == peerId) {
debug('Received signalling data', data, 'from Peer ID:', peerId);
peer.signal(data.signal);
}
});
peer.on('signal', function(data) {
debug('Advertising signalling data', data, 'to Peer ID:', peerId);
socket.emit('signal', {
signal: data,
peerId: peerId
});
});
peer.on('error', function(e) {
debug('Error sending connection to peer %s:', peerId, e);
});
peer.on('connect', function() {
debug('Peer connection established');
peer.send("hey peer");
});
peer.on('data', function(data) {
debug('Recieved data from peer:', data);
});
peers[peerId] = peer;
});
<!doctype html>
<html>
<head>
</head>
<body>
<script src="/js/bundle.js"></script>
</body>
</html>
{
"name": "simple-peer-test",
"version": "1.0.0",
"description": "",
"dependencies": {
"browserify-middleware": "^6.0.0",
"debug": "^2.2.0",
"express": "^4.13.1",
"lodash": "^3.10.0",
"simple-peer": "^5.11.3",
"socket.io": "^1.3.5",
"socket.io-client": "^1.3.5"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"repository": {
"type": "git",
"url": "git+https://gist.github.com/d9bf021c395835427aa0.git"
},
"license": "MIT",
"homepage": "https://gist.github.com/d9bf021c395835427aa0"
}
var _ = require('lodash');
var http = require('http');
var browserify = require('browserify-middleware');
var debug = require('debug')('server');
var express = require('express');
var app = express();
var server = http.Server(app);
var io = require('socket.io')(server);
var DEFAULT_PEER_COUNT = 5;
app.use(express.static(__dirname));
app.get('/js/bundle.js', browserify(['debug', 'lodash', 'socket.io-client', 'simple-peer', {'./client.js': {run: true}}]));
io.on('connection', function (socket) {
debug('Connection with ID:', socket.id);
var peersToAdvertise = _.chain(io.sockets.connected)
.values()
.without(socket)
.sample(DEFAULT_PEER_COUNT)
.value();
debug('advertising peers', _.map(peersToAdvertise, 'id'));
peersToAdvertise.forEach(function(socket2) {
debug('Advertising peer %s to %s', socket.id, socket2.id);
socket2.emit('peer', {
peerId: socket.id,
initiator: true
});
socket.emit('peer', {
peerId: socket2.id,
initiator: false
});
});
socket.on('signal', function(data) {
var socket2 = io.sockets.connected[data.peerId];
if (!socket2) { return; }
debug('Proxying signal from peer %s to %s', socket.id, socket2.id);
socket2.emit('signal', {
signal: data.signal,
peerId: socket.id
});
});
});
server.listen(process.env.PORT || '3000');
@falvhb

This comment has been minimized.

Copy link

@falvhb falvhb commented Jan 23, 2018

lodash v4 change: use sampleSize instead of sample in server.js

@DanielOX

This comment has been minimized.

Copy link

@DanielOX DanielOX commented Jun 11, 2019

can u please tell me that what is the proper to destroy simple-peer peer connection because upon calling peer.destroy() i get cannot signal after peer is destroyed

@GabrieleCicconetti

This comment has been minimized.

Copy link

@GabrieleCicconetti GabrieleCicconetti commented Oct 24, 2020

Sorry, can you explain where the peer connection take place? i don't understand this :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment