peerjs working on loaclhost but not on heroku?

Viewed 762

I'm running my node server on 3000 port and peer server on port 3001.In this scenario its working properly.But when deployed over heroku i'm running my server at 3000 and peer server over 443. In this scenario peerjs not wroking. It might be port alloction issue i guess but i'm unable to find the issue.

peer.js

const myPeer = new Peer( {
secure:true,
host: 'my-app-name.herokuapp.com',
port: 443
})

server.js

const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));

github link to project : link

New to Heroku. Any help will be appreciated!

4 Answers

Add this to your server file:

var ExpressPeerServer = require("peer").ExpressPeerServer;    
var options = {
  debug: true,
  allow_discovery: true,
};
let peerServer = ExpressPeerServer(server, options);
app.use("/peerjs", peerServer);

And call on client side like this:

var peer = new Peer({
                host: "yoursite.herokuapp.com",
                port: "",
                path: "/peerjs",
              });

You Have to Host Two Apps on Heroku. First Your Main App and Second Your PeerJS Server. Because You Cannot Host your App On different Port (i.e. https://your-app-name.herokuapp.com:5000). And Then You can Connect Your Main App PeerJS Client With Your PeerJS Server by using this.

const myPeer = new Peer( {
     secure:true,
     host: 'my-peerjs-server-name.herokuapp.com',
     port: 443
})

Happy Coding!

Just use this Heroku Element to deploy your own peer server with zero configuration. Connect to it from your client providing the host attribute as the url of your Heroku app without the https:// part and you may need to also set secure to true.

    {
      host: "you_app_name.herokuapp.com", // exclude protocol
      secure: true
    }

add this in server(index,app) file

const { ExpressPeerServer } = require("peer")
const peerServer = ExpressPeerServer(server, {
    debug: true
})
app.use("/peerjs", peerServer);

and in client side add this

const myPeer = new Peer(undefined, {
    path: "/peerjs",
    host: "/", 
    port: "443",
})

port should same as your server.listen(port) This will give invalid frame header for socket io but its fine

Related