Over the weekend, I was trying to figure out websockets (since I think it would probably be a really fun thing to know).
I did a search around for socket.io tutorials and found this Good beginners tutorial to socket.io? which suggested I start on http://socket.io
On a fresh ubuntu I built node.js 4.1.13-pre
(many packages won't work with the current 0.5.8)
I added, NPM and the express, jade & socket.io packages.
I set up and ran a server:
var io = require('socket.io').listen(8000); // I moved the port
var express = require('express'); // I had to add this
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
I cloned https://github.com/LearnBoost/socket.io.git
and made an index.html
in the directory above the place I cloned socket IO into
<script src="socket.io/lib/socket.io.js"></script> <!-- changed path from example -->
<script>
var socket = io.connect('http://localhost:8000');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
When I load up the index page locally, I get the error: require not defined
I'm assuming that I've missed something here, is the client-side JS not the same one from the lib folder? DO I need to add something to allow for the existence of 'require'?
What am I missing? How do I serve up the client side JS correctly?