I have a Blazor Server application which listens to messages on a message broker and updates the UI with the new messages as they arrive. The gist of the component looks like this below, except of course the data is much more complex.
@page "/"
@implements IDisposable
@inject MessageBroker MessageBroker
<h1>@message?.Data</h1>
@code
{
private Message message;
protected override void OnInitialized()
{
MessageBroker.OnMessage += OnMessage;
}
private async void OnMessage(object sender, Message m)
{
message = m;
await InvokeAsync(StateHasChanged);
}
public void Dispose()
{
MessageBroker.OnMessage -= OnMessage;
}
}
The MessageBroker service is listening to messages over AMQP and invoking the OnMessage event. I find that there can significant delays between the time when the message is received and the UI updates. This is exacerbated when there are numerous clients connected via their browser.
I'm trying to track down the source of these delays and one suspect is the BuildRenderTree method which I understand is called each time StateHasChanged is invoked.
Is there anyway to see how long this method is taking? Or if there are other Blazor methods which might be responsible for the delays?