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

Send Notifications to Groups and Connection Id in SignalR

signalr

8 Articles

Improve

In this article, let's learn about Groups and Connection Id in SignalR.

Note: If you have not done so already, I recommend you read the article on Send Notifications using IHubContext and Caller in SignalR.

Table of Contents

  1. Introduction
  2. Groups and Connection Id
  3. Summary

Introduction

In our previous article we about what is IHubContext and Caller and how to send notifications using IHubContext and Caller. Today in this article, lets learn about another server and client features named Groups and Connection Id available in SignalR.

Groups and Connection Id

Client Groups are like superchargers in SignalR. You can organize clients into groups using their Connection ID. When a client first connects to the hub or calls a hub method, it gets assigned a unique connection ID. This ID sticks around as long as the connection stays alive. Just a heads up though, if a new connection happens, like from a network hiccup or a page refresh (you know, hitting F5 in the browser), the connection ID changes.

But let's get back to the good stuff - groups. They're dynamic and live right in the server's memory. No need to set them up or save them anywhere. Once you add a connection ID to a group, boom, the group exists. And once you've got a group, you can use the Clients property in the hub to talk to all the clients in that group. How cool is that?

Code Sample - Groups and ConnectionId

When a call comes in, we first figure out a name for the group it belongs to. Then, we add the connection ID of the client making the call to that group. The Hub base class has this handy property called Context that gives us info about the connection, like the connection ID. But there's more to come on that.

If the group doesn't exist yet, no sweat. It'll be created when we add that connection ID to it. And if the connection ID was already in the group? No biggie. Adding it again doesn't change a thing. That's the beauty of groups being so dynamic.

Now, here's the fun part: We can call a function on every client in the group, except the one making the call. How? By using the OthersInGroup method on the Clients property, we specify the group name and then call SendAsync.

The Clients property has some extra tricks up its sleeve. For instance, we can send messages to a whole group, including the caller, or leave out specific connection IDs. And if we need to target a specific connection ID or a list of them, we can use the Client and Clients methods. So many options to play around with!

Summary

In this article, we learnt about Groups and Connection Id in SignalR. We also saw how to send notifications using Groups and Connection Id. In the next article, we will learn about Message Pack Hub Protocol and Keep Alive in SignalR.

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