ajax - Redis smembers with node -
i have node, express, , websockets
chat app. usernames of users in chatroom stored redis
. redis-cli
when type in smembers 'onlineusers'
, list of users in room:
127.0.0.1:6379> smembers 'onlineusers' 1) "jackson" 2) "bubba yump" 3) "dog" 4) "rumba" 5) "buba"
in main application javascript
file have method current users can display them on page:
var showcurrentusers = function(){ $('#list_of_users').empty(); $.getjson('getusers', function(data){ console.log(data) (var in data){ $('#list_of_users').append("<li> user: "+data[i]+"</li>") } }); }
in node
server file have matching route db.smembers
keeps returning true
instead of list of users.
app.get('/getusers', function(req,res){ res.send(getonlineusers()); }); // redis data var getonlineusers = function(){ return db.smembers("onlineusers") }
and here username posted database:
client.on("setusername", function(user){ pub.publish("chatting","a new user in connected:" + user); db.sadd("onlineusers",user); } );
i know usernames correctly posting database, , can access them redis-cli
, i'm not sure why json call returning true
instead of list.
(i'm assuming you're using https://github.com/mranney/node_redis)
the node redis client async. can treat sync writes , deletes, read db need use callback.
app.get('/getusers', function(req, res, next){ getonlineusers(function (err, onlineusers) { if (err) { return next(err); } res.send(onlineusers); }); }); // redis data var getonlineusers = function(cb){ return db.smembers("onlineusers", cb); }
Comments
Post a Comment