Question

socket.io + node.js + apache port error

Posted May 25, 2021 149 views
UbuntuApacheNode.js

I created an application I set up using apache on my ubuntu server.

My IP address is 159.203.115.144.

Currently, apache is listening on port 80 and port 8080.

My app is live and can be viewed at either port. http://159.203.115.144/
http://159.203.115.144:8080

I am using node.js to run the server-side at port 8000 with server.js

I cannot run server.js and apache on the same port.

When I try to change the port on server.js to 8080 or 80 I run into the following error:

events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::8080
    at Server.setupListenHandle [as _listen2] (net.js:1280:14)
    at listenInCluster (net.js:1328:12)
    at Server.listen (net.js:1415:7)
    at Object.<anonymous> (/root/server.js:17:8)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
Emitted 'error' event at:
    at emitErrorNT (net.js:1307:8)
    at pro```code```cess._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

How do I connect my application to socket.io using the same port?

Do I need to use a reverse proxy or is there a more simple way?

Here is my server.js code:

const server = require('http').createServer()
const io = require('socket.io')(server, { 
    cors: { 
         origin: '*', 
         methods: ["GET", "POST"],
         allowedHeaders: ["my-custom-header"],
         credentials: true
     }
});

const port = 8000;

// Listen for incoming connections
server.listen(port, (err) => {
    if (err) throw err
    console.log(`Listening on port ${port}`);
});
Submit an answer

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!