Tracking online users in signalr: where I should create instance of PresenceManager and how to pass the parameter heartbeat to it
<p>I read this <a href="http://patdunn.net/2015/05/05/building-a-simple-presence-manager-for-signalr/" rel="nofollow">topic</a> which is about enhancing the method for tracking online users using database. In short, the method use the following database :</p>

<pre><code>public class ConnectedClient
{
    public long ID { get; set; }
    [StringLength(100)]
    public string ConnectionId { get; set; }
    public ConnectionStatus Status { get; set; }
    public string ApplicationUserID { get; set; }
    public string UserAgent { get; set; }
    public DateTimeOffset LastActivity { get; set; }

    //Relationship
    public ApplicationUser ApplicationUser { get; set; }
}
</code></pre>

<p>And for tracking online users :</p>

<pre><code>public class PresenceManager
    {
        private const int zombiePeriodThreshold = 2;
        private readonly ITransportHeartbeat _heartbeat;
        private readonly TimeSpan _checkInterval = TimeSpan.FromSeconds(60);
        private readonly int _zombieThreshold;
        private Timer timer;

        public PresenceManager(ITransportHeartbeat heartbeat)
        {
            _heartbeat = heartbeat;
            _zombieThreshold = (int)_checkInterval.TotalSeconds * zombiePeriodThreshold;
        }

        public void StartMonitoring()
        {
            if (timer == null)
            {
                timer = new Timer(t =&gt;
                {
                    try
                    {
                        Check();
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceError(ex.Message);
                    }
                }, null, TimeSpan.Zero, _checkInterval);
            }
        }

        //EF6 adds much better support for contains in queries http://bit.ly/1wmQ2VQ
        private void Check()
        {
            using (var db = new ApplicationContext())
            {
                //Update active connections
                var activeConnections = _heartbeat.GetConnections().Where(b =&gt; b.IsAlive).Select(b =&gt; b.ConnectionId);
                db.ConnectedClients
                    .Where(c =&gt; activeConnections.Contains(c.ConnectionId))
                    .ToList()
                    .ForEach(c =&gt; c.LastActivity = DateTimeOffset.UtcNow);

               //Remove zombie connections
               var zombies = db.ConnectedClients.Where(c =&gt; SqlFunctions.DateDiff("ss", c.LastActivity, DateTimeOffset.UtcNow) &gt;= _zombieThreshold).ToList();
               var zombieUserIDs = zombies.Select(b =&gt; b.ApplicationUserID);
               db.Users
                   .Where(c =&gt; zombieUserIDs.Contains(c.Id))
                   .Where(c =&gt; c.ConnectedClients.Count &lt;= 1)
                   .ToList()
                   .ForEach(u =&gt; u.Status = UserStatus.Offline);

                db.ConnectedClients.RemoveRange(zombies);
                db.SaveChanges();
            }
        }

    }
</code></pre>

<p>I understand all the code above, the only thing that I don’t know is where I should create instance of <code>PresenceManager</code> and how to pass the parameter <code>heartbeat</code> to it.</p>