I am running a LAMP server on a VPS, and I have just installed socket.IO on it (with node 0.6.6 as npm refused to install on 0.7.0-pre). Now, I would like to test my installation with a simple example, such as a simple chat application, and also learn from it how to use it in my own apps.
There is however something strange with this. There seems to be a well-known 10-line chat example, but it has vanished from the developer's website and despite hundreds of references to it and hours or searching, I fail to find the actual example.
Hidden in the page source on http://socket.io I find a screenshot of an improved version with nicknames, still in 10 lines, but alas still no code example and it's also not public. The last example on http://howtonode.org/websockets-socketio looks like it already, but alas without a matching server-side script or html-page to embed it in.
Does anyone know what's going on with this? Could anyone provide a working small example (chat or multiplayer game) to demonstrate the basics of socket.IO, that works with the current version?
Edit: from the examples at the socket.io website, I assume that I would need "broadcasting", but it's not clear to me at all how to build a working chat app from this (even though they claim it's only 10 lines). :/
Firstly you would want to install express along side for this, makes life easier :)
npm install express or npm -g install express if your installing globally, once installed create an app by going to your work directory and typing express sample to create a project called sample. go into the sample directory and open the app.js up with your favourite editor.
replace the contents with the following:
/**
* Module dependencies.
*/
var express = require('express')
, io = require('socket.io')
, routes = require('./routes')
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', routes.index);
//IO Bindings
io.sockets.on('connection',function (client){
client.on('hello', function(data){
//Client sent hello
});
//Add the rest of your event bindings here for client scopes
});
app.listen(8080);
io.listen(app);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
And then you can modify your jade to t spit out the following html to the front page:
<!DOCTYPE html>
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect(/*defaults to so ip:port*/);
socket.on('connect', function (){
console.log("Connected");
socket.emit('hello', { my: 'world' });
});
</script>
</head>
<body>
Body Here
</body>
</html>
You just need to turn that into your templates and you should be good to go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With