dockerasp.net-corenginxblazorsignalr

SignalR Subscription Not Working In Containerized Blazor WASM App on NginX


I am building a Blazor Web Assembly app that uses SignalR that reaches out to a hub that lives in a docker container. When I run Blazor locally everything works fine, even when the hub is in the container. However, when I containerize the Blazor app with docker and run with nginx it doesn't work. I've tried having blazor reach out to the dub container directly and through a reverse proxy using nginx.conf to no avail. Here's what I know:

  1. The containerized Blazor app is successfully linking up to the hub and communication is flowing back and forth between the two. I know this be I can see the traffic on the websocket in the network tab of the dev tools in the browser. The traffic is the same traffic that I see when I run Blazor locally. The subscription just isn't getting hit. It's like I have no subscription at all.

Here's my code in the hub and the Blazor app along with my nginx.conf

Hub

    await foreach (var stream in _accessor.ChatAsync(messages, model))
    {
        responseStreams.Add(stream);
        await Clients.Caller.SendAsync("ReceiveMessage", "test usernam", stream);
    }

Blazor

    hubConnection = hubAccessor.GetHubConnection();
    hubConnection.On<string, ChatResponseStream>("ReceiveMessage", (user, chunk) =>
    {
        ProcessChunk(chunk);
        InvokeAsync(StateHasChanged);
    });

    await hubConnection.StartAsync();

nginx.conf

    events {}
    http {
        include /etc/nginx/mime.types;
        server {
            listen 7070;
            server_name localhost;
            root /usr/share/nginx/html;
            index index.html;
            location / {
                try_files $uri $uri/ /index.html;
            }
            location /chathub {
                proxy_http_version 1.1;

                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";

                proxy_pass "http://chathub:8080/chathub";
            }
        }
    }

Any help would be greatly appreciated as I have been dealing with this for several days now.

Edit Adding how I'm newing up my hub connection. This includes setting up logging that was recommended in a comment.

var hubConnection = new HubConnectionBuilder()
    .WithUrl("http://localhost:7070/chathub")
    .ConfigureLogging(logging =>
    {
        logging.SetMinimumLevel(LogLevel.Debug);
    })
    .Build();

Solution

  • For some reason the SignalR didn't deserialize the object I was passing from the hub to my Blazor app properly, so my subscription didn't recognize it. I'm not sure why this only happens when I run Blazor in a container, but that's what I found. It could be related to the fact that the object had the JsonPropertyName attribute on all of its properties, but I'm not really sure, but this object was from a third party library, so I don't have any control over it. I ended up using Newtonsoft to manually serialize the object on the hub and then manually deserialize it on Blazor. That way the hub and client are only dealing with a string. This fixed my problem.