Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR and HttpContext/Session

I understand why SignalR doesn't give you access to the HttpContext. However, this is quite problematic for us. Let me explain:

Our application is a Multi-Tenant application where the user chooses the environment while logging in. This basically registers the ConnectionStringName in the HttpSession. In our SignalR Hub, we need to access the database on Disconnect. But this is not possible because we have no HttpContext at this point and cannot determine the environment to write to.

Can anyone provide us with a suggestion how to solve this problem? We're a bit stuck on this one.

EDIT: Bonus point if your solution works in a Load-Balanced environment.

like image 232
Lodewijk Avatar asked Sep 18 '12 08:09

Lodewijk


People also ask

Does SignalR require sticky session?

Sticky sessions, also known as client affinity, is not required, because clients are immediately redirected to the Azure SignalR Service when they connect.

When should I use SignalR?

SignalR can be used to add any sort of "real-time" web functionality to your ASP.NET application. While chat is often used as an example, you can do a whole lot more. Any time a user refreshes a web page to see new data, or the page implements long polling to retrieve new data, it is a candidate for using SignalR.

Does SignalR keep connection alive?

You can handle this event if you want your application to take some action when a transport connection is lost. The default keepalive timeout period is currently 20 seconds. If your client code tries to call a Hub method while SignalR is in reconnecting mode, SignalR will try to send the command.


2 Answers

This is an old question, but I'm leaving my answer just in case it is helpful to anyone out there.

Since your hub extends Microsoft.AspNet.SignalR.Hub it has access to the Context property of type HubCallerContext

This property exposes a lot of information from the caller:

  • ConnectionId
  • Headers
  • QueryString
  • Request
  • Cookies
  • User

In my solution I use the username stored in Context.User.Identity.Name as a key in my key/value store (Redis in my case) to keep track of all the connections a user has.

You can override OnConnnect and OnDisconnect to maintain the list of connections associated to the user. You can also store anything else you want along with the connections ids (your user connection strings, in your case).

like image 119
Raciel R. Avatar answered Sep 21 '22 21:09

Raciel R.


Hi had a similar problem as I needed to identify non-authenticated visitors to my application to personalise their requests to the SignalR hub.

I have solved it by accessing "HttpContext.Current.Request.AnonymousId". The AnonymousId maps to a temporary record in a self-implemented Session-entity in a SQL database - essentially emulating a database-backed session.

Here some relevant documentation in case you wish to customize the AnonymousId, or initialise the database-entry: http://msdn.microsoft.com/en-us/library/system.web.httprequest.anonymousid.aspx

Moreover, you should be able to access the Context in OnDisconnected() like this: Context.Request.GetHttpContext().

I hope this helps.

like image 38
JDR Avatar answered Sep 19 '22 21:09

JDR