77

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,994
  • 5
  • 33
  • 42

4 Answers4

29
$('#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,516
  • 2
  • 29
  • 36
user2201030
  • 299
  • 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
17

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
  • 293
  • 2
  • 12
Nada N. Hantouli
  • 1,310
  • 1
  • 12
  • 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
  • 648
  • 8
  • 26
1

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

Clients.Caller.myJavaScriptClientFunction();
radbyx
  • 9,352
  • 21
  • 84
  • 127