1

I'm new and experimenting SignalR, so go easy.

I know we can access clients of all sorts within the hub class itself by using

Clients.All
Clients.Others
Clients.Group
Clients.Caller
Clients.Client

However, whenever I try to access clients from other classes by the hub context

var hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();

hubContext only gives me access to

hubContext.Clients.All
hubContext.Clients.Group
hubContext.Clients.Client

There are two questions I'd like some answers to.

  1. How can I access Clients.Others/Caller from the hubContext?
  2. How can I retrieve my mapped connectionlist in the hub from hubContext so that I can iterate through the list of connection ids to be passed over to Clients.Client() method?

Assuming my mapped user lists as below:

public static List<UserConnection> ActiveUsers = new List<UserConnection>();

//onConnected will add users of UserConnection class into ActiveUsers list

class UserConnection
{
    public string UserName { set; get; }
    public string ConnectionID { set; get; }
}
NewbieCoder
  • 676
  • 1
  • 9
  • 32
  • It's because inside your `NotificationHub`, Client is `IHubCallerConnectionContext` type, but `GetHubContext()` method returns `IHubContext`. – SᴇM Sep 18 '18 at 12:46
  • Possible duplicate of [calling the Caller method in SignalR hub outside the hub context](https://stackoverflow.com/questions/28468294/calling-the-caller-method-in-signalr-hub-outside-the-hub-context) – SᴇM Sep 18 '18 at 12:51
  • A _caller_ (client) can obviously only exist in a `Hub` instance. Outside of this instance there can never be a caller. Hence no `Clients.Others` either, because this means, _all except the caller_ – ChristianMurschall Sep 18 '18 at 12:52
  • Noted on the answers to my first questions. How can I then retrieve the `ActiveUsers` list from `hubContext`? – NewbieCoder Sep 18 '18 at 12:55
  • Also, please help to look into this issue. https://stackoverflow.com/questions/52385058/signalr-call-stacks-on-different-browser-for-notification – NewbieCoder Sep 18 '18 at 12:57
  • @NewbieCoder I doubt you can get the `ActiveUsers` from the hubcontext. You already solved the problem yourself: You made the list `public static`. See also: https://stackoverflow.com/a/13514344/4634044 – ChristianMurschall Sep 18 '18 at 13:06

0 Answers0