Skip to content

[Feature request] WebSocketHub is not thread-safe #12

@christallire

Description

@christallire

Hi team,

Currently, calling WebSocketHub's BroadcastTextAsync concurrently throws an "invalid frame" error on client side. It seems the method is not natively thread-safe when multiple threads attempt to write at the same time.

Current Workaround:
To ensure thread safety, I had to implement a lock on the consumer side like this:

public async Task BroadcastEventAsync(string json)
{
await _writeLock.WaitAsync();
try
{
await _hub.BroadcastTextAsync("__all", json);
}
finally
{
_writeLock.Release();
}
}

Proposed Solution
While the workaround functions, acquiring a lock for every broadcast isn't ideal for high-throughput scenarios.

It would be a massive improvement if the library could handle concurrency internally. Specifically, implementing an internal buffering or queuing mechanism to batch and send payloads in a performant, thread-safe manner would be fantastic. This would significantly improve developer experience and out-of-the-box performance.

Thank you for considering this enhancement!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions