Skip to content

How to make Blazor Server SignalR connection use a different URL? #59546

Open
@pablosantosluaces

Description

@pablosantosluaces

My question is: how to configure the endpoint used by signalr in Blazor server?

Scenario

  • I have a Blazor server app running on Azure Container Apps.
  • The app implements 2 things: a REST API and the website itself using Blazor Server.
  • I configured APIM with the custom domain defined there, and the correct SSL Cert.
  • Then I had to configure an API for the REST API (obvious).
  • But then I configured "other" API to send all website traffic too, otherwise the website wouldn't work. I guess this is not the normal way to use APIM but since we only have 1 certificate and we need it to work for the API and the website, I did it there.
  • Everything was working except the websockets connection. Each time I visit our site, I get this:

Error: Failed to start the transport 'WebSockets': Error: WebSocket failed to connect. The connection could not be found on the server, either the endpoint may not be a SignalR endpoint, the connection ID is not present on the server, or there is a proxy blocking WebSockets. If you have multiple servers check that sticky sessions are enabled.

So, probably the solution is to define an extra WebSocket API in APIM and make the SignalR code go through it.

But, in order to achieve it, I need the javascript side of things to point to something like MYSITE/signalr/_blazor?id=xxx intead of wss://MYSITE/_blazor?id=xxxx

How can I achieve this?

There are some random answers here and there, and "probably" this is the way to achieve it:

<script src="~/_framework/blazor.server.js" autostart="false"></script>
<script>
Blazor.start({
        configureSignalR: function (builder) {
            builder.withUrl("/_blazor", {
                
            });
        }
    });
</script>

But it would be good to be able to find a proper example instead of stackoverflowing this.

@javiercn :-)

Extra info

I found some info about how to do it but the docu is not very clear here: https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/startup?view=aspnetcore-9.0 it mentions something, but where is the detailed info of each option?

It does mention this, but where are all the circuit options??

<script src="{BLAZOR SCRIPT}" autostart="false"></script>
<script>
  ...
  Blazor.start({
    ssr: {
      ...
    },
    circuit: {
      ...
    },
    webAssembly: {
      ...
    }
  });
  ...
</script>

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsThis issue tracks updating documentationarea-blazorIncludes: Blazor, Razor Components

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions