question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't get Peer server working with Express

See original GitHub issue

Hello,

I’ve got a simple code that doesn’t work :

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

app.get('/', function(req, res, next) {
    console.log("get /")
    res.send('Hello world!');
});

var server = app.listen(9000);

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

app.use('/api', ExpressPeerServer(server, options));

server.on('connection', function(id) {
    console.log(id)
  console.log(server._clients)
});

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

The code is from the Readme.

The issue is that I get a wrong id in the server.on(‘connection’, …) callback :

  • I want a text ID
  • I got a socket object…

From what I have studied, the signal we react to comes from onconnection in net.js. Here is the complete stacktrace of the event call :

main.js:25 (server.on(‘connection’, …) EventEmitter.emit (events.js:117) onconnection (net.js:1188)

Is there a way to fix this behaviour ?

Thanks

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
manh-vvcommented, Aug 20, 2018

I fix my problem after moving integrating peer part up.

const app = express();

/**
 * Create HTTP server.
 */
const server = http.createServer(app);

/* integrate peer server -- start */
const ExpressPeerServer = require('peer').ExpressPeerServer;
const peerServer = ExpressPeerServer(server, {
  debug: true
});

app.use('/peerjs', peerServer);
/* integrate peer server -- end */

app.use(cors());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

FYI: error happen with below code

const app = express();

/**
 * Create HTTP server.
 */
const server = http.createServer(app);

app.use(cors());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

/* integrate peer server -- start */
const ExpressPeerServer = require('peer').ExpressPeerServer;
const peerServer = ExpressPeerServer(server, {
  debug: true
});

app.use('/peerjs', peerServer);
/* integrate peer server -- end */
2reactions
ice3commented, Jan 21, 2016

I recommand to improve the docs for the project. I will see if I can help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Peer cli works but ExpressPeerServer doesnt - Stack Overflow
I am trying to get an express peerJs server up and running locally over HTTPS. Now if i start the peerJS server via...
Read more >
Lost connection to server. PeerServer with express app.
Small update, i get back to establishing peerjs server on express app. This time problem solved. Extremely simple, just by updating my node,...
Read more >
apollo-server-express - npm
Apollo Server is a community-maintained open-source GraphQL server that works with many Node.js HTTP server frameworks. Read the docs. Read the ...
Read more >
Taming WebRTC with PeerJS: Making a Simple P2P Web Game
A string that the peer can choose itself, or have a server generate one. ... Now that we have a source of confidence...
Read more >
Peer NPM
If you have your own server, you can attach PeerServer. ... If the server doesn't receive any data from client (includes pong messages),...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found