Carlo Casiglia
Carlo Casiglia

Reputation: 1

Can't get a self hosted peerJS server to work

I'm trying to realize a simple 2-peers videochat on an internal network for my workplace. I've written the client-side code, and it works: if I connect to peerjs using a peerjs API key the connection is established correctly. But there is a problem: the videostream is incredibly slow (sometimes totally frozen). I guess this is because the free peerjs server I'm connecting to is too weak, so I want to try to host it myself.

I've tried all the ways and deeply looked out for help, but couldn't get over it. I've tried combining it with express, or just creating a PeerServer, following the instructions I've found on https://github.com/peers/peerjs-server. I've tried with a simple 'npm install peer' and then 'peerjs --port 9000'. I've tried with heroku. I've tried all the combinations of ports, paths and snippets of code I could find, but no way. Also, I've gladly resulted to understand a lot about peerjs, but still no luck.

Can someone who has successfully run a peerjs server explain me where I'm wrong?

Here is my client side minimal code (commented lines are lines I've tried with no success):

var name = prompt('What's your name?');

var peer = new Peer(name, { 
  host: 'https://videodesk-ennesimo.herokuapp.com/',
  port: 9000,
  //key: 'peerjs',
  //path: '/peerjs',
  //secure: true
});

peer.on('open', function(name){
  alert(name + ' connected');
});

Everything works fine with

var peer = new Peer(name, { key: 'mypeerjsfreeapikey' });

but as said before, it is almost frozen. You can check my peer server is running at https://videodesk-ennesimo.herokuapp.com/

Other tries I've made are for example:

var express = require('express');
var app = express();
var ExpressPeerServer = require('peer').ExpressPeerServer;

var options = {
    debug: true,
    allow_discovery: true
}

var server = require('http').createServer(app);

app.use('/peerjs', ExpressPeerServer(server, options));
server.listen(9000, 'localhost');

with subsequent modification to client side code.

I must underline that my client side is a node.js app made with express, and that I'm using a self signed SSL certificate because Google Chrome won't allow the webcam on unsafe connections.

Any help would be useful. I'm stuck on this since last week and I can't waste any more time. Also I don't know what to try anymore. Also a working alternative to peerjs would be useful.

Thank you very much!

Upvotes: 0

Views: 5260

Answers (3)

Akshay Khanna
Akshay Khanna

Reputation: 21

The main problem lies in your client-side code, you've written...

host: 'https://videodesk-ennesimo.herokuapp.com/',

but in the case of the host, you either write

host: 'localhost' or 'your-app-name.herokuapp.com' without any https or the backward slash at the end.

Your correct code should be

var name = prompt('What's your name?');

var peer = new Peer(name, { 
  host: 'videodesk-ennesimo.herokuapp.com',
  port: 9000,
  secure: true
});

peer.on('open', function(name){
  alert(name + ' connected');
});

This worked for me! Wishing you luck & happy coding.

Upvotes: 2

Baptiste Martinet
Baptiste Martinet

Reputation: 43

A bit late to the party but your code wasn't working because the port automatically assigned by heroku is 443 & you have a backslash at the endof your host address.

Here is how I connected to your peerjs server.

var peer = new Peer({secure: true, host: 'videodesk-ennesimo.herokuapp.com', port: 443, path: '/'});

Still trying to make my own server working tho haha.

Upvotes: 1

Mikkel
Mikkel

Reputation: 7777

Carlo,

First of all, welcome to Stack Overflow.

Peerjs is only necessary while setting up a connection, and it has no part in the subsequent video call, so your assertion of it being a weak server is not right.

If two computers need to connect, the challenge is to work out how to connect them. This is what peerjs does, it looks at the possible ip address/port combinations that will work. Usually it lives in the cloud, so that it will allow machines behind firewalls to connect that would normally not be able to reach each other.

If you are connecting on a LAN, you could run the peerjs server on the lan, and it will work. But that isn't your problem, something is causing a video performance issue, maybe something is throttling the stream somehow.

Try putting both machines on the same router, or even creating a private network for them. This will convince you that peerjs is not the problem. Then you can trouble shoot why you are getting poor video performance.

Upvotes: 2

Related Questions