Coder Social home page Coder Social logo

Comments (7)

feross avatar feross commented on July 20, 2024

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.

from simple-peer.

dukevomv avatar dukevomv commented on July 20, 2024

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

from simple-peer.

feross avatar feross commented on July 20, 2024

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.

from simple-peer.

nicojanssens avatar nicojanssens commented on July 20, 2024

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.

from simple-peer.

tomcartwrightuk avatar tomcartwrightuk commented on July 20, 2024

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

from simple-peer.

feross avatar feross commented on July 20, 2024

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)
})

from simple-peer.

tomcartwrightuk avatar tomcartwrightuk commented on July 20, 2024

Ah I see. That makes more sense. Thanks.

from simple-peer.

Related Issues (20)

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.