3

I have a SignalR client application, written in C#, that subscribe an event from the Hub. When the Event is "fired" the application executes a method.

The question is that all events of that kind, to that client, are processed in sequentially.

Use Case / Flow:

  1. In a Browser, an user clicks a button
  2. The button triggers a call to a SignalR Hub
  3. The SignalR Hub send the message to a specific SignalR client
  4. The SignalR Client receives the event/message
  5. The SignalR Client invoke a method that will store the message in a Database
  6. The SignalR Client invoke a method in the Hub to return the result
  7. The Hub deliver to the user the result

If another user clicks the button at the same time, the SignalR Client will only store one message at a time.

Client Example:

myHub.On<ExecuteRequestModel, string>("Execute", (request, to) =>
{
    logger.Info("Execute request received for connectionID: " + connection.ConnectionId);

    myMethod();

    myHub.Invoke("ExecuteResult", response, to);

    logger.Info("Execute request processed");
    logger.Info("...");
});

There's any recommended practice to handle multiple events in a concurrent way?

Gui Ferreira
  • 4,367
  • 6
  • 28
  • 41
  • Can you elaborate a little more...maybe with an example scenario ? – Hackerman Jan 11 '17 at 17:33
  • Hi @Hackerman. Question updated. Could you take a look? – Gui Ferreira Jan 11 '17 at 17:46
  • Maybe it is possible to use `Task.Run`for it, but I am not sure whether you could send the response back from the different thread. And I am wondering about the design. Why should the web application (Hub) communicate with the database layer by SignalR and don't use a web service or something like that. – H.G. Sandhagen Jan 11 '17 at 17:55
  • Maybe related: http://stackoverflow.com/questions/27485996/how-to-use-async-await-with-hub-on-in-signalr-client – Hackerman Jan 11 '17 at 18:20
  • please see http://stackoverflow.com/questions/10180052/in-signalr-is-hub-context-thread-safe you basically don't need to do almost anything to.get concurrency, you may only worry about your shared state – Oleg Bogdanov Jan 12 '17 at 07:25

1 Answers1

0

That can be solved using Async/Await. The Async function will be responsible to reply to the Hub the result.

myHub.On<ExecuteRequestModel, string>("Execute", async (request, to) => 
    await onExecute(request, to)
);
Gui Ferreira
  • 4,367
  • 6
  • 28
  • 41