Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I update multiple users on Blazor Server-Side?

Suppose I want a UI where users in a group have a synchronized view. Imagine a group-chat application, where everyone in the group should see the exact same messages.

When a user posts a message to the group, the entire group needs to receive it.

With JS, I might use SignalR Groups, and have the front-end generate a SignalR event with a message is posted. The server-side hub would then send that message out to the group.

On Server-Side Blazor, since all the users' states are already on the server, how would I coordinate updating the UI of groups of users on Blazor?

like image 490
kg743 Avatar asked Jan 11 '20 17:01

kg743


People also ask

How many users can Blazor server handle?

We published a GitHub example that shows how to deploy an XAF Blazor application to a Kubernetes cluster with horizontal autoscaling. We tested the application in two types of clusters: locally-run K3s and Azure Kubernetes Service (AKS). The maximum pod replica number (20) allowed around 300 concurrent users.

What is the difference between Blazor server and Blazor WebAssembly?

Blazor Server apps have direct access to server and network resources where the app is executing. Because Blazor WebAssembly and Blazor Hybrid apps execute on a client, they don't have direct access to server and network resources.

How do I use session storage in Blazor server?

First to access browser seesionStorage in Blazor apps, write a custom code or use a third party package. The accessed data can be stored in the localStorage and sessionStorage. The localStorage is scoped to the user's browser. If the user reloads the page or closes and reopens the browser, the state persists.

How does server-side Blazor work?

Server-side Blazor is executed on the server within an ASP.NET Core application. All UI updates, event handling, and JavaScript calls are handled from server by using a SignalR connection, even a button click will go to server.


1 Answers

I was trying to achieve something similar. After a while, I discovered a way of doing this. Kyle's answer helped me figure it out.

You must create a class, let's call Messenger, which has an Action property, and a method that invokes this Action once called

public Action OnMessage { get; set; }
public void AddMessage(string message)
{
  Text = message;
  OnMessage?.Invoke();
}

In the razor component, inject the Messenger. Also, you should set an event to the OnMessage property, and then call StateHasChanged in order to tell the view that it can update

@inject Messenger messenger
// rest of component here, and then the code block 
protected override async Task OnInitializedAsync()
{
  messenger.OnMessage += () =>
  {
    // do something with the messenger.Text here
    InvokeAsync(this.StateHasChanged);
  };
}

Don't forget, on your Startup class, add the class Messenger as a Singleton in order to have it available for injection

services.AddSingleton<Messenger>();

I hope that is what you were trying to achieve, and it can help others as well. Cheers.

like image 108
Evandro Mendonça Avatar answered Sep 28 '22 06:09

Evandro Mendonça