Zijah
Zijah

Reputation: 31

SignalR system notification once a user is connected

So, I'm new to SignalR and I want to make sort of a notification that will trigger once a user is connected, which he will have to clear (just click x). That notification should be shown to every new user that connects. The framework is Angular and asp.net. So far, I've only made a function where a message is broadcast to every connected user, but that will not help me much...

Upvotes: 0

Views: 618

Answers (1)

Kiril1512
Kiril1512

Reputation: 3611

So you done the part of connecting your client side code to your hub. Now what you need to do is to send the message only to connected client overriding the OnConnected method like:

[HubName("YourHubName")]
public class YourHub : Hub
{
    public override async Task OnConnected()
    {
        await base.OnConnected();

        await this.Clients.Caller("BroadcastMessage", "You are now connected!");
    }
}

So basically when the user connects the hub, and since it is the caller of this method, it will receive the notification.

On the Angular side, I would recommend you to have two different modules, services and notifications.

  • Service - Will be responsible to connect to the hub, and invoke hub methods.
  • Notifications - Will be responsible to receive the notification objects from the service and add to your notification panel/card/list.

Upvotes: 1

Related Questions