SignalR SendAsync seems to block code execution when client is busy

Discussão em 'Angular' iniciado por Neran, Março 21, 2025.

  1. Neran

    Neran Guest

    Im am trying to understand what happens with signalR in my example. On my server side i have following methods

    while(clientWantsData)
    {
    byte[] imageData = LoadData(path);
    float start = stopwatch.ElapsedMilliseconds;
    await _signalRHub.SendImage(imageData, token);
    float end = stopwatch.ElapsedMilliseconds;
    Console.WriteLine($"Sending image took {end - start}ms");
    }
    // inside hub
    public async Task SendImage(byte[] imageData, CancellationToken token = default)
    {
    await _hubContext.Clients.All.SendAsync(nameof(SendImage), imageData, token);
    }


    Then on the client im registering a handler for that message:

    this.hub.addListener('SendImage', (data: Uint8Array) => {
    const startTime = performance.now();

    // do some work here

    const readElapsed = performance.now() - startTime;
    this.readTime = readElapsed.toFixed(2);
    });


    The work that is done here is some pre processing on the data. The point is the listener callback is doing some work. Client and server run on the same machine and im using signalr-messagepack.

    What I experience is that it looks like my client code affects code execution on my server side. When the callback on the client side is empty then the server prints around 180ms. Which seems to be the time it takes to transmit the data. Then if i add some pre processing code inside the clients handler the client prints that it takes 70ms to pre process the data. The server now prints around 220ms. However the first time the loop executes the server prints 180ms.

    Im not sure whats happening. Does the server wait until the client responds with a message after being done with the listener handler? But actually 180ms + 70ms does not result in 220ms. So I assume the client is just busy accepting new image data from the server while its executing the handler and the server waits for the client being ready to accept new data. How could I do better?

    Continue reading...

Compartilhe esta Página