Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending message from server to specific client is not working using SignalR 2 and MVC 4.0

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.

like image 423
imdadhusen Avatar asked Dec 30 '14 09:12

imdadhusen


1 Answers

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()
like image 193
Majid ALSarra Avatar answered Oct 11 '22 14:10

Majid ALSarra