[#SIGNALR] HowTo: deliver messages to different groups of customers

Hello!

Do you know SignalR? Not yes!? Right now you must start with SignalR. If you don’t know it, the best is a ride for the main site of SignalR or buy the eBook from the crack of José María Aguilar (link > €5).

As well, I’m learning it to jerks, and until a few hours ago I thought that it had the support of Robert and Benjamin. Lucky for them, both are in Barcelona and I not in Madrid … If both were here … for sure we’ll talk a little!

Not will go into much detail about what is SignalR, just comment that it is a set of libraries that allows two-way communication between server and client, isolating us from technologies such as websockets.

The communications client / server are nothing new, nor is the following scenario:

  • A number of customers subscribing to the reception of a specific type of message
    Literally, SignalR is not like this but this way easier to explain.
  • Optionally, each customer is identified with the server, defining he belongs to a group .
  • The client A sends a X message to all clients
  • The client B, also sends a X message, but not to all customers, only to a particular group

As well, after seeing a code quite complicated for this implementation, of my friends Benjamin and Robert; the following solution is much simpler (like always, we like the simple)

   1: public class MessagesHub : Hub

   2: {

   3:     public void JoinToArea(string area)

   4:     {

   5:         Groups.Add(Context.ConnectionId, area);

   6:     }

   7:  

   8:     public void SendMessage(string message, string area = "")

   9:     {

  10:         if (string.IsNullOrEmpty(area))

  11:         {

  12:             Clients.All.SendMessage(message);

  13:         }

  14:         else

  15:         {

  16:             Clients.Group(area).SendMessage(message);

  17:         }

  18:     }

  19: }

Let’s take a deeper look into this SignalR Hub.

  • The JoinToArea() function should be called from each client connected to the hub to identify himself in a specific area.
  • It adds the client connection Id (through its context) to a group.
  • The group is identical with the area.
  • The SendMessage() function receives a parameter that is the message itself and another optional parameter which is the area.
    • If not be specific an area, the message is sent to all clients (line 12), Clients.All .
    • If you specify an area, customers are filtered the group defined with the name of the area, Clients.Group (area) .

    The truth is that as a solution is very useful, and also only requires few lines of code. Below I leave a step by more complete step explaining work with groups.

    Finally, discuss the scenario where we are working us, is rather more complicated. This approach helps to reduce the complexity of the code, but I think that it will continue to “smelling bad” for my taste.

    Reference: http://www.asp.net/signalr/overview/signalr-20/hubs-api/working-with-groups

    Greetings @ Home

    El Bruno

    imageimageimageGoogle

  • Advertisements

    One thought on “[#SIGNALR] HowTo: deliver messages to different groups of customers

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out /  Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out /  Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out /  Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out /  Change )

    Connecting to %s