2

Hi I am trying to set up signalR to distribute messages to web users the problem is that the one site holds different 'areas' and also 'user types'.

The Area is decided when the user visits the site by passing a url parameter, this is then stored in a session variable, The 'user type' is by default not logged in then changed when the user logs in to there user type (returned from a webservice) and stored in a User object.

My aim was to connect to the hub using signalR when the user visits the site and add the users connectionId to a groupname, using the 'area id' from the session and the 'user type id' from the User object in the context.

The problem I am having is that I can obtain the user id but when it comes to the area id signalR cannot access the session. I understand the reasoning behind not allowing the session available in signalR but does any one know if there is a way to enable this or an alternative way that I can gain access to the users area so that I can create these unique groups.

6
  • do you know the users area on the javascript side? Nov 22, 2012 at 10:55
  • I do the first time they visit the site as this will be passed in the url but after it is saved in the session I get it from there
    – jclarkson
    Nov 22, 2012 at 11:11
  • Sure; So if you have it on the javascript page, view the answer below, you just need to call a function in the start completed callback and add them to the group. Nov 22, 2012 at 11:14
  • How would I capture the scenario where they close the browser down and reopen navigating to the page without the url parameter but there session is still alive? The server would know what area they are still but the signalR hub would not. I think I may have had a lightbulb moment could I pass this area back from the server to the javascript prior to the hub connection starting?
    – jclarkson
    Nov 22, 2012 at 11:26
  • yup, you could always place a variable on page load to the javascript using the register startup block and thus the area would always be on the client. Nov 22, 2012 at 11:28

1 Answer 1

1

If you do know the users area you can simply call a function from the client once you have connected to add them to that group:

In your page load:

ClientScript.RegisterClientScriptBlock(this.GetType(), "areaKey", "var theAreaTheyBelongTo= '" + serverSideAreaTheyBelongTo + "'", true);

On your hub:

public void SubscribeToArea(string area)
{
    Groups.Add(Context.ConnectionId, area);
}

On your client:

$.connection.hub.start(function () {
    yourHubName.subscribeToArea(theAreaTheyBelongTo);
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.