Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can this module be used to connect more than 2 peers? #7

Closed
dukevomv opened this issue Oct 23, 2014 · 7 comments
Closed

Can this module be used to connect more than 2 peers? #7

dukevomv opened this issue Oct 23, 2014 · 7 comments
Labels

Comments

@dukevomv
Copy link

Is there a way to connect more than 2 browsers together and have a 5 member peer2peer "call"?

@feross
Copy link
Owner

feross commented Oct 24, 2014

The simplest way to do that is to create a full-mesh topology. That means that every peer opens a connection to every other peer. To illustrate:

networktopology-fullyconnected

Then, you can just iterate over all the peers and call peer.send when you want to broadcast a message.

@dukevomv
Copy link
Author

Great news! and one last question about this, by having a full-mesh topology does it makes each peer to load 5 times slower, or its up to me to make it fast and keep a low internet useage?

Sorry for bad english

@feross feross changed the title Q: More than 2 browsers Can this module be used to connect more than 2 peers? Oct 25, 2014
@feross
Copy link
Owner

feross commented Oct 25, 2014

Well, say you have 5 peers. Then, when a peer wants to send some data it must send it 4 times, once to each of the other peers. So you're going to want to be a bit careful about the size of the data you send.

Full mesh topologies don't scale well. The total number of edges in the network will be be03bf93bc0ea124ffee997605a71e68 where n is the number of peers.

@nicojanssens
Copy link

I did some experiments in the past, and once the number of participants starts growing it becomes (much) more effective to distribute data between producer and consumers in a tree based fashion -- instead of using a mesh network. Obvious drawback of this approach is that it breaks the P2P serverless architecture, as you need an MCU type of server in the backend to create this tree-based overlay network. My prototype was based on the Erizo project, which I had to extend to glue multiple servers together into an overlay tree.

@tomcartwrightuk
Copy link

When extending the example in the readme to use 3 peers, a Failed to set remote answer sdp: Called in wrong state: STATE_RECEIVEDACCEPT error occurs. I assume this is because the peers are have received an answer and are now waiting for an candidate data, but then they receive another answer from the third peer. Is this correct? Here is a gist of the extended example: https://gist.github.com/tomcartwrightuk/396851af44e8315e378e

@feross
Copy link
Owner

feross commented Dec 18, 2014

No, you need to create a peer object for each connection. Like this:

Peer 1

// These are peer1's connections to peer2 and peer3
var peer2 = new SimplePeer({ initiator: true })
var peer3 = new SimplePeer({ initiator: true })

peer2.on('signal', function (data) {
  // send this signaling data to peer2 somehow
})

peer2.on('ready', function () {
  peer2.send('hi peer2, this is peer1')
})

peer2.on('message', function (data) {
  console.log('got a message from peer2: ' + data)
})

peer3.on('signal', function (data) {
  // send this signaling data to peer3 somehow
})

peer3.on('ready', function () {
  peer3.send('hi peer3, this is peer1')
})

peer3.on('message', function (data) {
  console.log('got a message from peer3: ' + data)
})

Peer 2

// These are peer2's connections to peer1 and peer3
var peer1 = new SimplePeer()
var peer3 = new SimplePeer({ initiator: true })

peer1.on('signal', function (data) {
  // send this signaling data to peer1 somehow
})

peer1.on('ready', function () {
  peer1.send('hi peer1, this is peer2')
})

peer1.on('message', function (data) {
  console.log('got a message from peer1: ' + data)
})

peer3.on('signal', function (data) {
  // send this signaling data to peer3 somehow
})

peer3.on('ready', function () {
  peer3.send('hi peer3, this is peer2')
})

peer3.on('message', function (data) {
  console.log('got a message from peer3: ' + data)
})

Peer 3

// These are peer3's connections to peer1 and peer2
var peer1 = new SimplePeer()
var peer2 = new SimplePeer()

peer1.on('signal', function (data) {
  // send this signaling data to peer1 somehow
})

peer1.on('ready', function () {
  peer1.send('hi peer1, this is peer3')
})

peer1.on('message', function (data) {
  console.log('got a message from peer1: ' + data)
})

peer2.on('signal', function (data) {
  // send this signaling data to peer2 somehow
})

peer2.on('ready', function () {
  peer2.send('hi peer2, this is peer3')
})

peer2.on('message', function (data) {
  console.log('got a message from peer2: ' + data)
})

@tomcartwrightuk
Copy link

Ah I see. That makes more sense. Thanks.

@feross feross closed this as completed Dec 29, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants