You could very well create a home grown analytics solution. However, a more cost efficient and practical option is to use an analytics service. One of the most popular ones around is Google Analytics.
Oops, I think you’ve a misunderstand. I mean I want to know how to create a module that display how many users are currently online in the website. Not a tracking analytics…
Do you mean you want to create a script for yourself, or do you simply want a script that performs your requirements? If so, there’s a free script here