Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is the SignalR hub constructor called?

I'm trying to debug a SignalR hub and noticed that the constructor is getting called multiple times, even with a single client. Is this the expected behaviour? I was expecting the constructor to be called only once for class initialisation, but I'm hitting my breakpoint multiple times.

like image 465
Tim Avatar asked Jul 28 '13 22:07

Tim


People also ask

How does SignalR hub work?

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.


2 Answers

In SignalR Hub instance will be created per each request. So it does't matter if there is only one client or more. In fact Hub is an abstraction over PersistentConnection, if you want more precise control over the things happening behind the scene you can use PersistentConnection. Check here : https://github.com/SignalR/SignalR/wiki/PersistentConnection

like image 51
Incognito Avatar answered Nov 15 '22 13:11

Incognito


As Incognito correctly pointed out, SignalR creates a new instance of the Hub for every request. You should use static members to store information you want in the Hub for all the requests. If you wish to do some operations every time a new client connects to the hub, you should put that code in the OnConnected.

like image 39
Abhishek Nanda Avatar answered Nov 15 '22 13:11

Abhishek Nanda