peerjs with ssl, https and express

Created on 7 Jul 2017  路  1Comment  路  Source: peers/peerjs

I am writing an app using https, express and peerjs ssl, below i have mentioned the code
`const express = require('express')
const app = express()
const https = require('https')
const path = require('path')
const fs = require('fs')
const ExpressPeerServer = require('peer').ExpressPeerServer

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

const server = https.createServer(options, app)

const io = require('socket.io')(server)

var PeerServer = require('peer').PeerServer({port: 8000,
proxied: true,
ssl: {key: fs.readFileSync('/etc/apache2/ssl/apache.key'),
certificate: fs.readFileSync('/etc/apache2/ssl/apache.crt')}},
() => {
console.log('running peerserver')
})
//
app.use('/peerjs', PeerServer)

app.use(express.static(path.join(__dirname, 'public')))

server.listen(3000, (req, res) => {
console.log('listening on port 3000')
})

app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public/index.html'))
})`

below is client side code
const peerObj = { host: '0.0.0.0', port: 9000, path: '/peerjs', debug: 3, config: {icerServers: [ { url: 'stun:stun1.l.google.com:19302' }, { url: 'turn:numb.viagenie.ca', credential: 'muazkh', username: '[email protected]' } ]} } peer = new Peer(peerObj)

After this when I run my app, it give me this error

GET https://0.0.0.0:3000/peerjs/peerjs/id?ts=14994123658290.8549044374474446 net::ERR_INSECURE_RESPONSE
PeerJS: ERROR Error retrieving ID undefined

Most helpful comment

Your client tries to connect to the secure endpoint using port 443. So change your express config like :

var PeerServer = require('peer').PeerServer({port: 443,
proxied: true,
ssl: {key: fs.readFileSync('/etc/apache2/ssl/apache.key'),
certificate: fs.readFileSync('/etc/apache2/ssl/apache.crt')}},
() => {
console.log('running peerserver')
})

I had the same issue. Now got it working in the end. This is my working code I used yesterday actually :

var express = require('express');
var app = express();
var PORT = process.env.PORT || 3000
var ExpressPeerServer = require('peer').ExpressPeerServer;
var path = require('path')
var http = require('http');
var https = require('https');
var fs = require('fs');

var sslOptions = {
  key: fs.readFileSync('key.key'),
  cert: fs.readFileSync('cert.cert')
};

var server = http.createServer(app).listen(PORT);;
https.createServer(sslOptions, app).listen(443)

// CORS
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

app.use('/peer', ExpressPeerServer(server, {debug:true}));
app.use('/', express.static(path.join(__dirname, '/client')))

>All comments

Your client tries to connect to the secure endpoint using port 443. So change your express config like :

var PeerServer = require('peer').PeerServer({port: 443,
proxied: true,
ssl: {key: fs.readFileSync('/etc/apache2/ssl/apache.key'),
certificate: fs.readFileSync('/etc/apache2/ssl/apache.crt')}},
() => {
console.log('running peerserver')
})

I had the same issue. Now got it working in the end. This is my working code I used yesterday actually :

var express = require('express');
var app = express();
var PORT = process.env.PORT || 3000
var ExpressPeerServer = require('peer').ExpressPeerServer;
var path = require('path')
var http = require('http');
var https = require('https');
var fs = require('fs');

var sslOptions = {
  key: fs.readFileSync('key.key'),
  cert: fs.readFileSync('cert.cert')
};

var server = http.createServer(app).listen(PORT);;
https.createServer(sslOptions, app).listen(443)

// CORS
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

app.use('/peer', ExpressPeerServer(server, {debug:true}));
app.use('/', express.static(path.join(__dirname, '/client')))

Was this page helpful?
0 / 5 - 0 ratings