Search code examples
node.jssessionredisbotsfacebook-messenger

How do I check if a session id already exists in redis


I have the following code and I am not sure how to check if the session key exists already, because I do not want to create another redis session if it already exists.

The request object is new with every call but I know that the event.sender.id is the same on each request.

     // If not set then create the session object
     if (!req.session.key) {
       console.log('Set session variable');
       req.session.key = event.sender.id;
       console.log('*** SESSION CREATED WITH ' + event.sender.id);
     }

Solution

  • Server.js

    const redis = require("redis");
    const client = redis.createClient(); 
    const hostname = '127.0.0.1'; 
    const port = 6379;
    
    
    client.on("error", function (err) {
        console.log("Error " + err); });
    
    var session_id_key = event.sender.id ; //your id
    
    client.exists("key",session_id_key, function (err, replies) {
    
        if(!err && replies===1){
    
            console.log("THE SESSION ID ALREADY PRESENT ");
        }else{
            console.log("THE SESSION ID DOES NOT PRESENT");
        } });
    

    The Answer is : THE SESSION ID ALREADY PRESENT