How SignalR works or to enable in Blazor Server App

Viewed 23

I have just started to learn Blazor and creating a basic Blazor server app. Its documentation says, its runs on the server and establishes the SignalR connection with browser. I am displaying a list of dummy employees in my project from DB using EF Core. Its working fine so far. Then I have updated on record directly from SSMS but its not reflecting in UI unless I explicitly refreshes browser. I am confused, where is the SignalR working here. Ideally it should reflect in browser without refresh if its using SIgnalR.

1 Answers

The SignalR connection is established to allow for you to publish and subscribe to events. On loading of the web page, you've probably got a method which is making the initial event publish, and then you've subscribed to the data being returned from the server and updating the UI.

In order for the data to refresh again, it's not linked automatically in anyway to your backend code or the database. You will need to implement another event publish (e.g. a button click action) which requests the data.

Something like

<button @onclick="@(() => RefreshData())">Refresh</button>

Then have method

@code {
   protected async Task RefreshData()
   {
      myData = await MyService.GetMyDataAsync();
   }
}
Related