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#)
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.....
And In Your Client Side You can Update Add Code After including SignalR Files:-
Hope This Helps...
If you want to send message to specific user in SignalR, easiest way is the use Form authentication. Also you can use your custom session with form authentication. Right after creation your session code put this code.
Then in signalR you can use this line for send message to this user:
EDIT:
For your question pass username to this method (receiver username) and push message to that user.Then you dont need to give a userForId, because you already have sender username with "var username = Context.User.Identity.Name;".Also this method will just push to method that receiver user name. If you want to get also message to sender you need to user "Caller" and you need a write new function to get caller messages in javascript.I hope its clear for you.
This message will go only that username.And my recommendation is use FormAuthentication implementation in your whole project.Thanks.