👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Send Notifications using IHubContext and Caller in SignalR

Send Notifications using IHubContext and Caller in SignalR

Author - Abdul Rahman (Bhai)

SignalR

8 Articles

Improve

Table of Contents

  1. What we gonna do?
  2. Why we gonna do?
  3. How we gonna do?
  4. Summary

What we gonna do?

In this article, let's learn about IHubContext and Caller in SignalR.

Note: If you have not done so already, I recommend you read the article on Types of Clients in SignalR.

In our previous article, we learnt the different types of clients in SignalR. Today in this article lets dive deep into the server and client features available in SignalR in .NET. One among them is the IHubContext and Caller available in SignalR.

Why we gonna do?

Sometimes you need to send notifications from outside a hub — from a background service, a REST API controller, or a hosted service. The Hub class is only active when a client is connected, so you need another way to access hub functionality from other parts of the application.

IHubContext is the solution. Without it, you would have no way to push messages from non-hub code. Equally important is understanding Caller — the ability to send a message only back to the client that triggered the hub method — which is only available inside the hub itself and not through IHubContext.

Knowing the difference between IHubContext and Caller, and understanding where each is accessible, is essential for designing a clean and correct SignalR notification architecture.

How we gonna do?

IHubContext and Caller

The IHubContext interface, which is tailored to each hub, can be injected and then used to access the hub's client property. It's super handy to have IHubContext available in dependency injection because it means we can tap into it from anywhere in the app where the Hub class is hanging out. But there's a catch: The caller is only accessible within the hub itself, not through IHubContext.


// Hub
public class ILoveDotNetHub : Hub
{
    public override async Task OnConnectedAsync()
    {
        await Clients.All.SendAsync("ReceiveNewArticle", new Article());
        await Clients.Caller.SendAsync("ConfirmNewArticle"); // Caller Accessible Here.
    }
}

// Program.cs
..
builder.Services.AddSignalR();
..

var app = builder.Build();

..
app.MapHub<ILoveDotNetHub>("notifications");

app.MapPost("article", async (string title, IArticleRepo articleRepo, IHubContext<ILoveDotNetHub> hubContext) =>
{
    articleRepo.NewArticle(title);
    await hubContext.Clients.All.SendAsync("ReceiveNewArticle", new Article(title));
    //hubContext.Caller.SendAsync("ConfirmNewArticle"); --> Caller Is Not Accessible Here.
});
..
            

This leaves us with no possibility to send any notifications to the Caller alone. But there's also the possibility to put clients in groups and then send messages to a specific group.

Summary

In this article, we learnt about IHubContext and Caller in SignalR. We also saw how to use IHubContext to access the hub's client property. In the next article, we will learn about Groups in SignalR.

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • Signalr
  • IHubContext
  • Caller
  • Notifications