title: What's the proper way to calculate credit in a commercial chat room?
body: <p>I'm working on a project which provides users to chat with experts in private chat rooms. 
  Users buy credits to chat with experts and they will be charged at the end of the every chat session based on the minutes they've talked. Each expert has different credit rate for every minutes of chatting.</p>
  
  <p>A timer starts counting up at the begining of the chat session and users being notified at regular intervals about the total time they spend on chat room and current total credits of the current chat session. These calculations have to be made on the server side and be saved on the database. Btw chat session can be pause / resume by expert.
  <hr/>
  Here is a simple scenario... <br/>
  User currenty has: 10 Credits<br/>
  "Expert A" demands 2credits / per minute</p>
  
  <pre><code>CurrentTime Event       Timer       Credit
  </code></pre>
  
  <ul>
  <li>10:40  Chat Session Starts   <kbd>00:01 min</kbd>   <kbd>2credits </kbd></li>
  <li>10:41  They are chatting     <kbd>00:02 mins</kbd>   <kbd>4credits</kbd></li>
  <li>10:42  Expert on idle        <kbd>00:02 mins</kbd>   <kbd>4credits</kbd> (Chat pauses)</li>
  <li>10:45  Expert becomes online <kbd>00:02 mins</kbd>   <kbd>4credits</kbd> (Chat resumes)</li>
  <li>10:46  They are chatting     <kbd>00:03 mins</kbd>   <kbd>6credits</kbd></li>
  <li>10:46  Client ends session   <kbd>00:04 mins</kbd>   <kbd>8credits</kbd></li>
  </ul>
  
  <p>Client will be charged for 8 credits.
  He has remaining 2 credits.</p>
  
  <blockquote>
    <p>Chat Sessions will be charged at the beginning of the new minute,
    calculations will be made based on minutes, seconds will be omitted.</p>
  </blockquote>
  
  <p><hr/>
  My question is how to make these calculations on server side for every chat sessions being talked currently in a proper way? </p>
  
  <p>My current approach is;</p>
  
  <blockquote>
    <p>A server side timer ticks on every 15 seconds, gets the current chat
    sessions being talked,</p>
    
    <p>For every chat session :  if chat session is not paused, then adds 15
    seconds to the timespan of the session, and then calculates the total
    credits of current session, if user is about to running out of credit,
    notifies him, if he already run out of credit, then end current chat
    session. Save these transactions to the database. Update the chat
    session clients.</p>
  </blockquote>
  
  <p>But there are some pitfalls on this approach. For example, if chat session started now and timer ticks in 2 seconds, then it adds 15 seconds to the total timespan of the current chat session so it makes miscalculation. 
  If I decrease the timer's tick interval, then there may be 500 current chat sessions being talked, and the timer tick interval will not be enough able to calculate every chat sessions credits in 10 seconds for instance.</p>
  
  <p>Is there a better way to handle this? 
  All suggestions are welcome.</p>
  
  <p>By the way, I am using Asp.net MVC4 C# and Signalr to handle real time chat. </p>
  
  <p>Thanks in advance.</p>