Marko Nikolov
Marko Nikolov

Reputation: 825

Reconnect peers without getting a new SDP

I have this demo code where I connect two peers and I share a string between them. I'm doing this all in one place for the sake of testing. My code successfully creates a connection between the two and shares the messages. What I want to do is terminate the connection between them and create a new one without getting new SDP's from the signalling server i.e. I want to reconnect the same two peers with the SDP's they had from their previous session that got destroyed. Is this even possible? Due to the nature of the project I'm working on I am required to create and destroy data channels i.e. create and destroy connections every few seconds and given the number of users, there is a huge strain on our signalling server. Thank you.

var peer1 = new SimplePeer({
  initiator: true,
  trickle: false,
  config: {
    iceServers: ["CONFIG PLACEHOLDER"]
  },
  objectMode: true,
})

var peer2 = new SimplePeer({
  initiator: false,
  trickle: false,
  config: {
    iceServers: ["CONFIG PLACEHOLDER"]
  },
  objectMode: true,
})

peer1.on('signal', function (data) {
  peer2.signal(data)
})

peer2.on('signal', function (data) {
  peer1.signal(data)
})

peer1.on('connect', function () {
  peer1.send('hey peer2, how is it going?')
})

peer2.on('connect', function () {
  peer2.send('hey peer1, how is it going?')
})

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

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

Upvotes: 0

Views: 183

Answers (1)

1) No, you can't. Reconnection requires new SDP to be exchanged. 2) No, you don't have to. You can create new datachannels and close old ones on either of the peerconnections without exchanging new SDP betweeen the parties.

The SDP has to provide for connecting datachannels - so add the first one before the initial offer/answer. But after that, you can add and remove datachannels as much as you like.

Upvotes: 1

Related Questions