Peerjs: README example broken in 0.3.2

Created on 19 Mar 2020  ·  13Comments  ·  Source: peers/peerjs

I recently upgraded PeerJS from 0.2.10 to 0.3.2 and noticed it broke my project. I compared my implementation to the example to see what might have changed. In doing so, I noticed the the example provided in the README does not seem to actually work?

Here is a version of the example from the README using version 0.2.10:
https://glitch.com/~unruly-chinchilla

It works as expected. The unique ID is shown on the page and logged to the console

Here is the exact same project with the only difference being I've bumped the PeerJS version to 0.3.2:
https://glitch.com/~abyssinian-piper

It gives a 404 error in the console trying to access this endpoint:
https://abyssinian-piper.glitch.me/peerjs/peerjs/id

For both examples we are using the most up-to-date version of the client library (0.3.2).

Can you help me figure out why it's not working?

Let me know if you need more information. Thanks!

bug

Most helpful comment

Thanks @afrokick. You can see the config (and rest of the code) for the working example here:
https://glitch.com/edit/#!/unruly-chinchilla?path=server.js:2:0

You can see the config (and rest of the code) for the non-working example here:
https://glitch.com/edit/#!/abyssinian-piper?path=server.js:1:0

Does that show you what you're looking for?

All 13 comments

Maybe useful and related: I submitted a pull request fixing a typo in the README and saw that Travis was unhappy about it:
https://travis-ci.org/github/peers/peerjs-server/builds/664198688

could you please provide your config for server?

Thanks @afrokick. You can see the config (and rest of the code) for the working example here:
https://glitch.com/edit/#!/unruly-chinchilla?path=server.js:2:0

You can see the config (and rest of the code) for the non-working example here:
https://glitch.com/edit/#!/abyssinian-piper?path=server.js:1:0

Does that show you what you're looking for?

@georgemandis thanks! There was an issue with path in old version.

So, in new version, your path for id must be https://abyssinian-piper.glitch.me/peerjs/peerjs/peerjs/id ( additional peerjs :) )

Lets fix it!

const express = require('express');
const app = express();
const { ExpressPeerServer } = require('peer');

//app.get('/', (req, res, next) => { res.send('Hello world!'); });

// =======

const server = app.listen(process.env.PORT);

const options = {
    debug: true,
    path: '/xyz'
}

const peerserver = ExpressPeerServer(server, options);

app.use('/', peerserver);

app.use("/", express.static("public"));

The result will be http://domain.name/xyz - it is a path.
If we use secret as a key, the final url must be http://domain.name/xyz/secret/id for id API method.

It is a final code:

const express = require('express');
const app = express();
const { ExpressPeerServer } = require('peer');

//app.get('/', (req, res, next) => { res.send('Hello world!'); });

// =======

const server = app.listen(process.env.PORT);

const options = {
    debug: true,
    path: '/peerjs' // <-- changed
}

const peerserver = ExpressPeerServer(server, options);

app.use('/', peerserver); // <-- changed

app.use("/", express.static("public"));

Thank you @afrokick for your quick response. I'm still seeing errors in my example though?

You can see a difference 404 error in the console here:
https://abyssinian-piper.glitch.me/

It says it can't find this URL:
https://abyssinian-piper.glitch.me/peerjs/id

I have path option set to "/" on the client as well as on the server. I'm mounting the peerServer at /peerjs. I think that's how your example is setup, but please correct me if I'm wrong?

Here's the code for my server:
https://glitch.com/edit/#!/abyssinian-piper?path=server.js:1:0

And here's the code for my client:
https://glitch.com/edit/#!/abyssinian-piper?path=public/index.html:8:8

I also realize I posted this issue to the wrong repository! Sorry for that. I was following the example from the README for peerjs-server here: https://github.com/peers/peerjs-server

The example on that page is the one I followed and might be incorrect? The relevant code for the server from that example looks like this:

const peerServer = ExpressPeerServer(server, {
  debug: true,
  path: '/myapp'
});

app.use('/peerjs', peerServer);

And the client-side code looks like this:

const peerServer = PeerServer({
  port: 9000,
  path: '/myapp',
  generateClientId: customGenerationFunction
});

This still gives me the 404 error even after I changed the path to "/" like you showed me here.

Does that make sense?

Your client code is incorrect! @georgemandis

const peer = new Peer({
        host: location.hostname,
        port: location.port,
        path: "/peerjs" // <- change to this value
      });

@georgemandis Sorry, I found an issue. I did the test for PeerServer but not for ExpressPeerServer. I'll try to fix it.

Just now you can fix it by change path in app.use('/', peerserver); // <-- changed

Server side issue, will fix in 0.5.1

Fixed. My example works on 0.5.1 https://prairie-six-ray.glitch.me

@georgemandis thanks for help!

Thanks for your prompt attention to all of this @afrokick ! Big fan of your project and happy to see you making improvements. :-D

Side question: are examples in the API docs (https://peerjs.com/docs.html#api) available in a repo somewhere? If so, would you be amenable to some edits and/or perhaps modernizing some of the examples to use await/async and maybe other more ES6+ friendly features?

@georgemandis unfortunately not, but I have a repo with p2p chat based no react https://github.com/afrokick/peerjs-examples

Was this page helpful?
0 / 5 - 0 ratings