Setup peerJS server combined with express

Francesco Giordano picture Francesco Giordano · Nov 3, 2014 · Viewed 7.2k times · Source

I'm trying to setup my own peerJS server following the readme on https://github.com/peers/peerjs-server#combining-with-existing-express-app

my code on server

port = process.env.PORT or 8080
http = require 'http'
express = require 'express'
app = express()
server = http.createServer app
app.use '/peerjs', ExpressPeerServer server, debug : on
server.listen port
server.listen 9000

my code on client

peer = new Peer
    host : 'localhost'
    port : 9000
    secure : no
    config :
        iceServers : [ url : 'stun:stun.l.google.com:19302' ]

I get this error on client console

GET http://localhost:9000/peerjs/peerjs/id?ts=14150106969530.4679094860330224 net::ERR_CONNECTION_REFUSED

Answer

udit picture udit · Jul 31, 2015

In case you are still looking for a way to establish your own peer server, then here is what I have done to make it work for me.

server.js

// initialize express
var express = require('express');
var app = express();
// create express peer server
var ExpressPeerServer = require('peer').ExpressPeerServer;

var options = {
    debug: true
}

// create a http server instance to listen to request
var server = require('http').createServer(app);

// peerjs is the path that the peerjs server will be connected to.
app.use('/peerjs', ExpressPeerServer(server, options));
// Now listen to your ip and port.
server.listen(8878, "192.168.1.14");

Client side code

I guess, you should not have much problem in this, but if you are wondering what to put for certain parameters, then here is the initialization of the peer object:

var peer = new Peer({
    host: '192.168.1.14',
    port: 8878,
    path: '/peerjs',
    config: { 'iceServers': [
    { url: 'stun:stun01.sipphone.com' },
    { url: 'stun:stun.ekiga.net' },
{ url: 'stun:stunserver.org' },
{ url: 'stun:stun.softjoys.com' },
{ url: 'stun:stun.voiparound.com' },
{ url: 'stun:stun.voipbuster.com' },
{ url: 'stun:stun.voipstunt.com' },
{ url: 'stun:stun.voxgratia.org' },
{ url: 'stun:stun.xten.com' },
{
    url: 'turn:192.158.29.39:3478?transport=udp',
    credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
    username: '28224511:1379330808'
},
{
    url: 'turn:192.158.29.39:3478?transport=tcp',
    credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
    username: '28224511:1379330808'
    }
  ]
   },

debug: 3
});

This should help you to establish the connection.