Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Removing someone from a user hash on navigating away from a page / page close

Removing someone from a user hash on navigating away from a page / page close

Problem

I'm building a Node.js / Socket.io project.

I have a Hash of Users based on their websocket id. When a user makes the following request i'd like to add them to a group of users viewing that page.

app.get('/board/:id', function(req, res){}

I'd like to keep something like

Browsing = { Username : id, username : id, ... }

However i'm unsure how to remove a user lets say if they navigate off the page. Is there some kind of function that gets called upon page leave?

Partial Solution:

The following seems to do the trick on Chrome:
   $(window).unload(function(){
      var username = $('#username').text();
      var pid = currentProject;

      var data = {
        username: username,
        id : pid
      }

      socket.emit('leaving-page', data);
    })
Problem courtesy of: Jack

Solution

... Is there some kind of function that gets called upon page leave? ...

Yes, but it is not reliable.

The way the people keep track of who is online and who isn't, is usually like this:

  • Add the time when the user last refreshed/visited a page
  • set a limit to you consider them offline
Solution courtesy of: ajax333221

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Removing someone from a user hash on navigating away from a page / page close

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×