Dylan McGrath
Dylan McGrath

Reputation: 151

Chat application won't run on port 80 with socket.io and node.js./express

I'm trying to implement the most rudimentary socket.io/node.js application on my site. I want to take run it on port 80(http) instead of port 3000 in the app.js, for whatever reason, it breaks the whole application server side. I am literally only changing server.listen(3000) to server.listen(80), and it breaks the application. Here's the app.js, and the client side script. I'm not that great with stack formatting so forgive me.

Server Side App.Js:

var express=require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
nicknames = [];


server.listen(3000);

app.get('/', function(req, res){
res.sendFile(__dirname + '/chat.html');

});


io.sockets.on('connection', function(socket){
socket.on('new user', function(data, callback){
    if(nicknames.indexOf(data) != -1){
        callback(false);
    }
    else{
        callback(true);
        socket.nickname = data;
        nicknames.push(socket.nickname);
        io.sockets.emit('usernames', nicknames);
    }

});
socket.on('send message', function(data){
    io.sockets.emit('pushMessage', {msg: data, nick: socket.nickname});


});

socket.on('disconnect', function(data){
    if(!socket.nickname) return;
    nicknames.splice(nicknames.indexOf(socket.nickname), 1);
    io.sockets.emit('usernames',nicknames);

});
});

Client HTML/JS:

    <html>
    <head>
    <script   src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
     <title>SchoolChat</title>
     <style>
    #chat{
    height:500px;
    }
    #mainWrap{
    display:none;
    } 
    #chatWrap{
    float: left;
    border:1px #000 solid;

    }

#chat {
    list-style-type: none;
    margin:0;
    padding:0;

}
#chat li {
    padding:5px 10px;

}

#chat li: nth-child(odd) {
    background:#eee;
}
</style>
</head>
<body>
<div id="nickWrap">
<p>Enter a Username</p>
<p id="nickError"></p>
<form id="setUser">
<input size="35" id="userName"></input>
<input type="submit"></input>
</form>
</div>

    <div id="mainWrap">
    <div id="chatWrap">
        <ul id="chat"></ul>
        <form id="send-message">
        <input size="35" id="message"></input>
        <input type="submit"></input>
        </form>
    </div>
    <div id="users"></div>
    </div>



    <script src="/socket.io/socket.io.js"></script>
    <script>
    jQuery(function($){
        var socket = io.connect();
        var $messageForm = $('#send-message');
        var $usernameForm = $("#setUser");
        var $userName = $("#userName");
        var $nickError = $("nickError");
        var $nickBox= $('#nickWrap');
        var $messageBox = $('#message');
        var $chat = $('#chat');

            $usernameForm.submit(function(e){
                e.preventDefault();
                socket.emit('new user', $userName.val(), function(data){
                if (data){
                    $("#nickWrap").hide();
                    $("#mainWrap").show();
                }
                else{
                    $("#nickError").html('That username is already in use.');

                }



                });
                $nickBox.val('')
            });

            socket.on('usernames', function(data){
                var html= " ";
                for(i=0; i<data.length; i++){
                    html += data[i] + '</br>'

                }
                $('#users').html(html);

            });

        $messageForm.submit(function(e){
            e.preventDefault();
            socket.emit('send message', $messageBox.val());
            $messageBox.val("");

        });



        socket.on('pushMessage', function(data){
            $chat.append("<li><b>" + data.nick+ ":" + "</b>" + data.msg+ "</li>");


            });

    });
        </script>
</html>

Upvotes: 0

Views: 1529

Answers (2)

Bilel Boulifa
Bilel Boulifa

Reputation: 216

maybe its too late but for those will face this issue in the future, this happen to me and the problem was coming from skype, you need to go to skype settings and under connections uncheck using port 80.

Upvotes: 0

Alex Taylor
Alex Taylor

Reputation: 8813

There are a number of things that may be going wrong. Some diagnosis is required. Firstly, are there any error messages being produced?

Another service is listening on port 80

Here is a slightly modified Hello World app from the Node.js site (under the Windows command line):

> var http = require('http');
undefined
> http.createServer(function (req, res) {
...   res.writeHead(200, {'Content-Type': 'text/plain'});
...   res.end('Hello World\n');
... }).listen(80, '127.0.0.1');
{ domain: null,
  _events:
   { request: [Function],
     connection: [Function: connectionListener],
     clientError: [Function] },
  _maxListeners: 10,
  _connections: 0,
  connections: [Getter/Setter],
  _handle: null,
  _usingSlaves: false,
  _slaves: [],
  allowHalfOpen: true,
  httpAllowHalfOpen: false,
  timeout: 120000 }

But if we try the same thing on the already in-use port 135:

> http.createServer(function (req, res) {
...   res.writeHead(200, {'Content-Type': 'text/plain'});
...   res.end('Hello World\n');
... }).listen(135, '127.0.0.1');
{ domain: null,
  _events:
   { request: [Function],
     connection: [Function: connectionListener],
     clientError: [Function] },
  _maxListeners: 10,
  _connections: 0,
  connections: [Getter/Setter],
  _handle: null,
  _usingSlaves: false,
  _slaves: [],
  allowHalfOpen: true,
  httpAllowHalfOpen: false,
  timeout: 120000 }
>
events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EACCES
    at errnoException (net.js:904:11)
    at Server._listen2 (net.js:1023:19)
    at listen (net.js:1064:10)
    at net.js:1146:9
    at dns.js:72:18
    at process._tickCallback (node.js:419:13)

The command to see what ports are in use on Windows and UNIX-like operating systems is netstat -a.

Your operating system/firewall is preventing listening on port 80

UNIX-like operating systems reserve ports in the range 0 to 1023 as well-known or system ports. To work around that, look at the answer 'Linux: allowing an user to listen to a port below 1024'.

The Windows firewall will often interfere with processes opening for the first time. That will typically pop up a message explaining what it's doing unless it has been explicitly blocked. But as you can see from the first example, it will allow regular processes to open port 80.

Upvotes: 1

Related Questions