0

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
Oliver Weichhold
  • 10,259
  • 5
  • 45
  • 87

3 Answers3

2

A message sent to a group in signalr will only arrive at the clients in that group. So there is no need to worry about broadcasting if you use groups. Groups are smart until you you have to do something like @Lars Höppner describes, groups are not dynamic and they are really just a very simple subscription. It is just like having pub/sub where you say that you are subscribing to a topic "A" but in SignalR you are a member of a group "A" instead.

If you do not use groups signalr will broadcast, that can be ok but consider this.

You have 2 pages "A" and "B", they both connect to the Hub "MyHub". When you send a message to all clients from "MyHub" to the method "MyMessage" and only the page "A" has implemented a client-side method for "MyMessage" the clients on page "B" will still get that message sent to the browser if you look in the frames tab (in chrome) So messages will arrive at clients not interested in the message, not a big deal until you get a lot of clients or send sensitive data.

If you never connect to the same hub from 2 different pages you will be fine. And if you always use groups you will also be fine. Otherwise you will have to think about where the messages will arrive!

Of topic: - My favorite thing about signalr is the nice transports! - The worst thing about signalr is that they have transports due to the fact that they have desgined it thinking that websockets is a OS feature (win8 2012 server to get websockets)

Kim See Jonge
  • 256
  • 1
  • 5
  • 1
    you don't need to broadcast if you don't want to; as I said in my answer, you can simply collect the relevant connection ids dynamically and send to them; it just requires some additional code to get the list of connection ids somehow, and that depends on how you implement user state – Lars Höppner May 30 '14 at 17:15
  • Yes, I do agree on that! But I think the way to send to a subset of clients in signalr is pretty ugly. We use xsockets when we need to have control over where messages are being sent, but we also use signalr sometimes. It depends on what we are building. Plain web we usually use signalr since the transports just work! When we have to connect other clients, have control or need better speed we use xsockets. Love both solutions, but both of them have drawbacks, but most things do! – Kim See Jonge May 30 '14 at 18:01
  • 1
    How do you know that "A message sent to a group in SignalR will only arrive at the clients in that group". Can you provide a link to some documentation or code that shows how the server knows who to send the messages to. The [documentation](http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server#callfromoutsidehub) says " the server does not maintain lists of groups or group memberships. This helps maximize scalability, because whenever you add a node to a web farm, any state that SignalR maintains has to be propagated to the new node. " – Bentley Davis Feb 08 '15 at 15:58
  • @BentleyDavis - I actually do not know that this the case, but if groups does not work that way why have them. If groups broadcast they are a serious security risk imho – Kim See Jonge Feb 11 '15 at 22:41
1

In SignalR you can subscribe to a topic (group) and get messages only for the topics you are subscribed, and global messages.

Working with groups.

I am not completely sure about what that paragraph want to mean.

vtortola
  • 34,709
  • 29
  • 161
  • 263
1

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.

Lars Höppner
  • 18,252
  • 2
  • 45
  • 73