Question

I am evaluating SignalR (which happens to be used with Knockoutjs) to see if we can use it to notify clients of concurrency issues. Basically user "a" saves a record and users "b,c,d,e,f,g" are notified. I basically have an example working that notifies all clients. So I think I am almost there.

I came across this link and it lead me on the current path that I am on. I have also been looking at the documentation on Github.

Basically I want to exclude the a single client from the Clients.method() call. I dont see a way to loop through the clients and check the ClientId. The only other I can see to accomplish this is to maybe look at using the groups to keep track of it, but that seemed a little cumbersome, but I was having issues with that as well.

 public class TicketHub : Hub
{
    static int TotalTickets = 10;

    public void GetTicketCount()
    {
        AddToGroup("ticketClients");
        Clients.setTicketCount(TotalTickets);
    }

    public void BuyTicket()
    {
        if (TotalTickets > 0)
            TotalTickets -= 1;

        RemoveFromGroup("ticketClients");

        //  This will call the method ONLY on the calling client
        //  Caller.updateTicketCountWithNotification(TotalTickets);

        // This will call the method on ALL clients in the group
        Clients["ticketClients"].updateTicketCountNotify(TotalTickets);

        AddToGroup("ticketClients");

        Caller.updateTicketCountDontNotify(TotalTickets);
    }
}
Was it helpful?

Solution

javascript code:

<script type="text/javascript">
    $(document).ready(function () {
        var test = $.connection.test;
        $("#btnTest").click(function () {
            test.testMethod();
        });
        test.show = function (text, guid) {
            if (guid != test.guid) //notify all clients except the caller
                alert(text);
        };
        $.connection.hub.start(function () { test.start(); });
    });
</script>

Class :

public class Test : Hub
{
    public void Start()
    {
        Caller.guid = Guid.NewGuid();
    }

    public void TestMethod()
    {
        Clients.show("test", Caller.guid);
    }
}

OTHER TIPS

If you want to exclude the caller from the call to the client side method you can use:

Clients.Others.clientSideMethod();

There is also Clients.AllExcept(...) that allows excluding certain people.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top