I get why removing global state is a good thing in general, but here you are replacing it with a whole lot of complexity for not a particularly clear reason. Is tracking the user count with an integer dangerous <i>in this case</i> (Have you run into problems with it)? Is your solution not using global state anyway, just wrapped in a library?<p>All in all, are the abstractions (a bunch of new concepts plus a new library) <i>worth it</i> to just replace an integer?<p><pre><code> function timerFunc() {
if (connectionCounter) get results();
}
socket.on('connect', function() { connectCounter++; });
socket.on('disconnect', function() { connectCounter--; });
</code></pre>
The real problem is you want to start and stop the timer when someone connects. Just keep it simple and keep it going, but not doing anything if there is nobody connected, like above.