SimpleZ.SignalRManager.RedisConnections
1.0.0
dotnet add package SimpleZ.SignalRManager.RedisConnections --version 1.0.0
NuGet\Install-Package SimpleZ.SignalRManager.RedisConnections -Version 1.0.0
<PackageReference Include="SimpleZ.SignalRManager.RedisConnections" Version="1.0.0" />
paket add SimpleZ.SignalRManager.RedisConnections --version 1.0.0
#r "nuget: SimpleZ.SignalRManager.RedisConnections, 1.0.0"
// Install SimpleZ.SignalRManager.RedisConnections as a Cake Addin #addin nuget:?package=SimpleZ.SignalRManager.RedisConnections&version=1.0.0 // Install SimpleZ.SignalRManager.RedisConnections as a Cake Tool #tool nuget:?package=SimpleZ.SignalRManager.RedisConnections&version=1.0.0
SignalR Manager
SignalR Manager is a easy to use library to track connections of Hubs.
There are two approaches to track connections.
- Local cache
- Redis cache
Local Cache
To use local cache only AddHubController Extension should be called.
builder.Services.AddHubController<int>(config =>
{
config
.AllowedMultiGroupConnection(true)
.AllowedMultiHubConnection(true)
.DefineClaimType(ClaimTypes.SerialNumber);
});
builder.Services.AddSignalR();
Where the generic int means that the Unique Id of user is integer type. The claim type configuration tells the library where the unique id can be found in the users' claims.
Library gives ability to restrict multiple group or hub connections from single user, so that single user can not connect to a single hub or group with several devices at the same time.
After adding HubController Hub object should be declared. For this MapperHub must be used which already connected to the HubController Object.
public class UserHub : MapperHub<int>
{
public UserHub(IHubController<int> hubController) : base(hubController)
{
}
}
Again Generic int refers to the type of user's unique Id;
Inside the MapperHub class HubController property is available from where several useful functions are available.
public class UserHub : MapperHub<int>
{
public UserHub(IHubController<int> hubController) : base(hubController)
{
}
public async Task SendMessageToUser(string userId, string message)
{
// Getting connected user's client Ids and groups by user Id
// The Id is taken from ClaimTypes configured in Program.cs
var connectedUser = await HubController.GetConnectedUserAsync(Int32.Parse(userId));
if(connectedUser == null)
return;
// Message will be sent to all devices connected to this user
if (connectedUser.ConnectionIds.Any())
await Clients.User(userId).SendAsync("SendMessageToUser", userId, $"Specific user: {message}");
// Or can be get specific connection and send a message directly to one device
if (connectedUser.ConnectionIds.Any())
await Clients.Client(connectedUser.ConnectionIds.First().Key)
.SendAsync("SendMessageToUser", userId, $"Specific device: {message}");
}
public async Task SendMessageToGroup(string groupName, string message)
{
// Getting client Ids which are connected to this specific Group
var groupConnections = await HubController.GetGroupConnectionsAsync(groupName);
if (groupConnections.Any())
await Clients.Group(groupName).SendAsync("SendMessageToGroup", $"Group -> {groupName}: {message}");
}
}
Redis Cache
Another way to track Hub connections is redis.
For this following configuration should be used:
builder.Services.AddHubController<int>(
"localhost:6379",
options =>
{
options.AllowedMultiGroupConnection(true)
.AllowedMultiHubConnection(true)
.DefineClaimType(ClaimTypes.SerialNumber);
});
The first parameter is the connection string of redis. The Hub configuration is the same mentioned above.
Also if you want to use SignalR with redis you can use this configuration made by Microsoft:
builder.Services
.AddSignalR()
.AddStackExchangeRedis("localhost:6379");
but this is optional and depends on your needs.
Mapper class is the same here. It still contains HubController property which has the same functionality mentioned above.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net7.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Nito.AsyncEx (>= 5.1.2)
- SimpleZ.SignalRManager.Abstractions (>= 1.0.0)
- StackExchange.Redis (>= 2.6.90)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 182 | 3/20/2023 |