---
title: "What is \"OnDisconnected\" in SignalR?"  
description: "What is \"OnDisconnected\" in SignalR?"  
author: "Anubhav Sharma"  
published: 2025-12-17  
updated: 2026-05-29  
canonical: https://answers.mindstick.com/qa/116219/what-is-ondisconnected-in-signalr  
category: "technology"  
tags: ["tech", "asp.net", ".net programming"]  
reading_time: 2 minutes  

---

# What is "OnDisconnected" in SignalR?

## Answers

### Answer by Ravi Vishwakarma

In **SignalR**, `OnDisconnected` (or `OnDisconnectedAsync` in [ASP.NET Core](https://www.mindstick.com/articles/326150/asp-dot-net-core-why-it-is-best-suited-for-banking-and-finance-sectors) SignalR) is a hub [lifecycle method](https://answers.mindstick.com/qa/33377/which-activity-lifecycle-method-gets-called-whenever-the-screen-of-the-device-turns-off-an-then-turns-on) that is called when a client connection is terminated.

### ASP.NET Core SignalR

You override `OnDisconnectedAsync` in your Hub:

```cs
using Microsoft.AspNetCore.SignalR;

public class ChatHub : Hub
{
    public override async Task OnDisconnectedAsync(Exception? exception)
    {
        string connectionId = Context.ConnectionId;

        Console.WriteLine($"Client disconnected: {connectionId}");

        await base.OnDisconnectedAsync(exception);
    }
}
```

### When it is called

The method runs when a client disconnects for reasons such as:

- The user closes the browser/tab.
- The client explicitly stops the SignalR connection.
- A [network](https://www.mindstick.com/articles/342247/choosing-the-right-business-network-solution-for-growth) interruption causes the connection to end.
- The server disconnects the client.
- A [connection timeout](https://www.mindstick.com/forum/1621/connection-timeout-in-urlconnection-using-java) occurs.

### The `exception` parameter

```cs
public override async Task OnDisconnectedAsync(Exception? exception)
{
    if (exception != null)
    {
        Console.WriteLine($"Disconnected due to error: {exception.Message}");
    }

    await base.OnDisconnectedAsync(exception);
}
```

`exception == null` usually indicates a normal disconnect.

A non-null [exception](https://www.mindstick.com/articles/61/exception-handling-in-c-sharp) may indicate an unexpected termination or [transport](https://answers.mindstick.com/qa/72238/india-s-first-rail-transport-university-will-be-established-at-which-city) error.

### Common uses

#### Track online users

```cs
public override async Task OnDisconnectedAsync(Exception? exception)
{
    OnlineUsers.Remove(Context.ConnectionId);

    await base.OnDisconnectedAsync(exception);
}
```

#### Remove users from custom tracking

```cs
public override async Task OnDisconnectedAsync(Exception? exception)
{
    _connectionManager.RemoveConnection(Context.ConnectionId);

    await base.OnDisconnectedAsync(exception);
}
```

#### Notify other clients

```cs
public override async Task OnDisconnectedAsync(Exception? exception)
{
    await Clients.All.SendAsync(
        "UserDisconnected",
        Context.ConnectionId);

    await base.OnDisconnectedAsync(exception);
}
```

### Relationship with `OnConnectedAsync`

A typical connection lifecycle looks like:

```plaintext
Client connects
    ↓
OnConnectedAsync()
    ↓
Client invokes hub methods
    ↓
Connection ends
    ↓
OnDisconnectedAsync()
```

### Older ASP.NET SignalR

In [classic ASP.NET](https://www.mindstick.com/forum/157453/what-are-some-benefits-of-asp-dot-net-core-over-the-classic-asp-dot-net) SignalR, the method was:

```cs
public override Task OnDisconnected(bool stopCalled)
{
    return base.OnDisconnected(stopCalled);
}
```

In ASP.NET Core SignalR, it became:

```cs
public override Task OnDisconnectedAsync(Exception? exception)
{
    return base.OnDisconnectedAsync(exception);
}
```

So `OnDisconnectedAsync` is the place to perform cleanup, update user [presence](https://www.mindstick.com/articles/12492/how-to-enhance-your-presence-on-facebook), release [resources](https://www.mindstick.com/interview/1391/explain-how-to-retrieve-resources-using-resourcemanager-class), and notify other clients when a SignalR connection ends.


---

Original Source: https://answers.mindstick.com/qa/116219/what-is-ondisconnected-in-signalr

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
