Socket killed when using onbeforeunload?

918 views
Skip to first unread message

Jeff

unread,
Feb 9, 2012, 2:08:19 PM2/9/12
to Socket.IO
Hey everyone,
I'm having a problem where if I use the onbeforeunload event and the
user opts to "stay on this page", it appears the connection to my
Node.js/Socket.IO server gets killed.

I'm using something similar to the code below so that when the client
receives the event "updateScript", it logs a message to the console.

Socket.on("updateScript", function(){
console.log('updateScript event received');
});

It works until you try to close the window, then choose "stay on this
page." After that, the event doesn't fire.

I'm having the problem in Chrome, Safari, and Firefox. I couldn't
replicate it in Opera, but I think that might be because it doesn't
support onbeforeunload. I didn't test Internet Explorer.

Any ideas?

Brian Gruber

unread,
Feb 9, 2012, 2:33:05 PM2/9/12
to sock...@googlegroups.com
Maybe set this option on the CLIENT side

sync disconnect on unload 

something like this

var opts  = {};
opts['sync disconnect on unload'] = false;

io.connect("server here", opts);

--
Regards,

Brian Gruber

Real time visitor analytics, chat, heatmaps & polls.

Yanik Lupien

unread,
Feb 9, 2012, 7:11:32 PM2/9/12
to sock...@googlegroups.com
REMOVE ME

Any ideas?
-----
No virus found in this message.
Checked by AVG - www.avg.com
Version: 2012.0.1913 / Virus Database: 2112/4799 - Release Date: 02/09/12

Jeff

unread,
Feb 10, 2012, 6:41:43 PM2/10/12
to Socket.IO
Wow. I just tested it and that fixed it. Thank you for your help!
> *Real time visitor analytics, chat, heatmaps & polls.*
Reply all
Reply to author
Forward
0 new messages