How to Perform Real-Time Updates in Blazor WASM apps with SignalR
Tags: webdev, dotnet, tutorial, devops
Published: 2022-08-26 20:55:02

---
canonical_url: https://www.grapecity.com/blogs/how-to-perform-real-time-updates-in-blazor-wasm-apps-with-signalr
description: Learn how to use a Javascript reporting tool with Ruby on Rails. See more from ActiveReportsJS today.
---

When data is updated on the server, how can we listen for those changes and instantly update our client application?

We will explore that question as we show how to get live or “real-time” updates in a client-side Blazor web application. This topic is ideal for C# developers with little knowledge of ASP.NET MVC.

Blazor WASM (WebAssembly) applications run completely on the client. However, the data is still on the server. We need a way to handle this communication (as illustrated below). For this scenario, Microsoft has provided the SignalR library.

![](https://files.grapecity.com/gc-website-media/idwd2txe/01-signalr.png?width=410&height=359)

### What is SignalR?

[SignalR](https://www.bing.com/ck/a?!&&p=371a42ba2888a2bb9d818a7eba4d511254db28fe5afb01463537abcde2f669b4JmltdHM9MTY1NzgzMjMwOCZpZ3VpZD0zMWQwMjMzNS1mODkwLTQ0YmMtODI0Ny1lODYzNjhkMTQxZjImaW5zaWQ9NTE5Nw&ptn=3&fclid=b5cf84bc-03b7-11ed-948f-aff3ffcc86c0&u=a1aHR0cHM6Ly9kb2NzLm1pY3Jvc29mdC5jb20vZW4tdXMvYXNwbmV0L3NpZ25hbHIvb3ZlcnZpZXcvZ2V0dGluZy1zdGFydGVkL2ludHJvZHVjdGlvbi10by1zaWduYWxy&ntb=1) simplifies the process of adding real-time web functionality to applications. It enables the server to push content to connected clients instantly as it becomes available, rather than having the server wait for a client to request new data. This works through the implementation of a [SignalR Hub](https://docs.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-6.0).

But SignalR is used by ASP.NET MVC developers. If you are developing with Blazor, you may have come from a .NET desktop development background. We help bridge this learning gap with the ComponentOne DataCollection (C1DataCollection) library that now has built-in SignalR communication features. You’ll still need to configure the hub, but C1DataCollection manages the send and receive code.

### What is C1DataCollection?

The [ComponentOne DataCollection](https://www.grapecity.com/componentone/net-data-service-components/data-collection-view) (C1DataCollection) library provides filtering, grouping, and sorting services for your data collection. It makes it easy to enable cursor and paging-based data virtualization for incrementally loading large data sets and displaying real-time updates from SQL Server.

When we consider showing extensive collections of data, it is mandatory to include data virtualization in the discussion; otherwise, we can end up with tons of data being sent and received through the network. This involves sending the data partially to the client app, which brings other consequences like sort or filter operations having to be made on the server, desirably delegating this to SQL Server.

To resolve this scenario, we added a new component to the library named **C1ProxyDataCollection**. This client-side collection will be the shadow of another collection on the server and will receive the data as needed and update when the server-side collection changes. Additionally, the filter and sort will pass through up to the server collection.

To implement this, we will make use of ASP.NET SignalR libraries. The solution involves two new sub-libraries: _C1.DataCollection.SignalR.Client_ and _C1.DataCollection.SignalR.Server_. The first one will contain **C1ProxyDataCollection,** and the second will have **C1DataCollectionHub,** which will serve the data and notify of changes.

![C1DataCollectionHub](https://files.grapecity.com/gc-website-media/3xud0zzj/02-signalr.png?width=250&height=317)

Since the Hub will serve the data of another collection, we will inherit from C1DataCollectionHub and configure the ASP.NET server to inject the collection into the hub.

Next, let’s walk through the steps to create this in a Blazor WASM app.

### Creating a Blazor WASM App with Real-Time Updates

Open Visual Studio and create a Blazor WebAssembly App named "SQLServerRealTimeUpdates". This will give you three projects within the solution: Server, Client, and Shared.

In the shared project, SQLServerRealTimeUpdates.Shared add a new class named "Product.cs".

```
    namespace SQLServerRealTimeUpdates.Shared
    {
        public class Product
        {
            public string Code { get; set; }
            public string Name { get; set; }
            public decimal Price { get; set; }
        }
    }
```

In the server project, SQLServerRealTimeUpdates.Server, perform the following steps:

Add the nuget packages C1.DataCollection.SignalR.Server, Microsoft.Data.SqlClient and SqlTableDependency.

Add a new folder named "Data" and add the SQLServerDataCollection.cs file from [this previous post](https://www.grapecity.com/blogs/data-binding-blazor-flexgrid-to-sql-server-with-real-time-updates).

Add a new folder named "Hubs"

![Hubs](https://files.grapecity.com/gc-website-media/2zybgrl3/03-signalr.png?width=229&height=172)

Create a new file named “ProductsHub.cs" in the Hubs folder.

```
    using C1.DataCollection;
    using C1.DataCollection.SignalR.Server;
    using Microsoft.AspNetCore.SignalR;
    using SQLServerRealTimeUpdates.Data;
    using SQLServerRealTimeUpdates.Shared;

    namespace SQLServerRealTimeUpdates.Server.Hubs
    {
        public class ProductsHub : C1DataCollectionHub<Product>
        {
            private IConfiguration Configuration { get; }

            public ProductsHub(IHubContext<ProductsHub> context, IConfiguration configuration)
                : base(context.Clients)
            {
                Configuration = configuration;
            }

            protected override IDataCollection<Product> GetCollection(HubCallerContext context)
            {
                var sqlConnection = new Microsoft.Data.SqlClient.SqlConnection(Configuration["ConnectionString"]);
                return new SQLServerDataCollection<Product>(sqlConnection, "Products", new ProductComparer());
            }
        }
    }
```

Open the Program.cs file and edit the following lines:

*   Configure JSON serializers to be used by SignalR (After the CreateBuilder line)

```
    using C1.DataCollection.Serialization;
    using System.Text.Json;

    builder.Services.AddSignalR().AddJsonProtocol(options =>
    {
        options.PayloadSerializerOptions = new JsonSerializerOptions
        {
            Converters =
            {
                new FilterExpressionJsonConverter(),
                new SortDescriptionJsonConverter(),
                new NotifyCollectionChangedEventArgsJsonConverter()
            }
        };
    });
```

Map the hub with the URL path

```
    using SQLServerRealTimeUpdates.Server.Hubs;

    app.MapHub<ProductsHub>("/productsHub");
```

In the client project, we will also use the ComponentOne [FlexGrid](https://www.grapecity.com/componentone/flexgrid-net-data-grid-control), a Blazor datagrid control, but you could use any data view control. In the client project, SQLServerRealTimeUpdates.Client, perform the following steps:

Add the NuGet packages C1.DataCollection.SignalR.Client and C1.Blazor.Grid.

Then, add the file "Index.razor" and replace it with the following content:

```
    @page "/"
    @using C1.Blazor.Grid
    @using C1.DataCollection.SignalR.Client
    @using SQLServerRealTimeUpdates.Shared
    @inject NavigationManager NavigationManager

    <FlexGrid ItemsSource="Products" AutoGenerateColumns="false" HeadersVisibility="GridHeadersVisibility.All" NewRowPosition="GridNewRowPosition.Bottom" Style="@("width:100%;")">
        <FlexGridColumns>
            <GridColumn Binding="Code"></GridColumn>
            <GridColumn Binding="Name" Width="GridLength.Star"></GridColumn>
            <GridColumn Binding="Price"></GridColumn>
        </FlexGridColumns>
    </FlexGrid>

    @code {
        C1ProxyDataCollection<Product> Products;

        protected override void OnInitialized()
        {
            var url = NavigationManager.ToAbsoluteUri("productsHub");
            Products = new C1ProxyDataCollection<Product>(url);
        }
    }
```

Here in the client-side WASM application, we created a C1ProxyDataCollection which will be connected to the hub. Since this collection implements standard .NET collection interfaces like IEnumerable and IReadOnly list, this can be used like a normal collection. Still, it also implements common C1DataCollection interfaces that allow sorting and filtering of the collection.

When we bind the collection to our Blazor FlexGrid, the data virtualization will make the application load very quickly. The final user will receive more data as the grid is scrolled. Also, the user will see real-time updates to the data as it changes in the SQL Server instance. Users can even filter and sort the data as they need.

![Blazor WASM](https://files.grapecity.com/gc-website-media/lkwdttj5/04-signalr.gif?width=500&height=203.125)

The C1ProxyDataCollection was added in the ComponentOne 2022 v2 release. In a previous post, [Data Binding Blazor FlexGrid to SQL Server with Real-Time Updates](https://www.grapecity.com/blogs/data-binding-blazor-flexgrid-to-sql-server-with-real-time-updates), we also demonstrated how to perform real-time updates for a Blazor Server application. Blazor Server does not depend upon the new proxy data collection classes to provide SignalR communication.

You can find the ComponentOne Service Components Samples [here](https://github.com/GrapeCity/ComponentOne-Service-Components-Samples/tree/master/DataCollection/Shared/ProxyDataCollection).