0

What is the most efficient way to count online users in a ROR application which uses devise?

I was considering adding a field called status to users table and update it according to several situations. What is the best practice about this subject?

  • I think this question will help to solve your problem: http://stackoverflow.com/questions/5504130/whos-online-using-devise-in-rails – zolter Aug 23 '13 at 04:07
  • @zolter hmm, in the answers there is a 10 mins buffer in last seen part. I am looking for something close to realtime status changes. –  Aug 23 '13 at 04:18
  • You have to keep updating the status every time users login or logout but how would you track left sessions? – techvineet Aug 23 '13 at 04:42

2 Answers2

1

In theory you can't do that plainly in server side.

The server is stateless. A visitor request something, server gives it, then bye bye, nothing more.

Then how does those "online" solutions like Google Analytice work?

The basic is, the visitor get a block of JS code to run on their browser, downloaded from either your server or Google's server. Then the JS code send requests to server at certain time interval to claim this visitor is still "online".

If after some time there is no such request from this visitor, he is "offline".

The JS code may also send request to server to ask for updating the view of visitors status, so a visitors can see some other visitors are "online".

If fully implemented on your own server, the solution may be a bit complex and require more efficient storage such as Redis or even some realtime solutions. I have not researched such gems before, you can check if some exists in analytic category.

If you want simple solution, the last visitors one mentioned by zolter, or Public Activities gem may fit you.

Billy Chan
  • 24,625
  • 4
  • 52
  • 68
0

The basic idea is to open a persistent connection to your server and increment a counter on the server any time a connection is established and decrement it when a connection is lost. You could use something like socket.io or Pusher to make this easy.

ErJab
  • 6,056
  • 10
  • 42
  • 54