How keep the socket connection when you refresh the page.?

16,525 views
Skip to first unread message

GUAN XI

unread,
Feb 9, 2011, 6:03:55 AM2/9/11
to Socket.IO
Follow the instruction on the homepage, I could build the socket
connection . But I found a serious problem: when I refresh the page,
on the console( terminal) , it will show things like below

9 Feb 11:58:00 - Initializing client with transport "websocket"
9 Feb 11:58:00 - Client 8583426235709339 connected
9 Feb 11:58:09 - Client 8583426235709339 disconnected
9 Feb 11:58:09 - Initializing client with transport "websocket"

That means, every time I refresh the web page, the socket client will
close the current one and build a new one.
Is there anyway to keep the socket connection when you refresh the
page?

Alexandros Tzortzidis

unread,
Feb 9, 2011, 6:59:34 AM2/9/11
to sock...@googlegroups.com
It would require to cache (both in the server and client) a second unique session id generated by the server part. Then, every time a client re-connects it should identify itself with the session id or, in the case of a new session, request one. You can also garbage-collect or implement a timeout for the outdated sessions in the server.

For the client-side caching I suggest you use cookies for easy consistency. (you can alternatively try jStorage if you use jQuery, mootools or prototype)

Speaking technically,

On the server:
The 'connect' event should do nothing related
The 'message' event should recognize { 'session': null } and { 'session': id } (id can be a hash for basic security)
    In the first case you generate a new session object in your session store (use a persistent one or use a global/scope map) and fall-through to:
    In the second case you assign the passed client object's .session = message's .session and update the session object's .connected attribute to true.
    client.session = message.session;
    sessions[ client.session ].active = true;
    sessions[ client.session ].client = client;
The 'disconnect' event should update 
    sessions[ client.session ].active = false;
    sessions[ client.session ].lastactive = new Date();
    sessions[ client.session ].client = null;

Set up an interval to check for outdated sessions and remove them from the session store

Don't forget to check for client.session before you send a message, and implement your own broadcast that iterates through the active sessions instead of clients

On the client:
Set socket.session = null
In the 'connect' event you should check for a previous session id and use this, or request a new one (using the protocol in the server's 'message' event) and update socket.session

Check for socket.session before you send a message.

Being simple as it is, multiple tabs would break the sever->client transmission for the older tabs. Make a client store in each of the sessions object instead of a setting a single one.

Alex

Kevin

unread,
Feb 9, 2011, 9:11:21 PM2/9/11
to Socket.IO
Seems like the root cause here is that the socket.io client or
websocket client is a javascript object that gets destroyed and
recreated when the page reloads, thus causing the disconnection and
the reconnection. Common browser caching techniques like cookies or
windows.name won't work because they only cache string data, not
object instances. I am not aware of any way to cache a javascript
object instance that survives a page refresh, therefore the disconnect/
connect is unavoidable. If someone knows a way, please leave me a
breadcrumb ;) You would need to write some kind of client (and/or
server) state that matches your app's need to bridge the disconnect/
connect. It's like you need session state of a series of connections
from a single client. Hope that helps.
Reply all
Reply to author
Forward
0 new messages