user3173098
user3173098

Reputation: 109

SignalR - invoking a message from server code

Been researching heaps and making no progress :( Trying to set up a small web app using VB in VS 2013. I have added all the SignalR resources to my project.

Each logged in client has a UserID in my database.

I want to invoke a SignalR message to certain UserID's from server-side code, not client-side. Firstly, is this possible? (it was possible using ReverseAJAX, but I have chosen not to use that)

If it is possible, how do I go about setting up the SignalR Hub to allow me to send a message using a UserID? I don't need help with the SQL, I can do that my self.

Also, what javascript do I need to persist the request? I'm guessing I will need the UserID somewhere in this piece of code.

Thanks heaps.

Upvotes: 0

Views: 300

Answers (2)

Tobias
Tobias

Reputation: 2840

As @Pepto mentioned, here it is described how you can get a reference to your hub, and then use it in your server code.

An easy way to invoke a client-side function for a specific user, would be to use Clients.User("Username") in your hub.

Intellisense will tell you that User() wants an ID as a parameter, but you should pass the username of the user, whose function you want to invoke, not his ID.

Upvotes: 1

Pepto
Pepto

Reputation: 1444

This is very possible. If you look here

http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-server#callfromoutsidehub

you will find a section on "Single User Groups". This is the mechanism available in version 2 to send a message to a single User Id. Essentially when a Connection is established you add the User Id to a "group" which can be accessed by Group Name (syn. User Id) without having to worry about persisting the relationship of a connection id to a user id yourself. The only limitation would be if more than 1 connection is established by the same user, all devices with that user would be in the same group and would therefore receive any messages sent to it...

There is also another section on how to retain the instance of your SignalR context on the web server to make calls out to clients (How to call client methods and manage groups from outside the Hub class)

I did read something about the SignalR team creating User Id methods but I have used the above approach with fair success and haven't looked much further into that.

Hope this helps

Upvotes: 1

Related Questions