C# Hub: Listener in the frontend only receives a new notification/message if the message is sent to all clients

29 Views Asked by At

I've experienced a problem with my C# SignalR-Hub.

A short description about the situation: The users can create some messages in my app for another users. If the other user is online at the same time I would like to show this new message without reloading the page.

My problem: The .on Listener just get triggered if I send the new message to all Clients. If I want to send the message just to one client it doesn't work and I don't know why. I have no idea. I've searched a lot but haven't found something helpful to solve this.

Here are some code snippets how I've implemented it:

Frontend - Listener to receive new messages, sent by the MessageHub

this.messageHubConnection.on('Message', (data: string) => {
      debugger;
});

Backend - Basic create-function of the messages, in which the messages are sent to the frontend

public ActionResult<int> Create(Message message)
{
       // This one doesn't work
     _messageHub.Clients.User(User.FindFirstValue(ClaimTypes.NameIdentifier)).SendAsync("Message",       JsonConvert.SerializeObject(message));

     // This one work
    _messageHub.Clients.All.SendAsync("Message", JsonConvert.SerializeObject(message) + " Test23");


    return base.Create(message);
}

What's my goal? My goal is that I can send the new messages only to the clients which are the real receivers and not to all clients.

0

There are 0 best solutions below