Why is my hubconnection on but the method is not being fired?

Jay Boy 1 Reputation point
2021-10-28T10:41:44.517+00:00

Hi guys, I've set up signalr in my blazor server side application and for some reason this hubconnection is not being triggered, when the hubconnection is on, it completely ignores the BroadcastData method and doesnt even fire it:

hubConnection.On<ClientDTO>("BroadcastData", BroadcastData);

private void BroadcastData(ClientDTO payload)
{
dashboardData = payload;
StateHasChanged();
}

I have everything setup for this to be "working" but clearly it isn't working and I'm completely lost at what could be the problem... Please take a look at what I have so far and see if you can see what's going on:

Startup:

public Startup(IConfiguration configuration)
{
Configuration = configuration;
StartTimer();
}

private void StartTimer()
{
_timer = new System.Timers.Timer();
_timer.Interval = 5000;
_timer.Elapsed += TimerElapsed;
_timer.Start();
}

private void TimerElapsed(Object source, ElapsedEventArgs e)
{
Trigger();
}

public void Trigger()
{
try
{
using (HttpClient client = new HttpClient())
{
//Trigger on elapsed
var response = client.GetAsync(Configuration.GetConnectionString("ApiTriggerURL")).Result;
}
}
catch
{
Console.WriteLine("something terrible has happened...");
}
}

services.AddScoped(typeof(SignalRHub));
services.AddScoped<IHub, SignalRHub>();
services.AddScoped<HttpClient>();
services.AddSignalR();

        services.AddResponseCompression(opts =>
        {
            opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
                new[] { "application/octet-stream" });
        });

public void Configure(IApplicationBuilde app, IWebHostEnvironment env)
{
app.UseResponseCompression();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
endpoints.MapHub<SignalRHub>(SignalRHub.HubUrl);
});
}

appsettings.json: (fyi, the trigger is working, the api endpoint is being hit as it returns a status 200 ok result)

"ConnectionStrings":
{
"ApiTriggerURL": "http://localhost:5000/api/SignalRHub/GetMyData"
}

Then we have my api controller: (here you can see the status 200 ok)

private readonly SignalRHub _signalRHub;

    public SignalRHubController(SignalRHub signalRHub)
    {
        _signalRHub = signalRHub;
    }

[HttpGet]
public ObjectResult GetMyData()
{
try
{
Task.WhenAll(_signalRHub.BroadcastData()); // Call hub broadcast method
return this.StatusCode((int)HttpStatusCode.OK, "trigger has been triggered");
}
catch
{
return this.StatusCode((int)HttpStatusCode.InternalServerError, "christ, the ting is broken fam");
}
}

When we look into the _signalRHub.BroadcastData(), we see this:

public class SignalRHub : Hub, IHub
{
private readonly ClientService _clientService;
readonly IHubContext<SignalRHub> _hubContext;

public const string HubUrl = "/chathub"; //this is being read in the startup in the endpoints

public SignalRHub(ClientService clientService, IHubContext<SignalRHub> hubContext)
    {
        _clientService = clientService;
        _hubContext = hubContext;

    }

public async Task BroadcastData()
{
var data = _clientService .GetDataAsync().Result;
await _hubContext.Clients.All.SendAsync("BroadcastData", data); //send data to all clients
}
}

And this in turn should basically do this signalrhub every x seconds (depending on timer) I know my code is a whole load of madness, but please look pass this and help me to understand why this isn't working! Thank you in advance!

If you guys want to see this a bit clearer, please go to this link:
https://stackoverflow.com/questions/69738673/why-is-my-hubconnection-on-but-the-method-is-not-being-fired

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,664 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,593 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.