I'm a beginner at node.js and socket.io trying to get a server running so I can push updates to clients. I'm attempting to follow this tutorial: http://danielnill.com/nodejs-tutorial-with-socketio/
I have node.js installed and confirmed with the command "node -v" on my VPS running Centos 6.4. I've ran the command "npm install socket.io" in the directory I'm running my server.js file. However, using "io.sockets.on" results in a "TypeError: undefined is not a function" error in the console.
server.js file:
var http = require('http');
var url = require('url');
var fs = require('fs');
var io = require('socket.io');
var server = http.createServer(function(request, response){
console.log('Connection started...');
switch(path){
case '/':
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('Hello World');
break;
case '/socket.html':
var data = fs.readFileSync(__dirname + path);
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(data);
break;
default:
response.writeHead(404);
response.write('ERROR 404');
break;
}
response.end();
});
server.listen(8000);
io.listen(server);
io.sockets.on('connection', function (socket) {
console.log('hello world');
});
client html:
<html>
<head>
<script src='/socket.io/socket.io.js'></script>
</head>
<body>
<script>
var socket = io.connect();
io.sockets.on('connection', function(socket){
socket.emit('message', {'message': 'hello world'});
});
</script>
This is our socket.html file
</body>
All code works fine without the socket.io components, so I know node.js is working properly. Is something wrong with my socket.io installation or am I missing something very obvious?
Thanks