简体   繁体   中英

How to monitor the number of users online in a site realtime

I've tried to implement the Google Analytics like statistical platform. Everything works fine except the real-time feature. It works but it consumes some bandwidth due to AJAX calls at regular intervals. But, I think Google Analytics is not using this method. So, was there any alternatives than using the inefficient AJAX method like sessions in PHP or keeping a live connection with the client?

You can use WebSockets, which maintains a persistent connection from the browser to the server. You can determine when someone leaves the page using one of a couple of methods, such as:

  • Code sent through the socket connection just before the page unloads.
  • The socket connection is unexpectedly dropped.

While the connection is open, you can send other information over the connection to gather analytic information, such as if someone clicks on a particular element, or if they've clicked anywhere on the page and where.

WebSockets is relatively new, and so requires a modern browser to function correctly. According to this site , WebSockets is fully supported in IE10, Firefox 6, Chrome 14, Safari 6, and Opera 12.1. If you need to support earlier browsers, you'd want to use an AJAX fallback. As browsers mature, the AJAX fallback will become less and less necessary.

For a PHP WebSocket implementation, you can try here .

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