1

I use signalR hub to send messages and files in my web API service chat. I write a method to handle how messages send to clients as broadcast, user, or group :

public class MessageHub : Hub<IMessageHub>,IMessage
    {
        private readonly static ConnectionMapping<string> _connections = new ConnectionMapping<string>();
        private readonly static GroupMapping<string> _groups = new GroupMapping<string>();


        public async Task MessageDispatcher(ChatMessage message)
        {
            message.User = GetUserName();
            // Broadcast message
            if (string.IsNullOrEmpty(message.SendToGroup) && string.IsNullOrEmpty(message.SendDirectToUser))
                await Clients.AllExcept(Context.ConnectionId).SendMessage(message);//send all except current user
            //Send direct to user
            if (!string.IsNullOrEmpty(message.SendDirectToUser))
            {
                var connectionIds = _connections.GetConnections(message.SendDirectToUser);
                foreach (string userConnectionId in connectionIds)
                {
                    await Clients.Client(userConnectionId).SendMessage(message);
                }
            }
            //Send to group
            if (!string.IsNullOrEmpty(message.SendToGroup))
            {
                foreach (var user in _groups.GetUsers(message.SendToGroup))
                {
                    var connectionIds = _connections.GetConnections(user);
                    foreach (string userConnectionId in connectionIds)
                    {
                        await Groups.AddToGroupAsync(userConnectionId, message.SendToGroup);
                    }
                }
                await Clients.GroupExcept(message.SendToGroup, Context.ConnectionId).SendMessage(message);
            }
        }
}

Now I add a controller to my web API app to transfer files between clients because SignalR doesn't support file transfer as I found.

I want to call my MessageDispacher method to notify another user as my client wants but I don't have access to MessageDispacher.

any solution?

Javad Azimi
  • 95
  • 1
  • 10
  • Does this answer your question? [Call SignalR Core Hub method from Controller](https://stackoverflow.com/questions/46904678/call-signalr-core-hub-method-from-controller) – Dimitris Maragkos Oct 02 '22 at 08:01
  • one option to notify user is that first you upload your file and then invoke MessageDispacher from jquery as you are using for send message. another option is provided by @dimitris-maragkos already – Rajesh Kumar Oct 02 '22 at 08:21
  • The mentioned method by @dimitris-maragkos has access to the dispatching method that all clients are connected to. I need to have access to Send() method that is part of my logic there. Right now first upload files to the endpoint and then ask React to call Dispatch method – Javad Azimi Oct 03 '22 at 05:37

0 Answers0