---
title: "What is \"OnConnected\" in SignalR?"  
description: "What is \"OnConnected\" in SignalR?"  
author: "Anubhav Sharma"  
published: 2025-12-17  
updated: 2026-05-30  
canonical: https://answers.mindstick.com/qa/116218/what-is-onconnected-in-signalr  
category: "technology"  
tags: ["tech", "asp.net"]  
reading_time: 2 minutes  

---

# What is "OnConnected" in SignalR?

## Answers

### Answer by Ravi Vishwakarma

In **SignalR**, `OnConnected` (or more specifically `OnConnectedAsync` in modern [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 runs when a client successfully establishes a [connection](https://www.mindstick.com/articles/123/connection-string) to a hub.

### ASP.NET Core SignalR

You typically override `OnConnectedAsync` in your Hub class:

```cs
using Microsoft.AspNetCore.SignalR;

public class ChatHub : Hub
{
    public override async Task OnConnectedAsync()
    {
        string connectionId = Context.ConnectionId;

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

        await base.OnConnectedAsync();
    }
}
```

### When it is called

The method executes when:

- A client starts a SignalR connection.
- The SignalR [handshake](https://yourviews.mindstick.com/view/81083/goodbye-to-handshake-in-coronavirus-pandemic) completes successfully.
- The connection is registered by the hub.

### Common uses

- Logging connection [activity](https://answers.mindstick.com/qa/32781/what-is-the-relationship-between-the-life-cycle-of-an-asynctask-and-an-activity-what-problems-can-this-result-in-how-can-these-problems-be-avoided)
- Tracking [online](https://www.mindstick.com/articles/12410/how-can-online-leads-lift-your-roi) users
- Adding users to groups
- Initializing connection-specific data
- Sending a welcome [message](https://www.mindstick.com/interview/682/which-are-the-various-message-map-macros)

Example:

```cs
public override async Task OnConnectedAsync()
{
    await Clients.Caller.SendAsync(
        "ReceiveMessage",
        "Welcome to the chat!");

    await base.OnConnectedAsync();
}
```

### Related lifecycle methods

- `OnConnectedAsync()` – called when a client connects.
- `OnDisconnectedAsync(Exception? exception)` – called when a client disconnects.
- Hub methods (your custom methods) – called when clients invoke [server](https://www.mindstick.com/articles/710/apache-server-installation-on-windows)-side actions.

### Older SignalR versions

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 (pre-Core), the method was named:

```cs
public override Task OnConnected()
{
    return base.OnConnected();
}
```

In ASP.NET Core SignalR, it was renamed to:

```cs
public override Task OnConnectedAsync()
{
    return base.OnConnectedAsync();
}
```

So if you're working with modern SignalR, `OnConnectedAsync()` is the method you'll use to [execute](https://www.mindstick.com/interview/49/how-can-i-execute-a-php-script-using-command-line) logic whenever a new client connection is established.


---

Original Source: https://answers.mindstick.com/qa/116218/what-is-onconnected-in-signalr

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
