Reputation: 4342
I need to call all SignalR clients in a group from my server backend.
I need the equivalent of Clients.OthersInGroup('MyGroupName').MyFunction
but trough an IHubContext
object (or otherwise from outside the Hub instance). Is this somehow possible?
My use case: I've a provider hosted SharePoint App. SignalR is used for real-time updating other clients about changes (using the SignalR JavaScript Client). Since the app supports Multi-Tenancy clients connected to the server can be from different SharePoints tenants. Communication should always only be between clients of the same tenant. Therefor if a client connects it is added to a SignalR group for his SharePoint tenant. Now there are certain operations, like showing a document and if it does not exist create it, happening on the server. In this case the client calls an URL of the backend which then returns the document. If it is newly created this should be communicated back to the clients of the affected tenant.
Upvotes: 0
Views: 1455
Reputation: 2925
Note: This answer was edited to reflect additional information provided in question.
This problem can be approached from multiple ways:
Approach 1:
Forward notice to all clients who belong to tenant (I assume you are using tenant identifiers as group names). Include identification of document author in notification message. Then in code which handles notifications in client decide, if user is author of document. If he (she) is, do not display the notification.
This way, you don't have to do any special filtering on server side of application and you can use just standard API.
Approach 2:
You will have to create some kind of mapping between SignaR connections and users. If your application runs on single server, you can store this information in some static map. If your provider hosted app runs in farm, you have to use something like Redis. Mapping between users and connections can be done in OnConnected
(add connection ID to map) and OnDisconnected
(remove connection ID from map) methods of the hub.
Then when you are going to send notification, use Group
method of IHubContext.Clients
, get list of connections which belong to document author from the map and pass them as connections which should be ignored.
Something like this:
string[] connectionIdsBelongingToAuthor = // get from mapping, these will be ignored
string groupName = // get group which should be notified
hubContext.Clients.Group(groupName, connectionIdsBelongingToAuthor).NotificationMethod(...)
I'd go with approach 1 and decide, if notification should be shown in client.
Upvotes: 1