Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR - sending parameter to OnConnected?

I have the following JS working:

var chat = $.connection.appHub;

My app has a single hub, AppHub, that handles two types of notifications - Chat and Other. I'm using a single hub because I need access to all connections at all times.

I need to be able to tell OnConnected which type it is via something like the following:

[Authorize]
public class AppHub : Hub {
    private readonly static ConnectionMapping<string> _chatConnections =
        new ConnectionMapping<string>();
    private readonly static ConnectionMapping<string> _navbarConnections =
        new ConnectionMapping<string>();
    public override Task OnConnected(bool isChat) { // here
        string user = Context.User.Identity.Name;
        if (isChat){
            _chatConnections.Add(user, Context.ConnectionId);
            _navbarConnections.Add(user, Context.ConnectionId);
        } else{
            _navbarConnections.Add(user, Context.ConnectionId);
        }  
    }
}

Usage would ideally be something like this:

var chat = $.connection.appHub(true);

How can I pass that parameter to the hub from javascript?

Update:

SendMessage:

 // will have another for OtherMessage
 public void SendChatMessage(string who, ChatMessageViewModel message) {
        message.HtmlContent = _compiler.Transform(message.HtmlContent);
        foreach (var connectionId in _chatConnections.GetConnections(who)) {
            Clients.Client(connectionId).addChatMessage(JsonConvert.SerializeObject(message).SanitizeData());
        }
    }
like image 240
RobVious Avatar asked Dec 21 '13 23:12

RobVious


2 Answers

I would rather add a method to the hub that you call from the client to subscribe to the type. E.g.

public void Subscribe(bool isChat) {
    string user = Context.User.Identity.Name;
    if (isChat){
        _chatConnections.Add(user, Context.ConnectionId);
    } else{
        _otherConnections.Add(user, Context.ConnectionId);
    }
}

You call this method after the hub is connected. It is more flexible in terms that it is then possible to change the notification type without having to reconnect. (Unsubscribe and Subscribe)

Alternative

If you don't want the extra roundtrip/flexibility. You can send QueryString parameters when connecting to the hub. Stackoverflow answer: Signalr persistent connection with query params.

 $.connection.hub.qs = 'isChat=true';

And in OnConnected:

 var isChat = bool.Parse(Context.QueryString["isChat"]);
like image 172
Hallvar Helleseth Avatar answered Nov 16 '22 13:11

Hallvar Helleseth


Hallvar's answer is useful in most cases. But sometimes you could also use headers to send data to the OnConnected method.

Code example for Asp .Net Framework:

var myParameter = HttpContext.Current.Request.Headers["HeaderName"];

For .NET 5+ you may need Dependency Injection to access HttpContext, as shown here

like image 21
Ruben Martirosyan Avatar answered Nov 16 '22 11:11

Ruben Martirosyan