Search code examples
c#asp.net-mvcasp.net-mvc-4signalrmessaging

How Can I Send Message To specific User with signalR


I have some problem with signalR, I can not send message to specific User From Hub.

I want to do like this:

public void Send(string userToId, string userForId, string message)
{

    IHubContext context = GlobalHost.ConnectionManager.GetHubContext<ChatHubs>();

    //userForId - it is Session["UserId"]
    context.Clients.User(userForId).updateMessages(message);
}

I have already read this topic: http://www.asp.net/signalr/overview/guide-to-the-api/mapping-users-to-connections , but It's not clear for me, because I have not this var name = Context.User.Identity.Name; I have User information in Session Variable and second when I get connectionId like this: var myClientId = $.connection.hub.id; connectionId -is changed when I refresh page or when I click another menu on my project.

I have some questions: 1) Can I send message to specific user without connectionId (use only session["UserId"]) ? 2) In General, How can I make easily one-to-one messaging with SignalR ?

P.S. I'm working ASP.NET MVC (C#)


Solution

  • You can send broadcast message to all Users without connection id. You just need to assign a Unique ID to Every user and Send it as Message parameters.

    SignalR Gives a Unique ID to Every Client as connection ID. Either you can use that connection Id or you can assign unique ID to client while creating client and use it as Connection ID. thats up to you what you want to Use.

    Edit

    Just Update Your Your Method in your Hub Class File.....

         public void Send(string name, string message, string connectionid)
    {
        // Call the addNewMessageToPage method to update clients.
        Clients.All.addNewMessageToPage(name, message, connectionid);
    }
    

    And In Your Client Side You can Update Add Code After including SignalR Files:-

            var chat = $.connection.chatHub;
    
    
                 chat.client.addNewMessageToPage = function (name, message, connectionid) {
                if (connectionid == $('#connection').val()) {
    
                   //   Do What You want Here...
                };
            };
            // Get the user name and store it to prepend to messages.
            $('#displayname').val(prompt('Enter your name:', ''));
            $('#connection').val(prompt('Enter your ID:', ''));
    
            // Set initial focus to message input box.
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    // Call the Send method on the hub.
                    chat.server.send($('#displayname').val(), $('#message').val(), $('#connection').val());
                    // Clear text box and reset focus for next comment.
                    $('#message').val('').focus();
                });
            });
    

    Hope This Helps...