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.