title: How to track the number of anonymous users server-side in Meteor?
body: <p>I'm writing a data-sensitive application in Meteor, and am trying to limit the client access to as much information as possible. Therefore, I want to implement server side a way of counting the number of logged-in, and anonymous, users.</p>
  
  <p>I have tried a variety of methods. The first was as outlined in this question <a href="https://stackoverflow.com/questions/10257958/server-cleanup-after-a-client-disconnects">Server cleanup after a client disconnects</a>, which suggests hooking into:</p>
  
  <pre><code>this.session.socket.on("close")
  </code></pre>
  
  <p>However when I did, and tried to change a collection, it threw a "Meteor code must always run within a Fiber" error. I assume this problem is because once the socket is closed, that Fiber is killed, and so accessing the database is impossible. The OP pointed to this <a href="https://stackoverflow.com/questions/10192938/meteor-code-must-always-run-within-a-fiber-when-calling-collection-insert-on-s">&quot;Meteor code must always run within a Fiber&quot; when calling Collection.insert on server</a> as a possible solution, but I wasn't sure if that's the best method, based on the comments to the answer.</p>
  
  <p>I then tried to autorun on the variable:</p>
  
  <pre><code>Meteor.default_server.stream_server.all_sockets().length
  </code></pre>
  
  <p>but the autorun never seemed to be called, so I'm assuming that variable is not a reactive context, and I wasn't sure how to make it one.</p>
  
  <p>The last idea was to do a keepalive style thing, but that seems to completely go against the grain of the Meteor philosophy, and I think I'll only use as an absolute last resort.</p>
  
  <p>I did a <code>console.log</code> of the functions on <code>this.session.socket</code>, and the only other function possible was <code>.on("data")</code>, but this isn't called when the socket is closed.</p>
  
  <p>I'm at a bit of a loss here, so any help would be great,
  Thanks.</p>