mrb398
mrb398

Reputation: 1317

Working with SignalR, WebApi, and Services

I'm new to SignalR and have some questions about using it correctly in certain scenarios

For my application I'm using signalR to communicate to a client the status of an operation they submitted, while they wait for the final response. My code is structured as such:

  1. User click a button in UI
  2. A connection established between the client and the hub
  3. A call is made to our Web Api
  4. The Web Api makes a call to logic on our service layer
  5. The service layer processes data through a foreach.
  6. For each iteration, the hub send out a signal containing data update a progress bar in the UI

I've been able to achieve all this. But I had to setup a new type using GetHubContext to be able to call my signal method, because I can't work with the Hub object directly. Doing this doesn't allow me to send the signal a specific user because it's not known to the service layer, I'm only able to broadcast to all.

What pattern should I be using to achieve this? For my current setup it seems I would need to setup incoming connections with a group equal to their connectionId, communicate the connectionId to the various layers, and send a signal to that specific "group" as needed. Is this the best setup?

Upvotes: 1

Views: 304

Answers (1)

Juan G Carmona
Juan G Carmona

Reputation: 2208

I'd say that the answer is yes, that seems to be the best setup. Please, read the answer to this question, I'd say this will help you with an example.

SignalR - Sending a message to a specific user using (IUserIdProvider) *NEW 2.0.0*

[Edit]

The key point is the mapping between the connectionId and the users. In your case it will depend on your implementation, I can't tell you the best way to handle it...

Also, a very good answer there talks about SendToUser tests here:

https://github.com/SignalR/SignalR/blob/release/tests/Microsoft.AspNet.SignalR.FunctionalTests/Server/Hubs/HubFacts.cs

It lookjs like something you could use in your solution. [Edit]

I hope it helps.

Upvotes: 1

Related Questions