xShirase
xShirase

Reputation: 12389

Using Socket.io inside an Apache served page

I know this has been a recurrent question lately, but despite reading all I could, I couldn't get past this simple problem :

My oversimplified socket server :

var socket = require('socket.io').listen(7000);

socket.on('connection', function(client){
  client.on('message', function(msg){
      socket.broadcast(msg);
  })
});

node socket.js output : socket.io started

http://localhost:7000 output : 'Welcome to socket.io'

Index.html (simplified too):

<html>
   <head>
      <script src="http://localhost:7000/socket.io/socket.io.js"></script>
      <script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
      <script>
         var name = '';
         var socket = io.connect('http://localhost:7000');

         $(document).ready(function(){
            $("button").click(function(){
               $("p#log").html('sent message: ' + $("input#msg").val());
               socket.emit('message', $("input#msg").val() );
               $("p#data_received").append("<br />\r\n"+ $("input#msg").val());
               $("input#msg").val('');
            });
         });

        socket.on('chat', function (data) {
            console.log(data);
         });
      </script>
   </head>
   <body>
      <input type="text" id="msg"></input><button>send</button>
      <p id="log"></p>
      <p id="data_received"></p>
   </body>
</html>

Page displays properly on the apache server. However I get the following, no matter what I tried :

GET http://localhost:7000/socket.io/socket.io.js net::ERR_CONNECTION_REFUSED  

I have tried using the full url (node_modules/socket.io/.../dist/socket.io.js), not working. Any thoughts? Am I not thinking of something like apache configuration? I'm thinking of moving my entire page to a node server, but the problem still bugs me...

Upvotes: 2

Views: 1285

Answers (1)

Paul Sweatte
Paul Sweatte

Reputation: 24617

Solved the problem by simply changing localhost to my domain name. It's not the first time I have troubles with 'localhost' on EC2.

Upvotes: 1

Related Questions