3

I'm trying to use PeerJS for establishing a WebRTC data channel connection between peers. Specifically, I'd like one user to act as a host that can have multiple listeners and the listeners do not interact with each other, but only with the host. I've been able to get this working when running a PeerJS server on localhost, but trying to use it over the internet, either by running a signaling server on Heroku or using PeerJS's own cloud server, a connection is never established between the browsers. This is true even when trying to just do this with two tabs in the same browser, two computers on the same local network, turning and disabling my Firewall. I also tried signing up for and configuring the PeerJS client to use the viagenie free TURN server, but this also changed nothing.

Turning on debugging shows that the connection is established with the signaling server and that ICE candidates are exchanged:

Peer 1 (Host):

PeerJS:  Server message received:
  {
    dst: "hoecz5335d000000"
    payload: {candidate: {…}, type: "data", connectionId: "dc_l6r8e69vrim"}
    src: "i1ondf126sp00000"
    type: "CANDIDATE"
  }

Peer 2 (Listener):

PeerJS: Received ICE candidates for hoecz5335d000000: RTCIceCandidate
PeerJS:  Received ICE candidates for hoecz5335d000000: RTCIceCandidateaddress: "<IP_ADDRESS>"candidate: "candidate:961511463 1 tcp 1518280447 <IP_ADDRESS> 9 typ host tcptype active generation 0 ufrag N571 network-id 1 network-cost 10"component: "rtp"foundation: "961511463"port: 9priority: 1518280447protocol: "tcp"relatedAddress: nullrelatedPort: nullsdpMLineIndex: 0sdpMid: "0"tcpType: "active"type: "host"usernameFragment: "N571"__proto__: RTCIceCandidate
PeerJS:  Socket open
PeerJS:  Server message received: Objecttype: "OPEN"__proto__: Object
PeerJS:  Received ICE candidates for hoecz5335d000000: RTCIceCandidate {candidate: "candidate:453802058 1 udp 41885439 <IP_ADDRESS> …eration 0 ufrag N571 network-id 1 network-cost 10", sdpMid: "0", sdpMLineIndex: 0, foundation: "453802058", component: "rtp", …}

Logs in heroku also show that the clients are indeed being connected.

Here's what I have for code:

Creating a session (using the host's ID as the session ID)

const peer = new Peer({
  debug: 3,
  config: {
    iceServers: [{
      urls: 'stun:stun.l.google.com:19302',
    }, {
      urls: 'turn:numb.viagenie.ca',
      credential: '<CREDENTIAL>',
      username: '<USERNAME>'
    }],
    iceTransportPolicy: 'all'
  },
  host: '<APP_NAME>.herokuapp.com',
  secure: true
});

peer.on('error', console.log);

peer.on('open', () => {
  peer.on('connection', connection => {
    console.log('new connection');
    connection.on('open', () => {
      console.log('opened new connection');
      setInterval(() => {
        connection.send('Test');
      }, 5000);
    });
  });
});

Joining a session

// Same peer initialization as session creation

const connection = peer.connect(sessionId); // sessionId is the host's ID

connection.on('open', () => {
  console.log('Connected');
  connection.on('data', console.log); // Log any new data messages
  peer.disconnect(); // Disconnect listener from signaling server once connection with host has opened
});

connection.on('error', console.log);

Does anyone have suggestions on how to get the two peers to actually connect here? I'm at a total loss for how to continue in this at this point and I'm having to consider a more traditional WebSocket relay server at this point which I'd much rather avoid to keep server load light and preferably free.

TechyTech
  • 455
  • 2
  • 13

1 Answers1

0

I encountered this issue with the version described in the official documentation

<script src="https://unpkg.com/peerjs@1.3.2/dist/peerjs.min.js"></script>

Switching to this version solved it for me:

<script src="https://unpkg.com/peerjs@1.4.7/dist/peerjs.min.js"></script>

See also https://github.com/peers/peerjs/issues/864

fivef
  • 2,397
  • 21
  • 21