0
votes

I'm working on a prototype which uses SignalR for broadcasting messages from the server to clients. I also communicate from clients to the server. This is possible via 2 ways: via controllers or via a method call in the SignalR Hub.

Now my question:
Why would I choose either SignalR Hub Methods or the ASP.NET API Controllers as endpoints for client commands to call? (so not for events / broadcasting messages)
And to follow up: Is it recommended to use one or both?

I read that it's not recommended to call the Hub methods from the API Controllers, because they're for the clients to call, not for the server.

Both API Controller and SignalR Hub use HTTP.

I've used:

  • ASP.NET Core SignalR 1.1.0
  • Latest .NET Core version

And following link for SignalR information:
https://docs.microsoft.com/en-us/aspnet/core/signalr/introduction?view=aspnetcore-5.0

1

1 Answers

0
votes

SignalR uses HTTP to establish connection with the hub (negotiation and handshake), then after connection established, it uses one of the following protocol: websockets, server sent events, long polling and forever frame. You can read more about it here.

You want to use SignalR to stream data to the client when it needs to be streamed. Most simple example would be refresh a status of something. Imagine you need to ask the server if the document was already loaded. You would call the endpoint n# of times until you get the desired answer. With the hub, the server will call the client via SignalR that the load was completed. As you can see you will save resources and drastically lower the amount of requests you make to the server.

So what you use? Hug or Controller? It really depends for the specific operation you want to do, not the general case of broadcast data.