In the latest version of Asp.Net SignalR, was added a new way of sending a message to a specific user, using the interface "IUserIdProvider".
public interface IUserIdProvider { string GetUserId(IRequest request); } public class MyHub : Hub { public void Send(string userId, string message) { Clients.User(userId).send(message); } }
My question is: How do I know to whom I am sending my message? The explanation of this new method is very superficial. And the draft Statement of SignalR 2.0.0 with this bug and does not compile. Has anyone implemented this feature?
More Info : http://www.asp.net/signalr/overview/signalr-20/hubs-api/mapping-users-to-connections#IUserIdProvider
Hugs.
In SignalR 2.0, this is done by using the inbuilt IPrincipal.Identity.Name , which is the logged in user identifier as set during the ASP.NET authentication. However, you may need to map the connection with the user using a different identifier instead of using the Identity.Name.
When user click on send button, the message to be posted to server side using signalR connection hub. Thus whenever you post any message after clicking the join group button, the message will appear to all the clients who has joined the group.
SignalR the newest member of the ASP.NET family, that enables developers to write incredibly simple real-time, two-way websites and apps. Two-way communication means that both the server and client share an open channel in which they can initiate contact with each other.
SignalR provides ConnectionId for each connection. To find which connection belongs to whom (the user), we need to create a mapping between the connection and the user. This depends on how you identify a user in your application.
In SignalR 2.0, this is done by using the inbuilt IPrincipal.Identity.Name
, which is the logged in user identifier as set during the ASP.NET authentication.
However, you may need to map the connection with the user using a different identifier instead of using the Identity.Name. For this purpose this new provider can be used with your custom implementation for mapping user with the connection.
Lets assume our application uses a userId
to identify each user. Now, we need to send message to a specific user. We have userId
and message
, but SignalR must also know the mapping between our userId and the connection.
To achieve this, first we need to create a new class which implements IUserIdProvider
:
public class CustomUserIdProvider : IUserIdProvider { public string GetUserId(IRequest request) { // your logic to fetch a user identifier goes here. // for example: var userId = MyCustomUserClass.FindUserId(request.User.Identity.Name); return userId.ToString(); } }
The second step is to tell SignalR to use our CustomUserIdProvider
instead of the default implementation. This can be done in the Startup.cs while initializing the hub configuration:
public class Startup { public void Configuration(IAppBuilder app) { var idProvider = new CustomUserIdProvider(); GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => idProvider); // Any connection or hub wire up and configuration should go here app.MapSignalR(); } }
Now, you can send message to a specific user using his userId
as mentioned in the documentation, like:
public class MyHub : Hub { public void Send(string userId, string message) { Clients.User(userId).send(message); } }
Hope this helps.
Here's a start.. Open to suggestions/improvements.
Server
public class ChatHub : Hub { public void SendChatMessage(string who, string message) { string name = Context.User.Identity.Name; Clients.Group(name).addChatMessage(name, message); Clients.Group("[email protected]").addChatMessage(name, message); } public override Task OnConnected() { string name = Context.User.Identity.Name; Groups.Add(Context.ConnectionId, name); return base.OnConnected(); } }
JavaScript
(Notice how addChatMessage
and sendChatMessage
are also methods in the server code above)
$(function () { // Declare a proxy to reference the hub. var chat = $.connection.chatHub; // Create a function that the hub can call to broadcast messages. chat.client.addChatMessage = function (who, message) { // Html encode display name and message. var encodedName = $('<div />').text(who).html(); var encodedMsg = $('<div />').text(message).html(); // Add the message to the page. $('#chat').append('<li><strong>' + encodedName + '</strong>: ' + encodedMsg + '</li>'); }; // Start the connection. $.connection.hub.start().done(function () { $('#sendmessage').click(function () { // Call the Send method on the hub. chat.server.sendChatMessage($('#displayname').val(), $('#message').val()); // Clear text box and reset focus for next comment. $('#message').val('').focus(); }); }); });
Testing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With