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

SignalR takes a long time to respond to a heartbeat


I created a very simple ASP.NET Core app with SignalR with Visual Studio using a Web App MVC application template with the following customization:

  • added a reference to @microsoft/signalr library via libman,
  • referenced <script src="~/lib/microsoft-signalr/signalr.min.js"></script> in _Layout.cshtml,
  • added the required SignalR services in Startup.cs and created an empty Hub, exposed in the following way:
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<MyHub>("hub/remote");
                // ... MVC router definitions
            }
  • created the SignalR connection in JS:
const connection =
    new signalR.HubConnectionBuilder()
        .withUrl("/hub/remote")
        .configureLogging(signalR.LogLevel.Trace)
        .withAutomaticReconnect()
        .build();

connection.start().then(() => console.log("Connected."));

Then I launched the MVC app and everything started without an error. However, I took a closer look at SignalR log messages:

signalr-logs

I believe this is the SignalR's internal heartbeat that keeps the connection alive. I wonder why does it take 4-5s between sending the message and receiving the response?

I also tried using SignalR in a more complex application and from time to time I even started receiving "Reconnecting" events, as the load was significantly larger there.

That makes me feel that I do something wrong while configuring the connection, but no idea what exactly.


Solution

  • "Connection Slow" isn't an event in ASP.NET Core SignalR.

    The heartbeats are not directly related to each other, so the gaps between client and server pings are normal.