Reputation: 5271
Today I was on a german delivery page for food because I was hungry. When I visited the page a popup was displayed which showed the current visitors on the page:
So I asked myself how the hell did they do that? So I tried to do some research in a huge JS file and found following:
This is a part of the function which builds this dialog:
getVisitorsTitle: function() {
return this.getNumberOfVisitors() + " " + s("stickyNote.title")
},
getNumberOfVisitors: function() {
var e, t;
return t = this.bigCityList.indexOf(this.city) > -1 ? {
max: 8,
min: 2
} : {
max: 5,
min: 2
}, e = Math.floor(Math.random() * (t.max - t.min + 1)) + t.min, e = this.getVisitorBoost(e)
}
So I'm not 100% sure but I think that the visitor counter is not a real counter. It's just a random number generated within a range. So this is a lie!
Now I'm asking myself if there is a way to get the current page visitors without faking it with a random number?
Upvotes: 2
Views: 632
Reputation: 371049
The general approach is easy enough to visualize - whenever a user opens the page, open a Websocket (or something similar) so as to have two-way communication between the client and the server. Have the server count up the number of currently active Websocket connections it has for that page (from all clients, filtering out duplicate IP addresses if you want), and send data to the client whenever that number changes. The client can then update the appropriate number text in the browser.
Though, a Websocket isn't necessary - you could achieve the same sort of thing with repeated Ajax calls (eg. request the number from the server every couple minutes, and the server responds with the number of clients which have make requests in the past couple minutes), it would just be more messy.
Upvotes: 4