C
C#•13mo ago
Rese

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:
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();
}
6 Replies
Rese
ReseOP•13mo ago
Uploading logs here
qqdev
qqdev•13mo ago
Compare them. Success
Rese
ReseOP•13mo ago
it's the same code for the signalr connection 😄 so I was just thinking it may be to do with the server setup or something? so any other ideas? 😄
qqdev
qqdev•13mo ago
Are the services deployed on different servers? Did you try it locally?
Rese
ReseOP•13mo ago
results are both running locally and there are really not many differences between the two codes, one is intended for use on localhost and the other one runs publicly, so those are really the only differences - the setup for the server so thus my confusion SignalR is set up in the same way so why would it only fuck with its performance
qqdev
qqdev•13mo ago
So the issue does only occur when being deployed?
Want results from more Discord servers?
Add your server