Make Node Redis get() Synchronous
Asked Answered
C

2

11

i just started implementing redis with node. during an implementation of authentication method i need to check whether the token exist in redis, if not update the new token in redis and in my mongo db for that i need to write a big callback block and not getting result properly. how can we make the redis get red of callbacks. how can we make it synchronous. sample code is below.

module.exports.authenticate = function(request, response)   {
    var reply = {};

    if(UserSchema)  {
        var UserModel, attributes;

        /** Registering User Model; **/
        mongoose.model('user', UserSchema);
        UserModel = mongoose.model('user');

        attributes = request.params;

        UserModel.findOne(attributes, "_id name email token", function(error, user) {

            if(!error && user)  {
                var token;

                //delete user.password;
                token = user.token;

                /** Checking token exists in redis; **/
                redisClient.get(token, function(error, value)   {
                    if(value === null && error === null)    {

                        /** Creating new token; **/
                        token = require('crypto').createHash('md5').update("" + (new Date()).getTime()).digest("hex");
                        user.token = token;

                        /** Storing new token on redis; **/
                        setTokenOnRedis(token);

                        /** Updating token in the user model; **/
                        UserModel.update({ _id : user._id}, { token : token }, function(error, user)    {
                            if(error !== null && user === null) {
                                deleteTokenOnRedis(token);

                                /** Error message; **/
                                reply = {
                                    error : true,
                                    code : "AUTH#001",
                                    msg : "User authentication failed, Please check user credentials."
                                }
                                response.send(reply);

                            }else if(error === null && user !== null)   {
                                reply = user;
                                response.send(reply);
                            }
                        });
                    }else if(value !== null)    {
                        reply = user;
                        response.send(reply);
                    }else   {
                        /** Error message; **/
                        reply = {
                            error : true,
                            code : "AUTH#001",
                            msg : "User authentication failed, Please check user credentials."
                        };
                        response.send(reply);
                    }
                });
            }else   {
                /** Error message; **/
                reply = {
                    error : true,
                    code : "AUTH#001",
                    msg : "User authentication failed, Please check user credentials."
                }
            }       
        });
    }else   {

        /** Error message; **/
        reply = {
            error : true,
            code : "AUTH#001",
            msg : "User authentication failed, Please check user credentials."
        }

        response.send(reply);
    }
};
Ceil answered 11/12, 2012 at 9:27 Comment(0)
I
15

No you won't be able to make any io calls synchronous including the redis ones. The only synchronous io calls available that I'm aware of are filesystem and console ones.

However, there are some coding techniques you can use to make async coding a bit more manageable.

  • return early by checking for err first.
  • move repetitive code into a separate function, e.g, creation of error structures.
  • use this async library: https://github.com/caolan/async. In particular, the waterfall function might be handy here.

I also think that you will need to pass in a callback method these functions as they are async.

  • setTokenOnRedis(token);
  • deleteTokenOnRedis(token);

I've refactored your sample code which hopefully should be less indented and more readable/maintainable. I haven't used async, I'll leave that to you.

Personally, I found the whole node async coding model very frustrating initially but you get used to it. After a while you learn to use various async coding patterns and then it becomes just about tolerable :)

Some links that you might find helpful:

refactored code:

module.exports.authenticate = function(request, response){
  authenticate(request, response, function(err, reply){
    if(err){
       reply = authenticationError(err);
    }
    response.send(reply);
  });
};

var authenticationError = function(internalmsg){
  return {
    internalmsg : internalmsg,
    error : true,
    code : "AUTH#001",
    msg : "User authentication failed, Please check user credentials."
  };
};

var authenticate = function(request, response, callback)   {
  if(UserSchema)  {
    var UserModel, attributes;

    /** Registering User Model; **/
    mongoose.model('user', UserSchema);
    UserModel = mongoose.model('user');

    attributes = request.params;

    UserModel.findOne(attributes, "_id name email token", function(err, user) {
      if(err || !user){
        return callback(err || "UserModel.findOne, no user");
      }

      var token;

      //delete user.password;
      token = user.token;

      /** Checking token exists in redis; **/
      redisClient.get(token, function(err, value){
        if(err){
          return callback(err);
        }
        if(value){
          return callback(null, value);
        }

        /** Creating new token; **/
        token = require('crypto').createHash('md5').update("" + (new Date()).getTime()).digest("hex");
        user.token = token;

        /** Storing new token on redis; **/
        setTokenOnRedis(token);

        /** Updating token in the user model; **/
        UserModel.update({ _id : user._id}, { token : token }, function(err, user) {
          if(err || !user) {
            deleteTokenOnRedis(token);
            return callback(err || "UserModel.update, no user found");
          }
          callback(null, user);
        });
      });
    });
  }
};
Integrity answered 12/12, 2012 at 13:22 Comment(1)
thank you for the refracted code. the points you plotted are exactly what i am looking for.Ceil
M
0

Many years after the original question, the Redis client for Node.js remains asynchronous in nature - and it's not likely to change.

But in order to handle that way of working more conveniently, the package supports promises, as described at http://redis.js.org/#redis-a-nodejs-redis-client-usage-example-promises.

It doesn't make Node.js more synchronous, nor the Redis client, but it's an (small) improvement over registering callback functions, making your code a bit more readable.

Moorer answered 30/4, 2018 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.