Home » Nodejs » Nodejs & socket io : warn – error raised: Error: listen EADDRINUSE

Nodejs & socket io : warn – error raised: Error: listen EADDRINUSE

Posted by: admin November 30, 2017 Leave a comment

Questions:

I am giving a try to Node.js with socket.io

Now here is my scenario i am ubuntu 12.04 user and i have folder pp on desktop

inside that i am putted server file i.e app.js

Here is the content

var fs = require('fs')
    , http = require('http')
    , socketio = require('socket.io');

var server = http.createServer(function(req, res) {
    res.writeHead(200, { 'Content-type': 'text/html'});
    res.end(fs.readFileSync(__dirname + '/index.html'));
}).listen(8080, function() {
    console.log('Listening at: http://localhost:8080');
});

socketio.listen(server).on('connection', function (socket) {
    socket.on('message', function (msg) {
        console.log('Message Received: ', msg);
        socket.broadcast.emit('message', msg);
    });
});

Now in the same folder i have another file index.html like

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        $(function(){
            var iosocket = io.connect();

            iosocket.on('connect', function () {
                $('#incomingChatMessages').append($('<li>Connected</li>'));

                iosocket.on('message', function(message) {
                    $('#incomingChatMessages').append($('<li></li>').text(message));
                });
                iosocket.on('disconnect', function() {
                    $('#incomingChatMessages').append('<li>Disconnected</li>');
                });
            });

            $('#outgoingChatMessage').keypress(function(event) {
                if(event.which == 13) {
                    event.preventDefault();
                    iosocket.send($('#outgoingChatMessage').val());
                    $('#incomingChatMessages').append($('<li></li>').text($('#outgoingChatMessage').val()));
                    $('#outgoingChatMessage').val('');
                }
            });
        });
    </script>
</head>
<body>
Incoming Chat: <ul id="incomingChatMessages"></ul>


<input type="text" id="outgoingChatMessage">
</body>

when i am trying to run the app.js useing node like

 node app.js 

I am getting the error

 warn  - error raised: Error: listen EADDRINUSE

I go through some doc and found that port are busy so restarted the system but still i am getting the same error .

Please tell me what might i am doing wrong .

Answers:

Try another port. 8080 is usually used by tomcat e.g.

Use netstat -a -v to know which port are currently used.

Questions:
Answers:

It looks to me that your not connecting to the host on the client side:

var iosocket = io.connect('http://localhost:8080');

Questions:
Answers:

You are not connecting to your server, this is wrong:

var iosocket = io.connect();

this is right:

var iosocket = io.connect('http://localhost:8080');

Also your port 8080 is used on your server by “http-proxy”, try an other port.

Questions:
Answers:

An ultra-silly/simple possibility is to make sure you don’t have multiple terminal windows open. I got this error and realized I had a second pre-existing connection that I had started up my app with. When I closed it and restarted the app the error went away.

Questions:
Answers:

Here is an example for beginners of how you can set up NodeJS + Express + SocketIO so that you have no EADDRINUSE error for the second time you start your app:

'use strict';

// Init
var http = require('http');
var express = require('express');
var socketIO = require('socket.io');
var cors = require('cors');

// Create Server app
var app = express();
var server = http.createServer(app);
var io = socketIO(server);

// Socket.io settings
io.set('browser client minification', true);
io.set('browser client etag', true);
io.set('browser client gzip', true);
io.set('browser client expires', true);

// Starting Express server
server.listen(serverPort, function() {
  console.log(chalk.green('Server is running on localhost:' + serverPort + '...'));
});

// Middleware
app.use(cors());

// Routes
app.get('/', function(req, res) {
    res.send("Hello world!");
    console.log("Root visited!");
    // Broadcast socketIO message inside Express
    io.emit('new_message', "Hello world!");
});

// Socket.io
var connections = [];
var title = "Untitled";

io.sockets.on('connection', (socket) => {

    socket.once('disconnect', () => {
        connections.splice(connections.indexOf(socket), 1);
        socket.disconnect();
        console.log('Disconnected: %s. Remained: %s.', socket.id, connections.length)
    });

    // ...

    connections.push(socket);
    console.log('Connected: %s. Total: %s', socket.id, connections.length);
});

I think this is a useful example and I hope it will help someone who encountered a problem setting this all up.