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 ?