SignalR & Real-Time .NET Applications
Lesson 8 of 16 50% of course

Presence Tracking: Who is online?

16 · 8 min · 5/23/2026

Sign in to track progress and bookmarks.

Real-time Presence

'Is John online?' Presence tracking is a classic real-time feature that requires careful state management on the server.

1. OnConnected and OnDisconnected

Every Hub has these two overrideable methods. When a user connects, you can increment a count in Redis or update their 'LastSeen' status in your database. When they disconnect, you do the opposite. Then, you broadcast a UserStatusChanged event to all their friends.

2. Handling Multiple Connections

The trick: If a user has 3 tabs open, they are 'Online'. If they close ONE tab, they are still 'Online'. You only mark them 'Offline' when their **Last** connection is closed. This requires maintaining a mapping of UserID -> Set of ConnectionIDs in a distributed cache like Redis.

3. Architect Insight

Q: "Should I use my SQL database for presence?"

Architect Answer: "NO. Presence updates are incredibly frequent. Writing to SQL every time a user refreshes a page will kill your database performance. Use **Redis** with a short TTL (Time-To-Live). This is much faster and automatically 'expires' users if your server crashes and fails to run the OnDisconnected logic."

Test your knowledge

Quizzes linked to this course—pass to earn certificates.

Browse all quizzes
SignalR & Real-Time .NET Applications

On this page

1. OnConnected and OnDisconnected 2. Handling Multiple Connections 3. Architect Insight
1. SignalR Core
Real-time Theory: WebSockets vs Long Polling vs Server-Sent Events SignalR Hub Anatomy: Methods, Callbacks, and Protocols Configuring the Connection: Transports and Retries Strongly Typed Hubs: Enforcing the contract
2. Managing Users & Groups
Authentication & Authorization in SignalR Managing Connection IDs and User Identifiers Group Management: Designing Rooms and Channels Presence Tracking: Who is online?
3. Scaling SignalR
The Stateless Problem: Sticky sessions and Load Balancers Redis Backplane: Syncing multiple servers Azure SignalR Service: Offloading the connection load Monitoring Connection Health with Hub Metrics
4. Advanced Communication
Server-to-Client Streaming: Sending large data chunks Client-to-Server Streaming: Uploading in real-time Binary Protocols: Using MessagePack for extreme speed Handling Large Payload strategies