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)
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.
- 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.
Greetings @ Home