Amir Jalilifard
Amir Jalilifard

Reputation: 2059

Sending Notification to the users about the new events same as Facebook's notifications using SignalR

As you can see in Facebook's notification system,When an specified change related with your activities happens,Facebook notifies it to you. The important issue is that if we want to use SignalR to send notifications,we have 2 choices:

  1. Using the Client side to call a server method who broadcast a message to the Clients
  2. Using the Server side to Start a connection and Invoke a method in server side who broadcast a message to the Clients as bellow :
[HubName("messenger")]
    public class MessengerHub : Hub
    {
         /// <summary>
        /// Broads the cast message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void BroadCastMessage(Object message, string group)
        {
            _messenger.BroadCastMessage(message, group);
        }
    }

And here,we Invoke the BroadCastMessage method from Server to Notify Users :

            var connection = new HubConnection("http://localhost:21600/");
            SignalRConsole.SignalR.MessengerHub.Message message = new SignalR.MessengerHub.Message();
            message.Content = Console.ReadLine();
            message.Duration = 500;

            var myHub = connection.CreateProxy("messenger");
            connection.Start().Wait();
            myHub.Invoke("broadCastMessage", myData);
            Console.ReadLine();

Now,if we want to Broadcast a message as users Notifications based on the last changes in Database we should frequently check the Database and then start a connection to call a broadcast method to send notifications to the users.Then, the way i know for this continuous checking is using an infinite while loop same as bellow :

>      while (true){ 
>         //Some codes for tracing the last changes in Database such as new related events
>         if(CheckIfThereIsaNewEvent()){
>           connection.Start().Wait();
>           myHub.Invoke("broadCastMessage", myData);  
          }
>      }

This coding style should be terrible when we have a hundred millions of users because server should continuously calls a method and checks DataBase to the new events and broadcast a message to the vast array of clients,therefore we should allocate a huge computations and hence a huge load on the server and it is not logical. So,what is the true method to do this? What is the Facebook's solution for this problem?

Upvotes: 0

Views: 611

Answers (2)

James Dayeh
James Dayeh

Reputation: 526

Try using SqlDependency class OnChange Event to fire the SignalR method. You can specify on which query the Event should be fired :)

Upvotes: 0

Venkat
Venkat

Reputation: 115

Here we can trigger a message broadcast while database update. for example we have below method and db update method

[HubName("messenger")]
    public class MessengerHub : Hub
    {
         /// <summary>
        /// Broads the db update message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void BroadCastDbUpdate(Bool IsDbUpdate, string changes)
        {
            _messenger.BroadCastMessage(IsDbUpdate, changes);
        }
    } 



public void DbUpdate()
            {
//Do your db update and call the broadcast method
                _messenger.BroadCastMessage(IsDbUpdate, changes);
            }

Upvotes: 0

Related Questions