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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With