https://github.com/open-net-libraries/open.signalr.sharedclient
A HubConnection adapter and provider for easily sharing SignalR connections.
https://github.com/open-net-libraries/open.signalr.sharedclient
Last synced: 6 months ago
JSON representation
A HubConnection adapter and provider for easily sharing SignalR connections.
- Host: GitHub
- URL: https://github.com/open-net-libraries/open.signalr.sharedclient
- Owner: Open-NET-Libraries
- License: mit
- Created: 2025-02-08T00:54:05.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-02-13T20:58:52.000Z (12 months ago)
- Last Synced: 2025-06-19T22:53:02.920Z (7 months ago)
- Language: C#
- Size: 774 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Open.SignalR.SharedClient
[](https://www.nuget.org/packages/Open.SignalR.SharedClient/)
A scoped hub connection and provider for easily sharing SignalR connections.
## `IScopedHubConnection`
Provides all the same functionality as a `HubConnection`,
but does not interfere with other `IScopedHubConnection` instances.
Disposing the scoped connection cleans up all the handlers
but leaves the connection intact for others to use.
## Usage
### DI Setup
#### Default
Assumes the consumers will know which URL to ask for.
```csharp
services.AddScopedHubConnectionProvider();
```
Consumers use `.GetConnectionFor(url)` to get a hub connection.
#### With Named Connections
Allows for any hub configuration to be added and retrieved by name.
> Note: Client/Server hybrid apps will need to replicate the DI setup on the server side.
```csharp
services.AddScopedHubConnectionProvider(serviceProvider => {
var nav = serviceProvider.GetRequiredService();
return [
// Hub 1
KeyValuePair.Create("hub1",
new HubConnectionBuilder()
.WithAutomaticReconnect()
.WithUrl(nav.ToAbsoluteUri("/hubs/hub1"))),
// Hub 2
KeyValuePair.Create("hub2",
new HubConnectionBuilder()
.WithUrl(nav.ToAbsoluteUri("/hubs/hub2")))
];
});
```
Consumers use `.GetGonnectionByName(hubName)` to get a hub connection.
### Example
```csharp
public class MyService : IDisposable
{
private readonly IScopedHubConnection _hub;
public MyService(IScopedHubConnectionProvider connectionProvider)
{
_hub = connectionProvider.GetConnectionByNam("hub1");
_hub.On("MyMethod1", (string arg1, string arg2) =>
{
// Do something when the method is invoked on the hub.
});
_hub.On("MyMethod2", (string arg1, string arg2) =>
{
// Do something when the method is invoked on the hub.
});
// Automatically invokes OnConnectionEstablished when a connnection is available
// and invokes it on every reconnection.
_hub.OnConnected(OnConnectionEstablished);
}
public void Dispose() => _hub.Dispose();
static void OnConnectionEstablished(IScopedHubConnection hub)
{
hub.SendAsync("SubTo", "MyMethod1");
hub.SendAsync("SubTo", "MyMethod2");
}
public async Task DoSomething()
{
// Automatically guarantees a connection and invokes the method.
await _hub.InvokeAsync("MyMethod", "arg1", "arg2");
}
}
```