Search code examples
signalrxsockets.net

Does SignalR really broadcast every message to all connected clients?


We currently evaluating solutions for implementing server-sent events (not neccecarily using the Server-Sent Events EventSource Transport).

Our use case is actually quite similar to Stackoverflow. We've got a custom CMS implemented as SPA that supports collaborative editing. As first step we want the server inform all clients on the same page when another user has modified it.

The obvious choice would be chosing SignalR for this but this statement on XSockets's comparison page got me thinking:

If a framework broadcasts data to all clients connected we have just inverted the AJAX issue and now we have a server hammering clients with information they do not want.

Is this still true with SignalR 2? I mean wouldn't broadcasting to all clients regardless of group membership make groups totally useless in the first place?

Some Stats:

  • > 10000 active users
  • > 10000 pages

Solution

  • When you send messages to a group, the clients that aren't in the group don't receive the message. This is based on a pub/sub model on the server (so when you use groups, it's not the client that decides whether it's interested in the message), so there is no "hammering all clients" going on in that case.

    I think what the XSockets team is talking about in this paragraph is that with XSockets, you have more fine-grained control over who to send messages to. So instead of sending to a group with a specific name (which you have to create first), you can send to clients with certain properties (sort of like a dynamic group):

    But what happens if you want to send to all clients within the range of x km from y? Or if you want to target only males between 25 - 30 yrs that has red hair

    You'd have to write your own code to do that in SignalR. This could, for example, be done on top of the code that maps users to connections. A simple solution (not necessarily the best) could use LINQ to select the appropriate users depending on the properties you're interested in, then get all corresponding connection ids from the ConnectionMapping instance to send to.