3

I am using SignalR in a Blazor server-side app. I added the Microsoft.AspNetCore.SignalR.Client Nuget package (v5.0.11) to the project, and used the following code to create the hub connection...

HubConnection hubConnection = new HubConnectionBuilder()
  .WithUrl("url")
  .Build();
await hubConnection.StartAsync();

I then send out a message with the following code (Debug.WriteLine added to confirm what's going on)...

Debug.WriteLine($"{DateTime.Now.ToLongTimeString()} SignalR msg sent");
await hubConnection.SendAsync("Send", "Hello");

The component that is to handle such messages creates the hub connection and hooks up to the On handler as follows...

HubConnection hubConnection = new HubConnectionBuilder()
  .WithUrl("url")
  .Build();
hubConnection.On<string>("Receive", msg =>
  Debug.WriteLine($"{DateTime.Now.ToLongTimeString()} SignalR msg received - {msg}"));
await hubConnection.StartAsync();

When the message is sent out, it is definitely only being sent once (which I can confirm from the output panel, where I only see the "sent" output once), but it is received twice.

Searching around, it seems that a common reason for this is if jQuery was loaded twice. However, I have checked this, and it's not the case. It's only being loaded the once. Furthermore, one of the other developers on the team tried it, and he got the message received 15 times! Even if we had accidentally included jQuery twice, we certainly didn't include it 15 times. Also, he's using exactly the same code as me (checked out from source control), so we should get the same results if this were the issue.

Anyone any idea why this could be happening? Thanks

jps
  • 20,041
  • 15
  • 75
  • 79
Avrohom Yisroel
  • 8,555
  • 8
  • 50
  • 106
  • Check your disposing of the hubConnection. You may have multiple instances if it constucted on component init and not disposed. – Brian Parker Oct 21 '21 at 19:24
  • @BrianParker Fantastic! That was the problem. Would you like to add that as an answer, and I'll accept it. Thanks very much. – Avrohom Yisroel Oct 21 '21 at 20:10
  • Question: is the `SignalR` hub you are connecting to on the **same server** hosting Blazor Server-side? If so you've created a totally unnecessary setup: a page with a client, _running on the server_, connecting to a SignalR hub _also running on that same server_ ? – Quango Oct 22 '21 at 13:08
  • @Quango Yes they are on the same server, but if I don't create a connection, how do I send out a message to the clients? I thought that was the whole point of SignalR. – Avrohom Yisroel Oct 24 '21 at 13:20
  • @AvrohomYisroel I'll put my answer below so it's clear (plus I can't insert diagrams in comments). – Quango Oct 25 '21 at 09:01

2 Answers2

3

This can occur when you initialise a HubConnection within a component and don’t configure IDispose or IAsyncDispose on your component to dispose the HubConnection.

Brian Parker
  • 11,946
  • 2
  • 31
  • 41
1

In a Blazor Server-Side application there is already a SignalR host pushing updates to the client.

If you want to push updates from a page you can use a Singleton Service to handle the communication.

If you set up your own SignalR hub and use the SignalR client in your components, your application is something like the diagram below: diagram showing SignalR client on Blazor Server

As you can see, the client is actually running on the server. The SignalR hub you've added adds processing and memory overhead and does not add any value.

I created a simple sample app that uses a service that clients listen to for updates: https://github.com/conficient/BlazorServerWithSignalR

Quango
  • 12,338
  • 6
  • 48
  • 83
  • 1
    Thanks for that. I did wonder why I needed to add the SignalR Nuget package when Blazor uses it already, but if you look at the Microsoft docs, that's exactly what they do. Do you know if there is any benefit in using the Nuget package over the way you've shown? – Avrohom Yisroel Oct 31 '21 at 18:36
  • Only reason I'd use SignalR directly on Blazor Server was if I needed to do any JS on the client, or on other clients not using Blazor – Quango Nov 01 '21 at 09:12
  • 1
    Thanks. For me, one of the main benefits of Blazor is that I rarely have to write JS, so I guess I probably don't need the full SignalR. Having said that, SignalR does have some other benefits, such as only sending out to certain subscribers, or to groups, but if I don't need that (which is most of the time), then I guess it's not necessary to use the package. Thanks again. – Avrohom Yisroel Nov 01 '21 at 14:31