ERROR when running peer server with SSL

2,009 views
Skip to first unread message

suvi

unread,
Nov 18, 2016, 11:19:21 AM11/18/16
to PeerJS
Hello,

I moved my app from http to https and realised I needed to make changes to my peer server for it to be able to connect. I realised, as mentioned here: https://github.com/peers/peerjs-server that I need to add in the security cert and key to the server script, so I now have an index.js as such:

var fs = require('fs');
var ip = require('ip');
var PeerServer = require('peer').PeerServer;

var port = 9000;
var server = new PeerServer({port: port, allow_discovery: true});
var server = PeerServer({
  port: port,
  ssl: {
    key: fs.readFileSync('/path to key.key'),
    cert: fs.readFileSync('/path to cetificate.crt')
  },
  allow_discovery: true
});

server.on('connection', function (id) {
  console.log('new connection with id ' + id);
});

server.on('disconnect', function (id) {
  console.log('disconnect with id ' + id);
});

console.log('peer server running on ' +
            ip.address() + ':' + port);

But I am getting this error when trying to run the server:
peer server running on 172.31.46.6:9000
events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::9000
    at Object.exports._errnoException (util.js:907:11)
    at exports._exceptionWithHostPort (util.js:930:20)
    at Server._listen2 (net.js:1250:14)
    at listen (net.js:1286:10)
    at Server.listen (net.js:1382:5)
    at PeerServer (/home/ubuntu/www/UH_Web/node_modules/peer/lib/index.js:90:12)
    at Object.<anonymous> (/home/ubuntu/www/UH_Web/server/index.js:7:14)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)

It will then shutdown automatically. This is something going quite deep into peer script and I'm completely new to SSL so it is baffling my mind. It seems to be unhappy with the port, but it worked perfectly on http....

Any ideas?


suvi

unread,
Nov 20, 2016, 2:21:57 PM11/20/16
to PeerJS
Okay replying to my own message, I was an idiot. I had not commented out the old server, so I have now changed this to:

 var fs = require('fs');
var ip = require('ip');
var PeerServer = require('peer').PeerServer;

var port = 9000;
var server = new PeerServer({port: port, allow_discovery: true});
var server = PeerServer({
  port: port,
  ssl: {
    key: fs.readFileSync('/path to key.key'),
    cert: fs.readFileSync('/path to cetificate.crt')
  },
  allow_discovery: true
});

server.on('connection', function (id) {
  console.log('new connection with id ' + id);
});

server.on('disconnect', function (id) {
  console.log('disconnect with id ' + id);
});

console.log('peer server running on ' +
            ip.address() + ':' + port);

But, the browser js is still complaining that it cannot make the connection (see below). Looks like the socket is not happy - could there be something that I need to change for SSL in the socket....?

WebSocket connection to 'wss://MyURL:9000/peerjs?key=peerjs&id=pc1&token=573zm5jwygkgz9tpiixu323xr' failed: WebSocket opening handshake was canceled Socket._startWebSocket @ peer.self-7ba1e4f….js?body=1:1248
Socket.start @ peer.self-7ba1e4f….js?body=1:1236
Peer._initialize @ peer.self-7ba1e4f….js?body=1:872

suvi

unread,
Nov 20, 2016, 2:38:31 PM11/20/16
to PeerJS
Also, I am running the server by "node index.js", which I took from a tutorial, on the github page it says to run it by "peerjs --port 9000 --key peerjs", so I don't know whether this could be an issue.....
Message has been deleted

Никита Петренко

unread,
Dec 31, 2016, 7:55:15 PM12/31/16
to PeerJS
Hello! I solved this problem!
1) You must paste in your chrome:
chrome://flags/#allow-insecure-localhost
And click Enable
2) You must use "https://" in url.
3)There is my code that works!
******************************
*****************************************
var express = require("express");

var fs = require("fs");
var https = require("https");
var ExpressPeerServer = require("peer").ExpressPeerServer;
var bodyParser = require("body-parser");
var urlencodedParser = bodyParser.urlencoded({extended: false});
var jsonParser = bodyParser.json();
var const_and_funcs = require("./vk_space_chat_constants_and_general_functions.js");

var credentials = {
    key: fs.readFileSync('/etc/apache2/ssl/apache.key'),
    cert: fs.readFileSync('/etc/apache2/ssl/apache.crt')   
 }


var app = express();
var httpsServer = https.createServer(credentials, app).listen(const_and_funcs.PEER_PORT_ADDR);

var options = {
    debug: true,
};

var peerServer = ExpressPeerServer(httpsServer, options);
app.use(const_and_funcs.PEER_PATH_ADDR, peerServer);
app.use(jsonParser);
app.use(urlencodedParser);
************************************************************************************************************

Aishwarya Chaturvedi

unread,
Jul 7, 2017, 1:34:09 AM7/7/17
to PeerJS
Hello I understand that PEER_PORT_ADDR points to "the port" where I have to run my https server, but  I need to know what this points to const_and_funcs.PEER_PATH_ADDR , a port or something else? If it is a port will it be different from the one that i used to run https server.
Reply all
Reply to author
Forward
0 new messages