简体   繁体   中英

Online users and server metrics

I wanted to have an online users counter in my website and at first I thought about including a websocket snippet in every page but this seemed a little overkill to me since I don't really need a constant ping-pong every half a second but rather a ping once every five seconds (for example). For performance reasons, I guess the best approach is to have the clients send the ping to the server, while the last one only receives it without sending anything back. I would like to take metrics too as in bandwidth consumption or how long a user is online.

How would you do it? Websocket or by posting a JSON somewhere every X seconds? Any out of the box solution you can think of? Thanks

I think the most accurate of results would be achieved using a web socket. Here is a very high level implementation, you can fill the meat in.

  1. Sockets have events for connection, disconnection, and errors so you only need to register a user statistic on socket connection event.

  2. Deregister the user statistic on socket disconnection event.

  3. Socket connection event can send information such as page accessed by user and any other information you may wish to send.

  4. There does not need to be any ping/pong code, socket events will take care of everything themselves.

  5. You can offload this connection from the main server and have a separate server for collection of user statistics.

If you have maintained your code well, there will not be a need to add socket snippet to every page. You will just need to add it in a template, say for example.

The other cheaper but not very accurate solution could be to check the session files, say every 15 minutes. All session files that were not accessed in the last 15 minutes are disconnected users so subtract them from the total number of session files and you will get the users who have been online in the last 15 minutes. I am not sure how you would collect detailed statistics using this approach though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM