0

I must be doing something incredibly stupid, as the tutorial (!) chat example at http://socket.io/get-started/chat/ for Node.JS and Socket.IO is not working for me. More specifically, the 'connection' event is not firing when I load the webpage.

This is the full index.js:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

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

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

http.listen(3000, function(){
  console.log('1e listening on *:3000');
});

This is the full index.html:

<!doctype html>
<html>
  <head>
    <title>1e Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
  </head>
  <body>
    <script src="/socket.io/socket.io.js"></script>
    <script>
      var socket = io.connect('http://X.YY.ZZZ.QQ:3000');
      socket.on('message', function(msg){
        console.log(msg);
        document.getElementById("message").innerHTML = msg;
      });
    </script>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
  </body>
</html>

Firewall info:

  • On X.YY.ZZZ.QQ:3000, I have disabled the firewall (yes, quick and ugly temporary hack to avoid timeout and get the page to load, till I figure out how to properly set up the iptables rules) via service iptables stop
  • On local Windows machine, I can easily access X.YY.ZZZ.QQ via ssh, and firewall is also disabled in Windows, so I presume the port should not be an issue here

What works:

  • Client : I can access X.YY.ZZZ.QQ:3000 via InternetExplorer and it displays the Chat bar with Send button.
  • I know for sure that the index.html is the file being loaded, as I keep incrementally changing the title tag ("1e") upon each change in the html file
  • Server: the console output displays *"1e listening on .3000" upon starting it via command ./node index.js
  • I also keep incrementally changing the version ("1e") to make sure the node.js file is the correct one

What does not work:

  • I should get the console message 'a user connected' upon (re)loading the webpage tab, but it's not firing. I put a breakpoint into the HTML code at the var socket = io.connect () line, and it did indeed execute the line, so it looks like socket.io is being loaded

Are there any error codes ? How do you troubleshoot the Node.JS/Socket.IO ? Any internal logs ? I looked, but haven't see any.

I'm pretty sure I've neglected something incredibly primitive, but since this web technology is new to me (I'm mostly C/C++ guy), I'm lost. Any ideas ?

UglyMF
  • 9
  • 1
  • 2
    Are you seeing any errors in the browser console on the client side? FYI, since it appears you're just trying to connect back to the same server that the page was loaded from, you can just use `var socket = io()` and it will automatically connect back to the host/port that the page was loaded from. – jfriend00 Sep 09 '16 at 15:45
  • is it loading the javascript file for socket.io? – sova Sep 09 '16 at 17:57
  • @jfriend00: I am not seeing any errors there. Also, using socket = io() did not work - that's what is in example by default, but by searching similar threads I noticed that was the syntax only for localhost and one must use io.connect (IP) – UglyMF Sep 09 '16 at 18:20
  • I'd suggest looking at the network tab in the browser debugger as the page loads and see what is happening with the socket.io connection. – jfriend00 Sep 09 '16 at 18:56
  • `var socket = io()` works for ANY connection back to the same host/port that the page was loaded from. I use it in production sites, not just for localhost. It prevents any accidental cross-origin mistakes in your typing and is, obviously, simpler. – jfriend00 Sep 09 '16 at 19:16
  • @jfriend00 : It most certainly did not work for me, as until I changed the var socket=io() into the current syntax with an actual IP address, I could not access/open the webpage. – UglyMF Sep 22 '16 at 17:45

0 Answers0