Skip to content

Commit 20ae8fa

Browse files
committed
minor improvements
1 parent 326b66f commit 20ae8fa

File tree

2 files changed

+49
-57
lines changed

2 files changed

+49
-57
lines changed

README.md

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,18 @@ C# client SDK for [Centrifugo](https://github.com/centrifugal/centrifugo) and [C
99

1010
## Features
1111

12+
- ✅ Multi-platform support
1213
- ✅ WebSocket and HTTP-streaming transports with automatic fallback
1314
- ✅ Browser-native transports for Blazor WebAssembly (using JS interop)
1415
- ✅ Protobuf binary protocol for efficient communication
1516
- ✅ Automatic command batching for improved network efficiency
1617
- ✅ Automatic reconnection with exponential backoff and full jitter
17-
- ✅ Channel subscriptions with recovery and positioning
18+
- ✅ Channel subscriptions with recovery and Fossil delta compression support
1819
- ✅ JWT authentication with automatic token refresh
19-
- ✅ Publication, join/leave events
20-
- ✅ RPC calls
21-
- ✅ Presence and presence stats
20+
- ✅ Publications and RPC calls
21+
- ✅ Presence and presence stats, join/leave events
2222
- ✅ History API
23-
- ✅ Async/await throughout
2423
- ✅ Thread-safe
25-
- ✅ Comprehensive event system
26-
- ✅ Multi-platform support
2724

2825
## Platform Support
2926

@@ -54,7 +51,7 @@ The SDK supports multiple transport protocols with platform-specific implementat
5451
| **WebSocket** | ✅ Native | ✅ Native | ✅ JS Interop* | ✅ Native | ⚠️ Plugin Required |
5552
| **HTTP Stream** | ✅ Native | ✅ Native | ✅ JS Interop* | ✅ Native | ❌ Not Supported |
5653

57-
**\*Blazor WebAssembly**: Requires `IJSRuntime` parameter in constructor. The SDK automatically uses browser-native implementations via JavaScript interop for both WebSocket and HTTP Stream transports.
54+
**\*Blazor WebAssembly**: Requires `builder.Services.AddCentrifugeClient()` in `Program.cs`. The SDK automatically uses browser-native implementations via JavaScript interop for both WebSocket and HTTP Stream transports.
5855

5956
**Unity WebGL**: Requires a third-party WebSocket plugin. HTTP Stream is not supported in WebGL.
6057

@@ -257,16 +254,15 @@ var transports = new[]
257254
)
258255
};
259256

260-
// For regular .NET apps
257+
// Create client with transport fallback
261258
var client = new CentrifugeClient(transports);
262259

263-
// For Blazor WebAssembly (pass IJSRuntime)
264-
var client = new CentrifugeClient(transports, jsRuntime);
265-
266260
// Client will try WebSocket first, then fall back to HTTP Stream if needed
267261
client.Connect();
268262
```
269263

264+
**Note**: In Blazor WebAssembly, ensure you've called `builder.Services.AddCentrifugeClient()` first (see Blazor Support section). The SDK will automatically use browser-native transports.
265+
270266
### Advanced Configuration
271267

272268
```csharp
@@ -279,7 +275,7 @@ var options = new CentrifugeClientOptions
279275
// Connection data
280276
Data = Encoding.UTF8.GetBytes("{\"custom\":\"data\"}"),
281277

282-
// Client identification
278+
// Client identification (used for observability)
283279
Name = "my-app",
284280
Version = "1.0.0",
285281

@@ -294,7 +290,7 @@ var options = new CentrifugeClientOptions
294290
// Logging (pass ILogger for debug output)
295291
Logger = loggerFactory.CreateLogger<CentrifugeClient>(),
296292

