"onunload" vs "onbeforeunload" are the correct answers, but you must also send an AJAX call to the PHP file.
window.onbeforeunload = function () {
jQuery.ajax({url:"http://localhost:8888/do_somethink_in_db.php?", async:false});
}
OR
$( window ).unload(function() {
jQuery.ajax({url:"http://localhost:8888/do_somethink_in_db.php?", async:false})
});
: , . nodejs, , . socket.io(library nodejs)