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

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

SignalR & Real-Time .NET Applications
Course syllabus
1. SignalR Core
2. Managing Users & Groups
3. Scaling SignalR
4. Advanced Communication
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