I wanted to call notify specific client from server using signalR but it was not working. my code was executed successfully but client dose not receive any call from server.
However this is working for all client.
var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProcessStatusNotifyHub>();
hubContext.Clients.All.notify("Got it!");
But this is not working for specific client [Updated Code] Following code written in chat.cshtml
$(function () {
// Reference the auto-generated proxy for the hub.
var chat = $.connection.processStatusNotifyHub;//chatHub;
chat.client.notify = function (msg) {
alert(msg);
}
// Start the connection.
$.connection.hub.start().done(function () {
var myClientId = $.connection.hub.id;
console.log('connected: ' + myClientId);
$('#sendmessageToClient').click(function () {
//chat.server.send('imdadhusen', 'This is test text');
$.ajax({
url: '@Url.Action("Send", "PushNotification")',
type: 'POST',
data: { 'clientID': myClientId },
dataType: 'json',
success: function (result) {
alert(result.status);
}
});
});
});
});
Following code is written in Controller
[HttpPost]
public ActionResult Send(string clientID)
{
var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProcessStatusNotifyHub>();
//hubContext.Clients.All.notify("Got it!");
hubContext.Clients.User(clientID).notify("Got it!");
responseResult result = new responseResult();
result.status = "OK";
result.message = "Notification sent successfully";
return Json(result, JsonRequestBehavior.AllowGet);
}
I have tried debug the code it is showing correct value of client id on .cstml or controlloer. e.g. clientid : 0fdf6cad-b9c1-409e-8eb7-0a57c1cfb3be
Could you please help me to send notification to specific client from server.
The operation hubContext.Clients.User(string)
is expecting the User ID of the current HTTP request, not SignalR's client ID.
If you want to use client ID use this operation:
hubContext.Clients.Client( )
If you want to access it by the current user, then you can get current user ID with this
string UserID=User.Identity.GetUserId()
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