Live Chat Icon For mobile
Live Chat Icon

How do I push data and update the UI from the server to the browser in Blazor?

Platform: Blazor| Category: General

Calling the StateHasChanged() method in Blazor, you can be notified that the state has been changed and re-render the components to push their changes to the browser using SignalR. The following example demonstrates how the Counter component is pushing the increment count data automatically using the Timer function and updating the UI in the browser by calling the StateHasChanged() method in the InvokeAsync action.


@page "/counter"
@using System.Timers

<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private int currentCount = 0;
    private Timer time;

    private void IncrementCount()

    protected override void OnAfterRender(bool firstRender)
        if (firstRender)
            time = new Timer();
            //Set the time interval.
            time.Interval = 1000;
            time.Elapsed += OnTimeInterval;
            time.AutoReset = true;
            // Start the timer.
            time.Enabled = true;

    Private async void OnTimeInterval(object sender, ElapsedEventArgs e)
        await InvokeAsync(() => StateHasChanged());

    public void Dispose()
        // While navigating to other components, Dispose method will be called and clean up the Timer function.

Refer to this link for more details.

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.