0

Premising I'm new to sockets, I'm trying to set up the first configuration on server (Node w/Express).

While doing so, I encountered an issue where nodemon crashes every time I save, by returning Error: listen EADDRINUSE: address already in use :::8080.

Here the server implementation:

import express from "express";
import cors from "cors";
import { Server, Socket } from "socket.io";
import { createServer } from "http";
import morgan from "morgan";

// App configuration
const app = express();
const server = createServer(app);
const io = new Server(server);
const port = process.env.PORT || 8080;

// Middlewares
app.use(cors());
app.use(express.json());
app.use(morgan("combined"));

// Socket connection
io.on("connect", (socket: Socket) => {
  socket.send("test");
});

server.listen(port, () => {
  console.log(`Server is up and running on ${process.env.NODE_ENV} - port ${port}`);
});

I would think that happens as the socket.io connection never get closed / disconnected, leaving the server instance up listening.

I had a look around and found many q/a that were suggesting commands to find and kill the process, but I'd need a solution able to disconnect the socket automatically every time the express server gets turned down.

I tried the following:

io.on("disconnect", () => {
  io.sockets.disconnect();
}

As suggested here, but TS complains about io.sockets.disconnect() with a Property 'disconnect' does not exist on type 'Namespace<DefaultEventsMap, DefaultEventsMap>'.ts(2339).

Any idea how this should be tackled?

ale917k
  • 1,494
  • 7
  • 18
  • 37

1 Answers1

2

I think there may be an issue in socket io initialization. Can you try this type of initialization showed at https://socket.io/get-started/chat#Integrating-Socket-IO:

const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const io = require('socket.io')(server);

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', (socket) => {
  console.log('a user connected');
});

server.listen(3000, () => {
  console.log('listening on *:3000');
});

So basically pass server object to the default import from socket io like this(right now, you are using "Server" inside socket.io ):

import express from "express";
import cors from "cors";
import ioDefault, { Server, Socket } from "socket.io";
import { createServer } from "http";
import morgan from "morgan";

// App configuration
const app = express();
const server = createServer(app);
const io = ioDefault(server);
const port = process.env.PORT || 8080;

// Middlewares
app.use(cors());
app.use(express.json());
app.use(morgan("combined"));
// ...

UPDATE:

The issue was another server running on the same port. To check which process is running on a specific port you can run

netstat -anpe | grep "8080" | grep "LISTEN"

if netstat is not installed run sudo apt install net-tools in debian based systems(ubuntu, fedora and others)

you can then kill it with its pid:

kill -9 5454

srknzl
  • 776
  • 5
  • 13
  • I gave that a try, but unfortunately it seems that code contains the exact same problem (to trigger the error, try to copy the code on a server, then add a console.log(), save, remove the console.log() and save again) - I used your first version (ES6), but I'd guess the second would have the same behaviour too – ale917k Apr 11 '21 at 09:20
  • I see. the first code is taken from the linked socket io website, it's not meant to be used directly with your code. – srknzl Apr 11 '21 at 09:22
  • I replaced completely my code with what has been provided on their documentation, there was nothing custom with what I just tested – ale917k Apr 11 '21 at 09:27
  • Then I think there is a leftover server listening on port 8080. Try finding and killing it, or restart your pc. – srknzl Apr 11 '21 at 09:28
  • Already done it but that's not a solution as that error raises every time I save new changes - I can't turn down the server, look for the process and kill it every time I need to apply changes to the server. I need a solution to disconnect the socket connection instead – ale917k Apr 11 '21 at 09:30
  • 1
    No I am talking about another server other than your code. Normally you should be able run a socket.io server on top of your express server as shown in the docs. – srknzl Apr 11 '21 at 09:31
  • Oh wow, you are actually damn right! I have been listing processes on Linux by running `lsof -i :8080` but I couldn't see any process running apart the ones I was killing; do you perhaps know why that was not listed out? Or do you perhaps know more reliable commands for this? Either way, by restarting the pc I don't seem to have this problem any longer, so big thank you for your help! – ale917k Apr 11 '21 at 09:43
  • I found this `netstat -anpe | grep "8080" | grep "LISTEN"` at https://www.ibm.com/support/pages/how-can-i-check-if-application-listening-port-and-applications-name. To install netstat on debian(ubuntu is debian based) `sudo apt install net-tools` – srknzl Apr 11 '21 at 09:47