Handling high volume of messages in C#

I have a bot subscribed to several EventSub types. Last night two people subscribed at the same time but my bot only processed one of the channel.chat.notification messages. I’m using C# and was wondering whether my processing loop is good enough for high volumes of messages or if I’m creating some sort of bottleneck here. Any input would be greatly appreciated.

My processing loop:

using System.Net.WebSockets

private static ClientWebSocket Socket;

private static async Task SocketProcessingLoopAsync()
{
    var cancellationToken = SocketLoopTokenSource.Token;
    try
    {
        var buffer = WebSocket.CreateClientBuffer(4096, 4096);
        while (Socket.State != WebSocketState.Closed && !cancellationToken.IsCancellationRequested)
        {
            string message = "";
            WebSocketReceiveResult recieveResult = null;
            do
            {
                recieveResult = await Socket.ReceiveAsync(buffer, cancellationToken);
                message += Encoding.UTF8.GetString(buffer.Array, 0, recieveResult.Count);
            }
            while (!recieveResult.EndOfMessage);
            if (!cancellationToken.IsCancellationRequested)
            {
                if (Socket.State == WebSocketState.CloseReceived && recieveResult.MessageType == WebSocketMessageType.Close)
                {
                    await Socket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Acknowledge Close frame", CancellationToken.None);
                }
                if (Socket.State == WebSocketState.Open && recieveResult.MessageType != WebSocketMessageType.Close)
                {
                    MessageQueue.Add(message);
                }
            }
        }
    }
    catch (OperationCanceledException)
    {
        // Error Handling
    }
    catch (Exception ex)
    {
	    // Error Handling
    }
    finally
    {
        // Exit Code
    }
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.