Tagada
Tagada

Reputation: 1

Why simple peer does not work in nodeJS?

Im trying to use this example on this github https://github.com/feross/simple-peer

var Peer = require('simple-peer')
var p = new Peer({ initiator: location.hash === '#1', trickle: false })

p.on('error', function (err) { console.log('error', err) })

p.on('signal', function (data) {
  console.log('SIGNAL', JSON.stringify(data))
  document.querySelector('#outgoing').textContent = JSON.stringify(data)
})

document.querySelector('form').addEventListener('submit', function (ev) {
  ev.preventDefault()
  p.signal(JSON.parse(document.querySelector('#incoming').value))
})

p.on('connect', function () {
  console.log('CONNECT')
  p.send('whatever' + Math.random())
})

p.on('data', function (data) {
  console.log('data: ' + data)
})

But when i try to execute this code, i have an error: ReferenceError: location is not defined :(

and when I try to delete all the arguments in the brackets like this just to test:

var p = new Peer()

I have an other error: Error: No WebRTC support: Specify opts.wrtc option in this environment

But... How to fix those error to make it work ?

Upvotes: 0

Views: 3448

Answers (3)

Ayesha Khan
Ayesha Khan

Reputation: 474

  • First install 'wrtc' module in you application by running the command: npm insatll wrtc
  • Then you need to import it: var wrtc = require('wrtc');
  • Next step is to pass wrtc argument to both the peers: var peer1 = new Peer({ initiator: true, wrtc: wrtc }) var peer2 = new Peer({ wrtc: wrtc })

After running this .js file, both peers will establish the connection between them and can start communication but on server not in browser.

Upvotes: 0

Jacklyn Lim
Jacklyn Lim

Reputation: 115

It is now supported by specifying wrtc: wrtc when you're initiating the connection. Source: https://www.npmjs.com/package/simple-peer#in-node

var wrtc = require('wrtc');

var peer = new SimplePeer({ initiator: true, wrtc: wrtc })

Upvotes: 2

Igor Vujovic
Igor Vujovic

Reputation: 419

You are starting example in nodejs (on server), and there is no window.location on server. You need to start this example from client (browser). There is no need for server for p2p (only for initial connection data exchange)

Upvotes: 0

Related Questions