Sunday 11 June 2017

Socket with session handling. Creating session on Post method. server side values are set. but emitting into client takes refresh

I have created a session variable on post method. value is set on server side.

var sess;
app.post('/loginn', function(req,res){
    sess=req.session;
   sess.email=req.body.email;
console.log("sess.email"); // value is set here
io.emit("socket email",{email: sess.email}); // To access the session in client i tried emit on post but its not accessible 
   res.json({id : req.body.socket, user : req.body.email, message : "done" });
 });

i used the session variable at the time we access the url http://localhost:3001/notification

by the following code.

app.get('/notification', function(req,res){
  sess=req.session;
  console.log(sess);
  console.log("sess id "+sess.email);
  var some_date="2313232432232";
  if(sess.email){
      io.emit("socket email",{email: sess.email});
      connection.query('select * from mdl_mobile_user where mob_email="'+sess.email+'"', function(error,rows){
        if(error){
          console.log("Error"+error);
        }
        else{
          if(rows.length==0){
            connection.query('insert into mdl_mobile_user (mob_email,timejoin) values ("'+sess.email+'","'+some_date+'")',function(error,results){
              if(error){
                console.log("Error"+error);
              }
              else{
                console.log("User saved in db");
              }
            });
          }
          else{
            console.log("This user already enrolled in mobile");
          }
        }
      })
   res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(notification);
  }
  else{
    res.writeHead(200, {'Content-Type': 'text/html'});
     res.end(index);
  }


});

By Doing it the values are set fine in the server side but on post the values are not set in client side. On post i made redirect to the notification page.

$("#submit").click(function(){
            email=$("#email").val();
            pass=$("#password").val();
            /*
            * Perform some validation here.
            */
            $.post("http://10.21.1.50:3001/loginn",{email:email,pass:pass,socket:socket.id},function(data){
              console.log(data.message);
              if(data.message==='done')
              {
                window.location.href="/notification";
              }
            });
          });

on first instance i am not getting the values from server. but when i refresh the page. i get the value. The same is happening for me in other instance. where on this notification page i want to show all the messages he/she is pending, its working for me. but its happening only when i refresh the page for several times .

This i performed through the server code

socket.on("message check",function(req,res){
      console.log("Email from user"+req.res_email);
      connection.query("select mdl_mobile_user.mob_email,mdl_mobile_notification.mob_message,mdl_mobile_notification.notification,mdl_mobile_notification.id from mdl_mobile_user join mdl_mobile_notification on mdl_mobile_user.id=mdl_mobile_notification.mob_user_id where mdl_mobile_user.mob_email='"+req.res_email+"' and mdl_mobile_notification.notification=0", function(error,rows){
        if(error){
          console.log("Error"+error);
        }else {
          if(rows.length!=0){
            console.log(rows);
            var message_json=JSON.stringify(rows);
            var message_parse=JSON.parse(message_json);
            for(var i=0;i<message_parse.length;i++){
              console.log(message_parse[i].id+"says"+message_parse[i].mob_message);
              io.emit('post notification message', {message_id: message_parse[i].id, message_content: message_parse[i].mob_message});
            }
            //socket.emit('post notifiation message' , {id : user_id, message : server_msg});
          }else{
            console.log("No messages found");
          }
        }
      });

Client:

socket.on('post notification message', function(res){
                                console.log(res);
                                addMessage(res.message_id,res.message_content);
                                socket.emit("change notification status", {id : res.message_id});
                            }); 



via Faizal Shaikh

No comments:

Post a Comment