Skip to content

Instantly share code, notes, and snippets.

@mbrevoort
Created December 3, 2011 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbrevoort/1426410 to your computer and use it in GitHub Desktop.
Save mbrevoort/1426410 to your computer and use it in GitHub Desktop.
Dojo Stateful Replication over Socket.io
<!DOCTYPE html>
<html>
<head>
<title>Dojo Stateful Replication</title>
<script>
var dojoConfig = {
baseUrl: '/js/dojo/1.7.0/dojo',
};
</script>
<script src="/js/dojo/1.7.0/dojo/dojo.js"></script>
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<script>
dojo.require('dojo.Stateful');
dojo.addOnLoad(function(){
var state = new dojo.Stateful(),
socket = io.connect('http://localhost'),
watcher = null;
socket.on('connect', function() {
watcher = state.watch( watch );
});
socket.on('state', function (data) {
state.set(data);
});
socket.on('update', function (data) {
state[data.name] = data.value;
});
socket.on('delete', function (data) {
delete state[data.name];
});
function watch(name, previous, updated){
if( (updated === undefined) ) {
emit('delete', name);
} else {
emit('update', name, updated);
}
}
function emit(action, name, value) {
socket.emit(action, {
action: action,
name: name,
value: value
});
}
});
</script>
</body>
</html>
var util = require('util')
, express = require('express')
, io = require('socket.io')
, argv = require('optimist').argv
, requirejs = require('requirejs')
, port = argv.port || 8080;
// ============================================================================
// RequireJs and Dojo Stateful wireup
// ============================================================================
requirejs.config({
nodeRequire: require,
baseUrl: './public/js/dojo/1.7.0/dojo'
});
requirejs(['stateful'], function(stateful) {
Stateful = stateful;
});
var state = new Stateful();
var watcher = state.watch(watch);
// ============================================================================
// Express app configuration
// ============================================================================
var app = module.exports = express.createServer();
app.configure(function(){
app.use(express.static(__dirname + "/public"));
});
var io = io.listen(app);
app.listen(port) && util.log("Listening on " + port);
io.sockets.on('connection', function(client) {
client.emit('state', state);
client.on('update', function(data) {
client.broadcast.emit('update', data);
state[data.name] = data.value;
console.log(state);
});
client.on('delete', function(data) {
client.broadcast.emit('delete', data);
delete state[data.name]
console.log(state);
});
client.on('disconnect', function() {
// placeholder in case we need to do anything on disconnect
});
});
function watch(name, previous, updated){
if( (updated === undefined) ) {
emit('delete', name);
} else {
emit('update', name, updated);
}
}
function emit(action, name, value) {
io.sockets.emit(action, {
action: action,
name: name,
value: value
});
}
// increment a count variable in the state for testing server state propagation
var i=0;
setInterval(function() { state.set('count', ++i)}, 5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment