74

I want to call specific client from server, and not broadcast to all of them. Problem is that I'm in scope of some AJAX request (in .aspx codebehind let say), and not in Hub or PersistentConnection, so don't have Clients property - and client who made that ajax (jquery) call is not the client I want to send signalr message!

Now, I have one hub that it's called on JS page load, which registers new client into server static list, so I have client Guids. But don't know how to use that to send message from server to specific client.

Hrvoje Hudo
  • 8,914
  • 5
  • 29
  • 42

5 Answers5

49

See the docs for the latest:

Persistent connections - https://github.com/SignalR/SignalR/wiki/PersistentConnection

Hubs - http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server

Renzo
  • 355
  • 3
  • 11
davidfowl
  • 34,606
  • 7
  • 88
  • 96
  • Thanx! That was too easy:) clientId is Context.ClientId in Hub? Does ClientID change in every client request, Ayende mentioned something about it in his SR review, or I can rely on that as constant for every client? – Hrvoje Hudo Oct 24 '11 at 08:21
  • 3
    "Client = connection" so every request will give you a new client id. In the next version of SignalR we'll let your override the logic that generates the client id so your can associate it with something more relevant. Right now, you should associate a client id to something relevant to your application (like user name). – davidfowl Oct 24 '11 at 10:12
  • So every time page renders (when I start the hub) , I have to update my list of users with new clientId? – Hrvoje Hudo Oct 24 '11 at 12:17
  • Yes. In signalr 0.3.4 you can take over client Id generation if you want to change the meaning of client id to be something else. – davidfowl Oct 29 '11 at 07:44
  • 4
    For those not interested in digging through the wiki (which I highly recommend you do - it's awesome), it seems that connection.BroadCast(clientId, messageHere) has been replaced simply by Send(clientId, data). – nillls Feb 29 '12 at 19:10
  • 21
    Does anyone have a sample of setting the client id rather than using the auto generated? – Mr Zorn Mar 08 '12 at 05:16
  • David, I updated your answer so that it works with the latest version of SignalR. – Judah Gabriel Himango Apr 04 '12 at 21:20
  • 31
    IMO if you're just going to provide links to documentation, then it should just be a comment on the question. – AaronLS Apr 17 '15 at 22:44
  • 3
    This question is currently being discussed on [meta](http://meta.stackoverflow.com/q/308640/4639281) –  Oct 22 '15 at 22:45
  • 3
    Can you please fold the information in the comments back into the answer davidfowl? Imagine the links went dead (they moved, github died, asp.net went defunct, etc). – Yakk - Adam Nevraumont Oct 23 '15 at 14:08
  • Hey, what's the .net core version of this solution? – Ensar Turkoglu May 29 '20 at 10:12
  • Same solution different API – davidfowl May 30 '20 at 05:59
27
$('#sendmessage').click(function () {
    // Call the Send method on the hub. 
    chat.server.send($('#displayname').val(), $('#message').val(), $.connection.hub.id);
    // Clear text box and reset focus for next comment. 
    $('#message').val('').focus();
});

at server side send the id of the client and response to that id

  public void Send ( string name , string message , string connID )
  {
        Clients.Client(connID).broadcastMessage(name , message);
  }
Krishnraj Rana
  • 6,336
  • 2
  • 28
  • 36
user2201030
  • 279
  • 3
  • 2
  • 6
    $.connection.hub.id is `undefined`? – Bruno Aug 21 '15 at 02:25
  • 2
    I think `Clients.Client(connID)` in the `Send()` returns the same client which actually calls the `Send` function to begin with.. which means the `Send` function will actually broadcast message to the same client. What is the point? – Nawaz Jan 27 '16 at 18:12
  • @Nawaz They're passing in connID, actually, so that's the target. One should not pass in implementation details, though, nor expose them externally in the first place. A user id should be passed in and perhaps a static Dictionary of user ids to client proxies used to map them. – Chris Bordeman Aug 25 '19 at 03:15
  • is it safe to pass the connection Id from the server to the client or vice versa kind of feels like a vulnrability to me? – Max Carroll Jul 28 '20 at 18:15
  • what happens when we send message to receiver that earlier closed connection? How to deal with such scenarios? – Piotrek Mar 22 '21 at 19:36
13

Every time you send a request to the hub server, your request will have a different connection id, so, I added a static hash table that contains a username- which is not changing continuously, and a connection id fro the signal r,every time you connect, the connection id will be updated

 $.connection.hub.start().done(function () {
   chat.server.registerConId($('#displayname').val());
 });

and in the server code:

public class ChatHub : Hub
{
    private static Hashtable htUsers_ConIds = new Hashtable(20);
    public void registerConId(string userID)
    {
        if(htUsers_ConIds.ContainsKey(userID))
            htUsers_ConIds[userID] = Context.ConnectionId;
        else
            htUsers_ConIds.Add(userID, Context.ConnectionId);
    }
}
Jan Hansen
  • 291
  • 2
  • 11
Nada N. Hantouli
  • 1,150
  • 1
  • 11
  • 20
3

when you want to send a message to specific id

 Clients.Client(Context.ConnectionId).onMessage(
               new Message{From = e.Message.From, Body = e.Message.Body}
               );
bilal
  • 590
  • 7
  • 26
1

If the specific user actually is the caller it self, you can use:

Clients.Caller.myJavaScriptClientFunction();
radbyx
  • 9,002
  • 18
  • 79
  • 121