I need help.
I am trying to get around asynchronous programming of node.js and socket.io during the day. I understand that I need flow control, but I don't seem to understand how to implement it correctly.
I have a redis repository that has the modules stored in the set, let them say 'moda', 'modb' the instances of these modules are "moda: instances" and in "modb: instance" the properties of these instances are stored in "moda: instancea" and "modb: instanceb" as a hash.
I am trying to get the following json:
"moda": {"instancea": {"property1": "value1", "property2", "value2"}}, "modb": {"instanceb": {"property1": "value1"}}
Can someone push me a little in the right direction?
Here is my current code:
var io = require('socket.io').listen(2000); var redis = require('redis').createClient(); var http = require('http'); var async = require('async'); var step = require('step'); io.sockets.on('connection', function (socket) { var notifications = require('redis').createClient(); notifications.subscribe("notification"); notifications.on("message", function (channel, message) { socket.send(message); console.log(channel + ':' + message); }); socket.on('modules', function(params, callback) { var response = {}; async.series([ function (callback) { console.log('1>'); redis.smembers('modules', function (err, modules) { async.forEachSeries(modules, function(module, moduleCallback) { response[module] = {} redis.smembers(module + ':instances', function(err, instances) { async.forEachSeries(instances, function(instance, instanceCallback) { response[module][instance] = {} console.log('2>' + module + ':' +instance); instanceCallback(); }); moduleCallback(); }); }); callback(); }); }, function (callback) { console.log('3'); callback(); } ], function() { console.log(JSON.stringify(response)); }); }); });
Exit from this code:
info - socket.io started debug - client authorized info - handshake authorized JMMn1I8aiOMGCMPOhC11 debug - setting request GET /socket.io/1/websocket/JMMn1I8aiOMGCMPOhC11 debug - set heartbeat interval for client JMMn1I8aiOMGCMPOhC11 debug - client authorized for debug - websocket writing 1:: 1> 3 {"moda":{}} 2>moda:instancea 2>moda:instanceb 2>modb:instancea