0

So I am coding this web on Heroku and I try to integrate a socket into the setup. But the problem is that I cannot make both my Heroku app and socket.io server listen to the same port.

The default port for Heroku seems to be 5000 and for socket.io seems to be 3000.

If I try to change either of the ports to the same as the other, then the EADDRINUSE error pops up.

However, if I don't, then the two processes seems to operate separately

when I run npm start, then the terminal says

Listening on 5000
listening on xoxo 3000

And when I turned on to localhost:3000 it actually works ( the socket.io ), but on localhost:5000 it obviously doesn't. As a matter of fact, it also doesn't work on the Website that Heroku generated for me (only the localhost:5000 showed up)

The code lines below are from my index.js file.

How could I fix this? Thank you in advance.

const express = require('express');
const app = express();
const path = require('path')
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
const PORT = process.env.PORT || 5000;
const INDEX = '/index.ejs'

app.listen(PORT, () => console.log(`Listening on ${ PORT }`))
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.get('/', (req, res) => res.render('index'));

io.on('connection', (socket) => {
  socket.on('playerEvent', (msg) => {
    console.log(msg);
  });
  console.log('a user connected');
  socket.on('disconnect', () => {
    console.log('user disconnected');
  });
  socket.broadcast.emit('hi');
});

server.listen(3000, () => {
  console.log('listening on xoxo 3000');
});
 //socket.on('playerEvent', function(msg){
  //  console.log(msg);
  //});
  • You cannot have more than one process listening on each port. – Alicia Sykes Apr 25 '22 at 10:30
  • @Lissy93 I made an edit to clarify my problem, could you help with more detailed solution? – Nikola Tolzsek Apr 25 '22 at 10:33
  • Use two separate dynos or a reverse proxy. AFAIK a dyno only forwards one port. – jabaa Apr 25 '22 at 10:35
  • It doesn't work with Heroku's demo socket.io site? That's weird. Are you using a single dyno? I believe it might require 2 + session affinity enabled – Alicia Sykes Apr 25 '22 at 10:37
  • Does this answer your question? [Recommended approach for multiport app using Heroku](https://stackoverflow.com/questions/41690066/recommended-approach-for-multiport-app-using-heroku) – jabaa Apr 25 '22 at 10:38
  • I also deployed this line from heroku instruction: heroku features:enable http-session-affinity – Nikola Tolzsek Apr 25 '22 at 10:39
  • @jabaa I tried, it doesn't. that's weird. as i followed instruction, it doesn't even bother running the second sever in the second file i included in Procfile. – Nikola Tolzsek Apr 25 '22 at 11:06
  • As far as I understand, you need two different apps. You can't start two different dynos in one app. – jabaa Apr 25 '22 at 11:10

0 Answers0