Tutorials SignalR & Real-Time .NET Applications

Presence Tracking: Who is online?

On this page

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."

SignalR & Real-Time .NET Applications
Course syllabus
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
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details