297-
// Custom headers (requires Centrifugo v6+)
293+
// Custom headers (works over header emulation, requires Centrifugo v6+)
298294
Headers = new Dictionary<string, string>
299295
{
300296
["X-Custom-Header"] = "value"
@@ -503,60 +499,31 @@ The library works in both Blazor Server and Blazor WebAssembly.
503499

504500
### Blazor Server
505501

506-
Works out of the box - uses standard .NET WebSocket client:
507-
508-
```csharp
509-
@inject CentrifugeClient Client
510-
511-
@code {
512-
protected override async Task OnInitializedAsync()
513-
{
514-
Client.Publication += async (sender, e) =>
515-
{
516-
// Update UI on message
517-
await InvokeAsync(StateHasChanged);
518-
};
519-
520-
Client.Connect();
521-
}
522-
}
523-
```
502+
Works out of the box - uses standard .NET WebSocket client. No special configuration needed.
524503

525504
### Blazor WebAssembly
526505

527-
Requires IJSRuntime for browser WebSocket access. Register the client in `Program.cs`:
506+
The SDK provides a simple DI-based configuration approach, similar to SignalR. Register Centrifuge services once in `Program.cs`:
528507

529508
```csharp
530509
using Centrifugal.Centrifuge;
531-
using Microsoft.AspNetCore.Components.Web;
532-
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
533510

534511
var builder = WebAssemblyHostBuilder.CreateDefault(args);
535512

536-
// Register CentrifugeClient as scoped service
537-
builder.Services.AddScoped(sp =>
538-
{
539-
var jsRuntime = sp.GetRequiredService<IJSRuntime>();
540-
var client = new CentrifugeClient(
541-
"ws://localhost:8000/connection/websocket",
542-
jsRuntime,
543-
new CentrifugeClientOptions
544-
{
545-
// Your options here
546-
}
547-
);
548-
return client;
549-
});
513+
// Add Centrifuge client services - automatically initializes browser interop
514+
builder.Services.AddCentrifugeClient();
550515

551516
await builder.Build().RunAsync();
552517
```
553518

554-
Then inject and use in your components:
519+
Then create and use clients anywhere in your app:
555520

556521
```csharp
557522
@page "/"
558-
@inject CentrifugeClient Client
523+
@inject IJSRuntime JS
524+
@inject ILoggerFactory LoggerFactory
559525
@implements IAsyncDisposable
526+
@using System.Text
560527

561528
<h3>Messages</h3>
562529
<ul>
@@ -567,35 +534,57 @@ Then inject and use in your components:
567534
</ul>
568535

569536
@code {
537+
private CentrifugeClient? _client;
570538
private List<string> messages = new();
571539

572540
protected override async Task OnInitializedAsync()
573541
{
574-
Client.Connected += (sender, e) =>
542+
// Create client directly - no need to pass IJSRuntime
543+
_client = new CentrifugeClient(
544+
"ws://localhost:8000/connection/websocket",
545+
options: new CentrifugeClientOptions
546+
{
547+
Logger = LoggerFactory.CreateLogger<CentrifugeClient>()
548+
}
549+
);
550+
551+
_client.Connected += (sender, e) =>
575552
{
576553
messages.Add($"Connected: {e.ClientId}");
577554
InvokeAsync(StateHasChanged);
578555
};
579556

580-
Client.Publication += (sender, e) =>
557+
var subscription = _client.NewSubscription("chat");
558+
559+
subscription.Publication += (sender, e) =>
581560
{
582561
var data = Encoding.UTF8.GetString(e.Data.Span);
583562
messages.Add($"Received: {data}");
584563
InvokeAsync(StateHasChanged);
585564
};
586565

587-
Client.Connect();
566+
_client.Connect();
567+
subscription.Subscribe();
588568
}
589569

590570
public async ValueTask DisposeAsync()
591571
{
592-
// DisposeAsync waits for disconnect to complete before releasing resources
593-
await Client.DisposeAsync();
572+
if (_client != null)
573+
{
574+
await _client.DisposeAsync();
575+
}
594576
}
595577
}
596578
```
597579

598-
**Important**: The JavaScript interop file is automatically included as a static asset. If you encounter module loading issues, ensure your `index.html` has the standard Blazor script tag:
580+
**How it works:**
581+
582+
- The SDK automatically uses browser-native transports when `AddCentrifugeClient()` is called
583+
- WebSocket uses browser's native WebSocket via JS interop
584+
- HTTP Stream uses browser's Fetch API with ReadableStream
585+
- No need to pass `IJSRuntime` to constructors - it's configured globally
586+
587+
**Important**: The JavaScript interop modules are automatically included as static assets. Ensure your `index.html` has the standard Blazor script tag:
599588

600589
```html
601590
<script src="_framework/blazor.webassembly.js"></script>

examples/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ The easiest way is using Docker (note, we are using client insecure mode here an
1717
```bash
1818
docker pull centrifugo/centrifugo:v6
1919
docker run -p 8000:8000 \
20+
-e CENTRIFUGO_ADMIN_ENABLED="true" \
21+
-e CENTRIFUGO_ADMIN_PASSWORD="change-me-to-secure-password" \
22+
-e CENTRIFUGO_ADMIN_SECRET="change-me-to-secure-secret" \
2023
-e CENTRIFUGO_HTTP_STREAM_ENABLED="true" \
2124
-e CENTRIFUGO_CLIENT_INSECURE="true" \
2225
-e CENTRIFUGO_CLIENT_ALLOWED_ORIGINS="*" \

0 commit comments

Comments
 (0)