Get current page visitors on document ready in JS












1















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:



enter image description here



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?










share|improve this question



























    1















    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:



    enter image description here



    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?










    share|improve this question

























      1












      1








      1








      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:



      enter image description here



      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?










      share|improve this question














      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:



      enter image description here



      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?







      javascript jquery html






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 23:20









      Mr. JoMr. Jo

      852318




      852318
























          1 Answer
          1






          active

          oldest

          votes


















          4














          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.






          share|improve this answer



















          • 1





            +1. I call the second one the ping-pong method.

            – JBis
            Nov 15 '18 at 23:41






          • 1





            This is easy to do with signalR, handles the Websocket if available, or falls back to long polling (repeated ajax requests) if not. Your server needs to track and store the connections but signalR handles this on the server as well, so you can easily push out updates to "subscribers" of a specific event, such as user connected/disconnected.

            – Chad H
            Nov 15 '18 at 23:42






          • 1





            Also wtf SO. We aren't allowed to do plus one comments anymore? Had to use diff unicode character.

            – JBis
            Nov 15 '18 at 23:43











          • Sounds like I can't do it just with jQuery. This will be some huge coding right?

            – Mr. Jo
            Nov 16 '18 at 1:20






          • 1





            @Mr.Jo I don't think so, I'd think something like this could probably be done in less than 100 lines total, for the client and server code combined - it's quite straightforward. The difficulty would come from having to learn websocket or ajax syntax (if you aren't familiar with it already), and from having to figure out how to handle it in the back-end (in whichever language/framework), if you aren't familiar with it already. Requires knowledge, but not much coding

            – CertainPerformance
            Nov 16 '18 at 1:57











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329241%2fget-current-page-visitors-on-document-ready-in-js%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          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.






          share|improve this answer



















          • 1





            +1. I call the second one the ping-pong method.

            – JBis
            Nov 15 '18 at 23:41






          • 1





            This is easy to do with signalR, handles the Websocket if available, or falls back to long polling (repeated ajax requests) if not. Your server needs to track and store the connections but signalR handles this on the server as well, so you can easily push out updates to "subscribers" of a specific event, such as user connected/disconnected.

            – Chad H
            Nov 15 '18 at 23:42






          • 1





            Also wtf SO. We aren't allowed to do plus one comments anymore? Had to use diff unicode character.

            – JBis
            Nov 15 '18 at 23:43











          • Sounds like I can't do it just with jQuery. This will be some huge coding right?

            – Mr. Jo
            Nov 16 '18 at 1:20






          • 1





            @Mr.Jo I don't think so, I'd think something like this could probably be done in less than 100 lines total, for the client and server code combined - it's quite straightforward. The difficulty would come from having to learn websocket or ajax syntax (if you aren't familiar with it already), and from having to figure out how to handle it in the back-end (in whichever language/framework), if you aren't familiar with it already. Requires knowledge, but not much coding

            – CertainPerformance
            Nov 16 '18 at 1:57
















          4














          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.






          share|improve this answer



















          • 1





            +1. I call the second one the ping-pong method.

            – JBis
            Nov 15 '18 at 23:41






          • 1





            This is easy to do with signalR, handles the Websocket if available, or falls back to long polling (repeated ajax requests) if not. Your server needs to track and store the connections but signalR handles this on the server as well, so you can easily push out updates to "subscribers" of a specific event, such as user connected/disconnected.

            – Chad H
            Nov 15 '18 at 23:42






          • 1





            Also wtf SO. We aren't allowed to do plus one comments anymore? Had to use diff unicode character.

            – JBis
            Nov 15 '18 at 23:43











          • Sounds like I can't do it just with jQuery. This will be some huge coding right?

            – Mr. Jo
            Nov 16 '18 at 1:20






          • 1





            @Mr.Jo I don't think so, I'd think something like this could probably be done in less than 100 lines total, for the client and server code combined - it's quite straightforward. The difficulty would come from having to learn websocket or ajax syntax (if you aren't familiar with it already), and from having to figure out how to handle it in the back-end (in whichever language/framework), if you aren't familiar with it already. Requires knowledge, but not much coding

            – CertainPerformance
            Nov 16 '18 at 1:57














          4












          4








          4







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 23:28









          CertainPerformanceCertainPerformance

          94.2k165584




          94.2k165584








          • 1





            +1. I call the second one the ping-pong method.

            – JBis
            Nov 15 '18 at 23:41






          • 1





            This is easy to do with signalR, handles the Websocket if available, or falls back to long polling (repeated ajax requests) if not. Your server needs to track and store the connections but signalR handles this on the server as well, so you can easily push out updates to "subscribers" of a specific event, such as user connected/disconnected.

            – Chad H
            Nov 15 '18 at 23:42






          • 1





            Also wtf SO. We aren't allowed to do plus one comments anymore? Had to use diff unicode character.

            – JBis
            Nov 15 '18 at 23:43











          • Sounds like I can't do it just with jQuery. This will be some huge coding right?

            – Mr. Jo
            Nov 16 '18 at 1:20






          • 1





            @Mr.Jo I don't think so, I'd think something like this could probably be done in less than 100 lines total, for the client and server code combined - it's quite straightforward. The difficulty would come from having to learn websocket or ajax syntax (if you aren't familiar with it already), and from having to figure out how to handle it in the back-end (in whichever language/framework), if you aren't familiar with it already. Requires knowledge, but not much coding

            – CertainPerformance
            Nov 16 '18 at 1:57














          • 1





            +1. I call the second one the ping-pong method.

            – JBis
            Nov 15 '18 at 23:41






          • 1





            This is easy to do with signalR, handles the Websocket if available, or falls back to long polling (repeated ajax requests) if not. Your server needs to track and store the connections but signalR handles this on the server as well, so you can easily push out updates to "subscribers" of a specific event, such as user connected/disconnected.

            – Chad H
            Nov 15 '18 at 23:42






          • 1





            Also wtf SO. We aren't allowed to do plus one comments anymore? Had to use diff unicode character.

            – JBis
            Nov 15 '18 at 23:43











          • Sounds like I can't do it just with jQuery. This will be some huge coding right?

            – Mr. Jo
            Nov 16 '18 at 1:20






          • 1





            @Mr.Jo I don't think so, I'd think something like this could probably be done in less than 100 lines total, for the client and server code combined - it's quite straightforward. The difficulty would come from having to learn websocket or ajax syntax (if you aren't familiar with it already), and from having to figure out how to handle it in the back-end (in whichever language/framework), if you aren't familiar with it already. Requires knowledge, but not much coding

            – CertainPerformance
            Nov 16 '18 at 1:57








          1




          1





          +1. I call the second one the ping-pong method.

          – JBis
          Nov 15 '18 at 23:41





          +1. I call the second one the ping-pong method.

          – JBis
          Nov 15 '18 at 23:41




          1




          1





          This is easy to do with signalR, handles the Websocket if available, or falls back to long polling (repeated ajax requests) if not. Your server needs to track and store the connections but signalR handles this on the server as well, so you can easily push out updates to "subscribers" of a specific event, such as user connected/disconnected.

          – Chad H
          Nov 15 '18 at 23:42





          This is easy to do with signalR, handles the Websocket if available, or falls back to long polling (repeated ajax requests) if not. Your server needs to track and store the connections but signalR handles this on the server as well, so you can easily push out updates to "subscribers" of a specific event, such as user connected/disconnected.

          – Chad H
          Nov 15 '18 at 23:42




          1




          1





          Also wtf SO. We aren't allowed to do plus one comments anymore? Had to use diff unicode character.

          – JBis
          Nov 15 '18 at 23:43





          Also wtf SO. We aren't allowed to do plus one comments anymore? Had to use diff unicode character.

          – JBis
          Nov 15 '18 at 23:43













          Sounds like I can't do it just with jQuery. This will be some huge coding right?

          – Mr. Jo
          Nov 16 '18 at 1:20





          Sounds like I can't do it just with jQuery. This will be some huge coding right?

          – Mr. Jo
          Nov 16 '18 at 1:20




          1




          1





          @Mr.Jo I don't think so, I'd think something like this could probably be done in less than 100 lines total, for the client and server code combined - it's quite straightforward. The difficulty would come from having to learn websocket or ajax syntax (if you aren't familiar with it already), and from having to figure out how to handle it in the back-end (in whichever language/framework), if you aren't familiar with it already. Requires knowledge, but not much coding

          – CertainPerformance
          Nov 16 '18 at 1:57





          @Mr.Jo I don't think so, I'd think something like this could probably be done in less than 100 lines total, for the client and server code combined - it's quite straightforward. The difficulty would come from having to learn websocket or ajax syntax (if you aren't familiar with it already), and from having to figure out how to handle it in the back-end (in whichever language/framework), if you aren't familiar with it already. Requires knowledge, but not much coding

          – CertainPerformance
          Nov 16 '18 at 1:57




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329241%2fget-current-page-visitors-on-document-ready-in-js%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python