That's awesome.
A cluster of servers.
Lightweight key/value persistence in Redis.
io.sockets.on('connection', function(socket) {
var nick = 'dshaw'
socket.set('nickname', nick, function() {
socket.emit('nickname', nick)
})
})
Inter-process dispatch.
io.sockets.emit('newuser', { nick: nick });
Cool. But how?
Up until today, YOYO!
Applies to the old school:
Express + Socket.io
As it does to the newest:
Tako
Just drop in Announce and broadcast data
directly with your Socket.io connected users!
var announce = require('socket.io-announce').createClient()
, symbols = 'THOO GOOF EXIT BOP SDD ALPP RIGM OPPL HPBG'.split(' ')
function dataStream () {
var n = Math.round(Math.random()*8)
, data = {
id: (Math.abs(Math.random() * Math.random() * Date.now() | 0))
, symbol: symbols[n]
, price: (Math.random()*1000).toFixed(2)
, n: n
}
announce.emit('quote', data)
}
setInterval(dataStream, 800)
var sio = require('socket.io')
, RedisStore = sio.RedisStore
, app = connect.createServer(connect.static(path.join(__dirname, './')))
, settings = { store: new RedisStore({
nodeId: function () { return 'socketio-1' }
}) }
, io = sio.listen(app, settings);
app.listen(port);
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect()
socket.on('quote', function (data) {
$('<span/>', {
id: data.id,
html: data.symbol + ' : ' + data.price }).appendTo('#ticker')
// if the last digit of the prices matches the id, buy.
var m = ((data.price).match(/[-+]?([0-9]*([0-9]))?\.[0-9]+/))
, watch = m && m[2];
if (nodeId && watch == nodeId) {
$('#'+data.id).addClass('reserved')
data.buyer = nodeId
data.quantity = Math.round(Math.random()*5)*5
socket.emit('purchase', data)
}
});
</script>
Insanely simple.
You know that.
Monitoring with MONITOR is bad.
The MONITOR command is good and useful for debugging, but not monitoring.
Monitor a single Redis or a cluster of Redis servers.
Now visualize what's going on on those servers.
More at: github.com/dshaw