How to notify a particular user to trigger a client side function using SignalR?

Viewed 103

I am working with a section of code in which I will be showing a popup(html) in my application to the logged in user whenever a message is received in his or her whatsapp account that is linked with my application. The backend portion is done with the help of an official whatsapp api and sql dependency. So whenever the onChange function of sql dependency injected method is triggered, I want to show a pop up in the client side.

I am using SignalR for this in the following way.

Hub : 

public class WhatsappRcvdMessageHub : Hub
    {
        #region Variables
        public static Dictionary<string, bool> dicConeectionId = new Dictionary<string, bool>();
        #endregion

        public override Task OnConnected()
        {
            dicConeectionId.Add(Context.ConnectionId, true);
            return base.OnConnected();
        }

        public override Task OnDisconnected(bool stopCalled)
        {

            if (dicConeectionId.ContainsKey(Context.ConnectionId))
                dicConeectionId.Remove(Context.ConnectionId);
            return base.OnDisconnected(stopCalled);

        }

        public override Task OnReconnected()
        {
            if (!dicConeectionId.ContainsKey(Context.ConnectionId))
                dicConeectionId.Add(Context.ConnectionId, true);
            return base.OnReconnected();
        }

        public void ShowPopupForRcvdMessages()
        {

        }
    }

This is how I have referred the hub connection in JS

var whatsappConnection = $.connection.whatsappRcvdMessageHub;
whatsappConnection.client.ShowPopupWithCandidate = function ()
{
    //   Code to display the pop up         
}

I have called the JS function in a server side class like the following.

 var context = GlobalHost.ConnectionManager.GetHubContext<WhatsappRcvdMessageHub>();
 context.Clients.All.ShowPopupWithCandidate();

Here I have used clients.all but I only want it to be notified to current logged in user (Say, CurrentUser). I have tried using clients.user(CurrentUser) but then the JS function will not trigger.

Can anyone tell me how to change the client side so that the execution will work properly?

0 Answers
Related