Aamir Sajjad
Aamir Sajjad

Reputation: 27

asp.net signalr send message to specific client is not working

sir i am trying to sent message to specific user with asp.net clean architecture SignalR and flutter app, but its not working by clindid and by user id, plz help me to solve this.

public class ChatHubs : Hub
{
    private readonly static ConnectionMapping<string> _connections = new ConnectionMapping<string>();

    public async Task SentMessage(string receiverId, string createdBy, string created, string text, string type)
    {
        var userId = GetCurrentUserId();

        foreach (var connectionId in _connections.GetConnections(receiverId))
        {
            // its working
            //await Clients.All.SendAsync("ReceiveMessage", receiverId, createdBy, created, text, type);

            // its not working
            await Clients.Client(connectionId).SendAsync("ReceiveMessage", receiverId, createdBy, created, text, type);
        }
    }
}

Upvotes: 1

Views: 388

Answers (1)

Tupac
Tupac

Reputation: 2910

You can print out the contents of

foreach (var connectionId in _connections.GetConnections(receiverId))

and see the contents.

If you want to correctly store the connectionid of each established connection, you may refer to this:

https://stackoverflow.com/a/13514344/7687666

Upvotes: 1

Related Questions