0

I'm new to stakoverflow, but I swear that I really searched a lot before ask here. In short, I've two browsers opened, and two PHP sessions, redis takes care of the session, but when I try to refresh the page the online user that refreshed the node.js server take the (maybe socket id?) of the refreshed one, and if I do a logout on the not refreshed one, the connection will close as the refreshed one.

I know, it's messy. I have to set a socket id array with also session together? Currently, I am storing each new connection in a room (to have private sockets), but I become to think that maybe I've to change something for make all work properly.

How you handle a refreshed page? Also because can happen that a user open another tab of same website, or a friend give a link and you visit that link, and you need the session id (ok) the saved user (ok) and the socket io socket (wrong in my case?). Maybe its because I don't know how to get the cookie outside this:

socket.on('connection', function(client) {
    cookieManager = new co.cookie(client.request.headers.cookie);

    clientSession.get("sessions/"+cookieManager.get("PHPSESSID"), function(error, result) {
        if (error) {
            console.log("error : "+error);
        }
        if (result != "") {
        // if (result.toString() != "") {
            console.log("PHP session id: " + cookieManager.get("PHPSESSID"));
            //console.log(result.toString());
            //console.log("print result: " + result); //warning! this will parse entire session, test only!

            userData = JSON.parse(result);
        }
    }
}
Gareth Oakley
  • 1,020
  • 1
  • 12
  • 34
James
  • 1
  • 1
  • 2
  • How to get the client (inside function) part so I can pass anytime also on disconnect event? A new cookie I mean, so I can pass that to redis clientSession.get and take finally the user cookie session that disconnect, and grab the user id to quit the socket.io room? – James Jul 03 '15 at 19:41

1 Answers1

0

By default, the nodejs server does not send back the php cookie.

When I used php + redis + socket.io, i used the configuration present in this answer: here (edit: I used memcache & a custom class, now I use redis + publish/subscribe to broadcast events!)

to connect, i read the cookie with javascript from the page and sent it in the beginning of the connection.

 /** C "cookie" function, gets the cookie string.
  * @param {string} [k] - key, the cookie to get
  */
function C(k){return(document.cookie.match('(^|; )'+k+'=([^;]*)')||0)[2]}

s = io.connect("//io."+ window.location.host +"/", {query: "id=" + C('PHPSESSID')});

from the nodejs side:

io.sockets.on("connection", function (socket) {
var cookie = socket.handshake.query.id;
...
});

I stored the data formatted in json in the redis database, with phpsessid as key.

Hope it helps!

Community
  • 1
  • 1
ruifn
  • 140
  • 8
  • Thank you for reply me, the main problem is that when I press F5 on one of two session (eg: website Chrome, website Firefox), for example the refreshed one on Chrome, takes control on node.js server, and closing the Firefox one, is supposed that close the Firefox one, instead close the Chrome one (it's room), mainly because I declare the redis and cookie connection on client connection (F5 refresh is a connection, unfortunately, I was thinking that this was going to be a little easy) – James Jul 03 '15 at 19:18