Skip to content

Commit 62d682a

Browse files
authored
Update README.md
1 parent b71fb67 commit 62d682a

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

README.md

+84-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,84 @@
1-
# BlazorSignalRApp
1+
# Blazor SignalR Demo
2+
3+
Sample application to demonstrate integrating web sockets into a balzor server application using [SignalR](https://learn.microsoft.com/en-us/aspnet/core/signalr/introduction?view=aspnetcore-7.0).
4+
5+
## Code Organization
6+
- **Hubs** - Hubs define the methods that will be exposed as RPCs to the client to invoke or subscribe to, these hubs are stateless in nature and get destroyed and created in-line with the life cycle of the UI component
7+
- **Data** - Data classes are used to define the format of data that will be transmitted over the SignalR hubs.
8+
- **Pages** - Pages represent the razor pages that will consume data from SignalR.
9+
10+
## Steps For WebSockets
11+
- **Step 1** Install Nuget Package [Microsoft.AspNetCore.SignalR.Client ](https://www.nuget.org/packages/Microsoft.AspNetCore.SignalR.Client), Make sure to install the Client library and not the named Core.
12+
- **Step 2** Configure the server to be able to octet streams to send data over web sockets and initialize the SignalR Hubs in the "Program.cs" file. Make sure to import the required name spaces form the installed nuget package
13+
```
14+
using Microsoft.AspNetCore.ResponseCompression;
15+
using BlazorSignalRApp.Hubs;
16+
17+
builder.Services.AddResponseCompression(opts =>
18+
{
19+
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { "application/octet-stream" });
20+
});//Configure server to send response as octet stream over web sockets
21+
```
22+
23+
```
24+
app.MapHub<ChatHub>("/chathub");
25+
app.MapHub<PlotHub>("/plothub", (options) =>
26+
{
27+
//options.TransportMaxBufferSize = 131072;
28+
});
29+
```
30+
- **Step 3** Define Hubs for data transfer, Hubs cab be a good way to organize different data streams. Methods exposed by the hub can be invoked from razor pages to transfer data back to the server.
31+
```
32+
public class PlotHub : Hub
33+
{
34+
//Signal R Hub to configure and receive sample data for UI plot
35+
private IPlotData _plotData;
36+
public PlotHub(IHubContext<PlotHub> hubContext, IPlotData plotData)
37+
{
38+
_plotData = plotData;
39+
}
40+
public void SetSamplesRate(int samplesPerTimeInterval, int timeIntervalInMs)
41+
{
42+
//Configures the PlotData class and starts sending data
43+
_plotData.SetSamplesRate(samplesPerTimeInterval, timeIntervalInMs);
44+
}
45+
46+
public void ResetPlotDataSource()
47+
{
48+
//Resets the PlotData class
49+
_plotData.ResetPlotDataSource();
50+
}
51+
}
52+
```
53+
- **Step 4** Use the "SendAsync" method to send data to all the registered clients from the server. The method named defined here is used in the razor pages to hook up call back methods when data is made available on these streams.
54+
```
55+
return _hubContext.Clients.All.SendAsync("ReceiveSamples", Samples.ToArray());
56+
```
57+
- **Step 5** Override the "OnInitializedAsync" life cycle method to setup connection the signalR hub and configure the call back methods for the streams that are of interest to the component.
58+
```
59+
protected override async Task OnInitializedAsync()
60+
{
61+
//Create a signalR client for the required hub
62+
hubConnection = new HubConnectionBuilder()
63+
.WithUrl(NavManager.ToAbsoluteUri("/plothub"))
64+
.WithAutomaticReconnect()
65+
.Build();
66+
67+
//Subscribe and register call back for the methods that you want to listen to
68+
hubConnection.On<PlotDataItem[]>("ReceiveSamples", (samples) => InitializeDataReception(samples)
69+
70+
);
71+
72+
await hubConnection.StartAsync();
73+
}
74+
```
75+
- **Step 6** Invoke any Hub method to pass back any data to the server.
76+
```
77+
private async Task Send()
78+
{
79+
if (hubConnection is not null)
80+
{//Invoke signalR hub method to configure plot sample rate
81+
await hubConnection.SendAsync("SetSamplesRate", SamplePerTI, TimeIntervalInMS);
82+
}
83+
}
84+
```

0 commit comments

Comments
 (0)