Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to filter the receivers in SignalR?

I hit the following problem. I'd like to do the following. When a new client is being connected, group parameter is being sent to the SignalR server's side (in URL or another way). Then I want to notify only clients from the specific group.

e.g.

I have 3 clients:
1) with group parameter = a
2) with group parameter = a
3) with group parameter = b

I want to notify only clients with group parameter == a. If I use dynamic field Clients, it'll send a message for all the clients. Is it possible to filter the receivers somehow?

like image 982
user1011719 Avatar asked Mar 05 '12 13:03

user1011719


1 Answers

If you want to send a message all group members, you need to add client in the group. you can define group name or you can let clients select. For example:

<script src="Scripts/jquery-1.6.4.min.js" type="text/javascript"></script>
<script src="Scripts/jquery.signalR.js" type="text/javascript"></script>
<script src="signalr/hubs" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        var g = $.connection.groups;

        g.send = function (t) {
            $("#groups").append(t);
        };
        $("#btnJoin").click(function () {
            g.addGroup($("#gr").val());
        });
        $("#btnSend").click(function () {
            g.sendMessage("a"); //for example a group.
        });
        $.connection.hub.start();
    });
</script>
<select id="gr">
    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
</select>
<div id="groups"></div>
<input id="btnJoin" type="button" value="Join"/>
<input id="btnSend" type="button" value="Send"/>

 

public class Groups : Hub
{
     public void AddGroup(string groupName)
     {
         GroupManager.AddToGroup(Context.ClientId, groupName);
         Clients.send(Context.ClientId + " join " + groupName + " group.<br />");
     }

    public void SendMessage(string groupName)
    {
        Clients[groupName].send(groupName + " group - Hello Everybody!");            
    }
}
like image 196
sinanakyazici Avatar answered Nov 04 '22 10:11

sinanakyazici