Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Get online users count and registered users count ( How wrong is my logic ? #1 )

Get online users count and registered users count ( How wrong is my logic ? #1 )

Scheduled Pinned Locked Moved ASP.NET
asp-netdatabasecsharpsql-serversysadmin
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hristiyan
    wrote on last edited by
    #1

    Good day! I read several acticles about getting the "users online count" and "registered users count". However most of them were not full or lack performance. So i decided to make my own logic and i would like to share it with you. Can you please comment on it ? Is my logic wrong ? Get registered users count: DB logic: - Store the count in a special table on the SQL server as a parameter. Increment the count when a new user is created. Decrement count when user is deleted ( or expired ). There is an alternative to SELECT COUNT(*) FROM Users...... but i discarded it. I don't think it's the best way to achieve my goal. Get online users count: DB logic: - Store session information in a special table on the SQL server. Update the table whenever an activity is idicated ( the important here is the LastActiveDate and ExpirationDate ... which determine if the session is expired ). - Logic for purging expired sessions and return the online users count after the purging. ASP.NET Web Server logic: Web the application is started ( Application_Start Global.asax ) i start a System.TIMERS.TIMER with interval of 10 minutes. Every 10 minutes i get the registered users count, purge the expired sessions , get online users count and keep the stored data as static. On Application_End i dispose the timer. However all my web forms are inherited by a base form in which "Page_Load" handler i idicate an activity and update the session information table on the sql server. I'm not quite sure if my logic is right about all those steps. Please comment. Best wishesh , Hristiyan

    S 1 Reply Last reply
    0
    • H Hristiyan

      Good day! I read several acticles about getting the "users online count" and "registered users count". However most of them were not full or lack performance. So i decided to make my own logic and i would like to share it with you. Can you please comment on it ? Is my logic wrong ? Get registered users count: DB logic: - Store the count in a special table on the SQL server as a parameter. Increment the count when a new user is created. Decrement count when user is deleted ( or expired ). There is an alternative to SELECT COUNT(*) FROM Users...... but i discarded it. I don't think it's the best way to achieve my goal. Get online users count: DB logic: - Store session information in a special table on the SQL server. Update the table whenever an activity is idicated ( the important here is the LastActiveDate and ExpirationDate ... which determine if the session is expired ). - Logic for purging expired sessions and return the online users count after the purging. ASP.NET Web Server logic: Web the application is started ( Application_Start Global.asax ) i start a System.TIMERS.TIMER with interval of 10 minutes. Every 10 minutes i get the registered users count, purge the expired sessions , get online users count and keep the stored data as static. On Application_End i dispose the timer. However all my web forms are inherited by a base form in which "Page_Load" handler i idicate an activity and update the session information table on the sql server. I'm not quite sure if my logic is right about all those steps. Please comment. Best wishesh , Hristiyan

      S Offline
      S Offline
      SeMartens
      wrote on last edited by
      #2

      Hi, 1. Why do you store the user count separately? What is wrong with count(*)? You have to write much more code (possible point of bugs etc.) when you store the count in an extra table. 2. Why do you store the session information within a table? You could use a static dataobject (could be a simple int) within your ASP.NET application. Just increment/decrement the counter whenever a session ends (use Global.asax for this) 3. To update the visible count on your site just query the static dataobject to get the active users. To get all registered users use the sql-query. You could do something like "Store the value for one hour. After one hour ask query the registered users again" You don't need a timer for this, just do it in a master page whenever you get a postback. To update the site you could use a little JavaScript that will force the client to update in an interval. Hope this helps you a bit. Regards Sebastian

      It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

      H 1 Reply Last reply
      0
      • S SeMartens

        Hi, 1. Why do you store the user count separately? What is wrong with count(*)? You have to write much more code (possible point of bugs etc.) when you store the count in an extra table. 2. Why do you store the session information within a table? You could use a static dataobject (could be a simple int) within your ASP.NET application. Just increment/decrement the counter whenever a session ends (use Global.asax for this) 3. To update the visible count on your site just query the static dataobject to get the active users. To get all registered users use the sql-query. You could do something like "Store the value for one hour. After one hour ask query the registered users again" You don't need a timer for this, just do it in a master page whenever you get a postback. To update the site you could use a little JavaScript that will force the client to update in an interval. Hope this helps you a bit. Regards Sebastian

        It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

        H Offline
        H Offline
        Hristiyan
        wrote on last edited by
        #3

        Thank you for your answer ! My oppinion (I'm not saying it's correct.... it's just what i think) : 1. Assume i have 100 000 users. SELECT COUNT(*) "count every time all the users when that statement is executed". Thats some really big unnecessary weight over the SQL server. I don't think thats the right approach. 2. I think that logic is incorrect. You don't increment and decrement the the online users when the Session_Start and Session_End. Assume that the user closes the browser. The logic fails there - it will actually count that user as online. 3. I think that logic is incorrect. You cannot query the database with that unnecessary logic weight every time there is a postback. That will make a segnificant performance loss. I store the session info in the database, because it's needed for other purposes too. However i update it every time a user makes a postback and purge the expired data every 10 minutes. Please give advices. Best wishes , Hristiyan !

        S 1 Reply Last reply
        0
        • H Hristiyan

          Thank you for your answer ! My oppinion (I'm not saying it's correct.... it's just what i think) : 1. Assume i have 100 000 users. SELECT COUNT(*) "count every time all the users when that statement is executed". Thats some really big unnecessary weight over the SQL server. I don't think thats the right approach. 2. I think that logic is incorrect. You don't increment and decrement the the online users when the Session_Start and Session_End. Assume that the user closes the browser. The logic fails there - it will actually count that user as online. 3. I think that logic is incorrect. You cannot query the database with that unnecessary logic weight every time there is a postback. That will make a segnificant performance loss. I store the session info in the database, because it's needed for other purposes too. However i update it every time a user makes a postback and purge the expired data every 10 minutes. Please give advices. Best wishes , Hristiyan !

          S Offline
          S Offline
          SeMartens
          wrote on last edited by
          #4

          1.+3. Well that's why I said you could store it in a variable that will be update every hour or so... 2. The problem is that you can't get the event if the user closes the browser. What you will get is a session end, when the timeout expires... so the mechanism implemented with the session object (timeout etc.) is the same as you have written by yourself (as you explained in your first message). Every I/O action takes time. If you have 10.000 users online you will have at approx. 1.000 sql inserts/updates every minute when you update the session data within your sql server. Using a select count(*) every hour seems a blink of an eye to me then. Regards Sebastian

          It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

          H 1 Reply Last reply
          0
          • S SeMartens

            1.+3. Well that's why I said you could store it in a variable that will be update every hour or so... 2. The problem is that you can't get the event if the user closes the browser. What you will get is a session end, when the timeout expires... so the mechanism implemented with the session object (timeout etc.) is the same as you have written by yourself (as you explained in your first message). Every I/O action takes time. If you have 10.000 users online you will have at approx. 1.000 sql inserts/updates every minute when you update the session data within your sql server. Using a select count(*) every hour seems a blink of an eye to me then. Regards Sebastian

            It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

            H Offline
            H Offline
            Hristiyan
            wrote on last edited by
            #5

            However , since i've already implemented my logic ( now i'm testing it ), i'm going to ask you in general: Do you strong disagree with my logic ? Do you see a reasonable point to delete my current implementation and "make it the easy way" ? Regards , Hristiyan

            S 1 Reply Last reply
            0
            • H Hristiyan

              However , since i've already implemented my logic ( now i'm testing it ), i'm going to ask you in general: Do you strong disagree with my logic ? Do you see a reasonable point to delete my current implementation and "make it the easy way" ? Regards , Hristiyan

              S Offline
              S Offline
              SeMartens
              wrote on last edited by
              #6

              If you already implemented it and it is working I wouldn't throw away the logic. But if you encouter any problems at a later point I would think about refactoring. Regards Sebastian

              It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups