Oliver Weichhold
Oliver Weichhold

Reputation: 10306

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:

Upvotes: 0

Views: 1262

Answers (3)

Kim See Jonge
Kim See Jonge

Reputation: 256

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)

Upvotes: 2

Lars Höppner
Lars Höppner

Reputation: 18402

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.

Upvotes: 1

vtortola
vtortola

Reputation: 35945

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.

Upvotes: 1

Related Questions