Sam
Sam

Reputation: 30388

Sending messages to groups using SignalR Core

This is my first implementation of SignalR and I'm starting with SignalR Core so some of the examples and documentation don't seem to apply to the new version.

My objective is to send the received message to a particular chat room. I understand this could be achieved using Groups.

First question: I'm not seeing a way to check if a connection is already in a group. Do I NOT need to check if a connection is already in the Group? I receive a ChatRoomId with each message so I can easily add the user/connection to a Group. I just want to know if I need to check whether or not the connection is already a member of group.

Second question: I want to exclude the sender from receiving the broadcast of his own message. Looks like OthersInGroup has been removed from SignalR Core. How do I exclude the sender if I'm sending the message to a Group?

These are the options I'm getting from IntelliSense:

enter image description here

Upvotes: 1

Views: 1914

Answers (2)

Bernardo Gonzalez
Bernardo Gonzalez

Reputation: 21

For the second question: GroupExcept method is not available on SignalR alpha. You need the preview. You can add it running on the PM: dotnet add PROJECTNAME package Microsoft.AspNetCore.SignalR --version 2.1.0-preview1-27884 --source https://dotnet.myget.org/F/aspnetcore-dev/api/v3/index.json

Upvotes: 2

Brennan
Brennan

Reputation: 1933

For the first question:

There is no built in way to check if a connection is in a group. It is up to you to handle that via an injected service to your hub or a static Dictionary or something. So each time you call Groups.AddAsync and Groups.RemoveAsync you can keep track of that.

For the second question:

GroupExcept(string groupName, IReadOnlyList<string> excludeIds) was added recently and should be exactly what you're asking for.

Upvotes: 1

Related Questions