socketio / socket.io-p2p

MIT License
1.02k stars 180 forks source link

Selectively connect to peer ? #19

Open darkyen opened 8 years ago

darkyen commented 8 years ago

I think with socket.io it should be rather simple to do this by allowing a peer to connect to remote peers selectively.Maybe In a discovery based method ? This would give more control to the developer, additionally authentication of peers can than be handled serverside (allows enforcing rules).

sio.on('peer-discovered', function(peer){
   peer.connect();
});

and on the other hand (or maybe filter this in server)

sio.on('peer-request', async function(user, response){
   const shouldAnswer = await getConfirmationAsync();
   if( shouldAnswer ) return response.answer();
   response.reject();
});
W4G1 commented 4 years ago

You can achieve this by letting the peers you want to connect together join in a room and then exchange signalling data by calling p2pserver() on the server:

const server = require('http').createServer()
const p2pserver = require('socket.io-p2p-server').Server
const io = require('socket.io')(server)

server.listen(3030)

io.on('connection', socket => {
  socket.join(room) /* Join the room */

  p2pserver(socket, null, room) /* All sockets in the specified room will attempt to establish a WebRTC connection. */
})