3

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);
    }
}
Community
  • 1
  • 1
Etch
  • 3,044
  • 25
  • 31
  • Can you state what you want to do clearly? Do you want to show the changes that made by the user to a specific user? – Sinan AKYAZICI Jan 11 '12 at 09:27
  • @sinanakyazici To reiterate, I need to notify all clients except the caller. There doesnt seem to be a way to loop through the clients to exclude a certain one. You can either call a specific one, or all. – Etch Jan 11 '12 at 14:15

3 Answers3

9

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);
    }
}
Sinan AKYAZICI
  • 3,942
  • 5
  • 35
  • 60
  • Excellent. I didnt think about managing it in the javascript. I also was hoping to use the client id vs. managing my own identifier. However, that works. Thanks! – Etch Jan 11 '12 at 16:25
  • 3
    just for the record you can also use `$.connection.hub.id` instead of your own guid for something trivial like this. – samandmoore Jul 13 '12 at 16:49
  • Only downside to this solution is that if that message being sent to all clients is rather large, then dumping it out on the client side is a waste of bandwidth. In my use, mobile development, that can be a significant loss! – Norman H Feb 06 '13 at 21:52
3

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

Clients.Others.clientSideMethod();
JBeagle
  • 2,630
  • 2
  • 18
  • 20
3

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

Chriseyre2000
  • 2,053
  • 1
  • 15
  • 28
  • Yep, I use this now. But the version at the time of this question didnt have that call. – Etch Feb 13 '13 at 19:44