Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcasting message to all clients except self in SignalR

I realize that these questions are similar:

SignalR - Broadcast to all clients except Caller

Send a message to all clients in a Group, except for the current client

However, they are old and nothing in the current documentation gave me any clues as to whether I am doing the right thing or not.

Here's the question/problem:

I need to be able to broadcast a message from the server-side to all connected clients except the guy that submitted the http request - imagine someone submitting a form and getting all people connected to the same form being notified that something has been submitted.

The way I am doing that now is something like this:

var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>(); var connectionID = HttpContext.Request.Cookies["conn-id"].Value; context.Clients.AllExcept(connectionID).addMessage(message); 

And every time a connection is established, I set ConnectionID in a cookie on the client side as so:

$.connection.hub.start().done(function(){        $.cookie("conn-id",$.connection.hub.id);  }); 

So my questions are:

  1. Is this the best/only approach I can take in this case? Can I read the connection id of the client that originated the request from somewhere else like hub.Context.ConnectionID or something similar to the way you would get the SessionID from HttpContext.Current.Session.SessionID?

  2. Can I set the connection id programmatically when a client connects by setting it to the SessionID, for example? Could that cause a problem for some reason? If so, can you please explain how to set the connection ID programmatically and to which event do I need to hook up in order to do this?

I noticed that everytime the page is reloaded, the connection id is changed to a different guid. How do you set the ConnectionId only once and maintain it at least for the life of the Session?

like image 353
Icarus Avatar asked Jun 21 '13 19:06

Icarus


People also ask

Is SignalR two way communication?

SignalR is a two-way RPC protocol (request–response protocol) used to exchange messages between client and server (bi-directional communication) that works independently of transport protocols.

Does SignalR guarantee delivery?

SignalR doesn't guarantee message delivery. Since SignalR doesn't block when you call client methods, you can invoke client methods very quickly as you've discovered. Unfortunately, the client might not always be ready to receive messages immediately once you send them, so SignalR has to buffer messages.

How do I send a message to a group in SignalR?

When user click on send button, the message to be posted to server side using signalR connection hub. Thus whenever you post any message after clicking the join group button, the message will appear to all the clients who has joined the group.


2 Answers

Your approach does work, however the correct approach is to use the built in API: Clients.Others.addMessage(message). Also there's no need to get the hub context inside the hub itself. You can always access the current connection ID via Context.ConnectionId or send message sto clients via Clients.All.foo();

And you cannot set the ConnectinoID programatically. If you'd like to track users across pages maintain a list of users on your server and then just re-assign the connection id's as they navigate through the site.

Here's a post explaining the tracking of users: SignalR 1.0 beta connection factory

like image 190
N. Taylor Mullen Avatar answered Sep 21 '22 14:09

N. Taylor Mullen


If you're sending the message in a method on the Hub class, you can also use Clients.Others to exclude the calling client. You only need to use AllExcept(id) if you're not in a hub method.

See the following for details:

http://www.asp.net/signalr/overview/hubs-api/hubs-api-guide-server#selectingclients

like image 33
Patrick Fletcher Avatar answered Sep 22 '22 14:09

Patrick Fletcher