Search code examples
asp.net-coresignalrasp.net-core-signalr

SignalR "Error during WebSocket handshake" sometimes


My application is having issues accepting the handshake about half the time in my production environment. On my local machine and in my staging environment, which is the same as production, it works every time.

It is a .Net Core 2.1 app using aspnet-signalr/1.1.4

I am not configuring SignalR to use any specifics in my startup.s

app.UseSignalR(routes => { routes.MapHub<PortfolioHub>("/loadpagedetailsjob"); });

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Dashboard}/{action=Index}/{id?}");
});

my PortfolioHub is just a direct implementation of the Hub class

and in my page

var connection = new signalR.HubConnectionBuilder()
    .withUrl('/loadpagedetailsjob')
    .configureLogging(signalR.LogLevel.Information)
    .build();

connection.start().then(function () {
    connection.invoke("Subscribe");
});

enter image description here

It looks like it tries to negotiate web socket then long polling and both fail. But the requests return 200

enter image description here

Since it is only happening sometimes I am having issues resolving the issue. My only suspicion at this point is since my environment is in AWS behind a load balance that the negotiate requests are routed to different servers which might cause the issue?

Any help is appreciated.


Solution

  • SignalR requires that one connection is always handled by the same server. The typical solution is to configure the load balancer to use sticky sessions. With sticky sessions enabled, the load balancer will route requests of the same user to the same backend server.

    https://learn.microsoft.com/en-us/aspnet/core/signalr/scale?view=aspnetcore-3.1