SignalR initial connection takes way too long (logs included in comments)
Hey there.
So I have two versions of the same application, using essentially the same code. One of them, however, has SignalR take around 10 seconds to make a connection to the hub, meanwhile the other ones takes like 2 seconds tops. Why could that be?
Providing the code for connecting below:
So I have two versions of the same application, using essentially the same code. One of them, however, has SignalR take around 10 seconds to make a connection to the hub, meanwhile the other ones takes like 2 seconds tops. Why could that be?
Providing the code for connecting below:
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_hubConnection = _hubService.MakeHubConnection(Constants.HubRoutes.DISPLAY_HUB);
_hubConnection.On<Settings>(Constants.HubRoutes.SETTINGS_UPDATED, async (settings) =>
{
_actionInProgress = true;
await InvokeAsync(StateHasChanged);
_settings = settings;
if (_settings.DefaultBackgroundId is not null)
{
using var db = _dbContextFactory.CreateDbContext();
_settings.DefaultBackground = await db.Images.FirstOrDefaultAsync(x => x.Id.Equals(_settings.DefaultBackgroundId));
}
_actionInProgress = false;
await InvokeAsync(StateHasChanged);
});
_hubConnection.On<Guid, string?, int>(Constants.HubRoutes.NOW_PLAYING_CHANGED, async (instance, _, _) =>
{
if (instance.Equals(_instance) || _queue is null)
return;
_actionInProgress = true;
await InvokeAsync(StateHasChanged);
using var db = _dbContextFactory.CreateDbContext();
await db.ReloadQueue(_queue);
_actionInProgress = false;
await InvokeAsync(StateHasChanged);
});
_ = TrackConnectionAsync(_hubConnection);
}
}
private async Task TrackConnectionAsync(HubConnection hubConnection)
{
await hubConnection.StartAsync();
_isConnected = true;
StateHasChanged();
}protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
_hubConnection = _hubService.MakeHubConnection(Constants.HubRoutes.DISPLAY_HUB);
_hubConnection.On<Settings>(Constants.HubRoutes.SETTINGS_UPDATED, async (settings) =>
{
_actionInProgress = true;
await InvokeAsync(StateHasChanged);
_settings = settings;
if (_settings.DefaultBackgroundId is not null)
{
using var db = _dbContextFactory.CreateDbContext();
_settings.DefaultBackground = await db.Images.FirstOrDefaultAsync(x => x.Id.Equals(_settings.DefaultBackgroundId));
}
_actionInProgress = false;
await InvokeAsync(StateHasChanged);
});
_hubConnection.On<Guid, string?, int>(Constants.HubRoutes.NOW_PLAYING_CHANGED, async (instance, _, _) =>
{
if (instance.Equals(_instance) || _queue is null)
return;
_actionInProgress = true;
await InvokeAsync(StateHasChanged);
using var db = _dbContextFactory.CreateDbContext();
await db.ReloadQueue(_queue);
_actionInProgress = false;
await InvokeAsync(StateHasChanged);
});
_ = TrackConnectionAsync(_hubConnection);
}
}
private async Task TrackConnectionAsync(HubConnection hubConnection)
{
await hubConnection.StartAsync();
_isConnected = true;
StateHasChanged();
}