0

I am using that code on my chat application using node.js and redis but without hiredis as I cannot install it on windows machine

var app = require('express')()
  , redis = require('redis')
  , client = redis.createClient()
  , server = require('http').createServer(app)
  , io = require('socket.io').listen(server)
  ;

server.listen(80);

io.sockets.on('connection', function (socket) {
    console.log("client connected")

    socket.on('join', function(userid) {
        socket.set('userid', userid);

        var userco = userid+":connects";

        client.incr(userco);
    });

    socket.on('disconnect', function(userid) {

        socket.get('userid', function(err, userid) {

            var userco = userid+":connects";
            client.decr(userco);
            var usercoco = client.get(userco);
            console.log(usercoco);
            if (parseInt(usercoco)<1) {
               io.sockets.emit("remove-user",{id:userid});
            }
        });
    });
});

but parseInt method not working also the remove-user event not working too .. do you have any explanations and solution for that ?

theWanderer4865
  • 861
  • 13
  • 20
Remon Amin
  • 1,506
  • 2
  • 19
  • 44
  • Can you further define 'not working'? Are you getting errors? If so, you should include them. – Nick Mitchinson Mar 11 '13 at 23:44
  • on console.log(usercoco) it gives me true value and when I am using redis client and I use the command get 2:connects it gives me the number of user connections for example "1" or "2" – Remon Amin Mar 11 '13 at 23:47

1 Answers1

0
var usercoco = client.get(userco);
console.log(usercoco);

.get is an ASYNC function, it does not return a variable.

client.get(userco, function(err, res) { console.log(usercoco); }

It will log to the console whenever it completes.