Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

Using the Client side to call a server method who broadcast a message to the Clients
Using the Server side to Start a connection and Invoke a method in server side who broadcast a message to the Clients as bellow :

C#
[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 :

C#
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 :

C#
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?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900