Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SignalR is Hub.Context thread safe?

If there is more than one request occurring from different clients simultaneously then the value of Hub.Context.ConnectionId changes during the execution of the handler.

Say I've got 2 clients connected with client Ids A and B, and I've got a method on my Hub called foo(). I send a request from A to the Server invoking foo(), then whilst the request from A is being processed I send a request from B invoking foo(). At the start of the processing of A's request Hub.Context.ConnectionId == A but at the end of the method call Hub.Context.ConnectionId == B.

Should I be copying the Hub.Context? At what point should I do this?

like image 981
Daniel James Bryars Avatar asked Apr 16 '12 19:04

Daniel James Bryars


People also ask

What is Hub in SignalR?

What is a SignalR hub. The SignalR Hubs API enables you to call methods on connected clients from the server. In the server code, you define methods that are called by client. In the client code, you define methods that are called from the server.

Does SignalR guarantee delivery?

SignalR doesn't guarantee message delivery. Since SignalR doesn't block when you call client methods, you can invoke client methods very quickly as you've discovered.


1 Answers

It doesn't need to be thread safe since Hub instances aren't static so you don't need to copy anything.

They are created per call. So each call from the client will create a new Hub instance and HubContext.

like image 123
davidfowl Avatar answered Sep 17 '22 18:09

davidfowl