Data Update Using SignalR

My last article explained SignalR and how to use it to create a simple web-based group chat. It included a good deal of explanation of how SignalR works and why it is a good candidate to create a real application. We will not get into details again and before you start with it, I recommend you read my previous article here. This discussion will be another practical implementation of what we had learned in the last article.

Let's create another sample application to understand the use of SignalR. This application will be a HTML page that displays some data from the database. At the back-end of this sample, we will have another application, say any form, Windows service or RSS feed that will insert the new data into the database. As soon as the new data is added to the database, that will be reflected on this HTML page.

An important point before we start the discussion is that this example will not only use SqlDependency, so that our server hub is notified about changes in the database and broadcast the latest data to the users. To describe the logic briefly, our hub will subscribe to SqlServer for any change in its data in the database, be notified, fetch the latest data and broadcast it to the HTML page. So our application will consist of the following 3 separate components.

A sample table called Users in a SQL database, with its ServiceBroker enabled.

An application with a HTML page to display the list of users from the database. This application will also include: 

 

  1. A webapi that receives data from a third application and save it into the database. 
  2. A SignalR component to broadcast the latest data to the users.
An application with an HTML page to submit new data to the database by making calls to the webapi.

So our overall application flow will be like the following:

 

Database setup: Create a new table named Users, in a sample database. To use the SqlDepedency, we need to enable the ServiceBroker for the database. This can be done using the following command:

ALTER DATABASE Database_Name SET ENABLE_BROKER

Or, from the UI by selecting the database, right-click on its properties, go to the Options, navigate to the Service Broker section and change the value to True for the Broker Enabled property.

 

Create the main application: This application will consist of the following components:

  1. Webapi to receive the data from the data feed application and save into the database.
  2. An HTML page to display the data from the database.
  3. SignalR component that will refresh the HTML page with the latest data, as soon as the new data is added to the database.

Let's create a Webapi that can receive the data from the user and store it in the database. For this, we add the references to the WebApi2 and OWIN packages, using the Nuget package manager. Once the references are added, we add a new class file of type Web API Controller class. Let's call it DataFeedController.cs. Add a POST method to receive the data from the user and store it in the database, using the Entity FrameworkSo our controller would look as in the following:

  1. public class DataFeedController: ApiController   
  2. {  
  3.     public void PostUserData([FromBody] User userData)   
  4.     {  
  5.         SampleDBEntities _dbEntities = new SampleDBEntities();  
  6.         _dbEntities.Users.Add(userData);  
  7.         _dbEntities.SaveChanges();  
  8.     }  
  9. }  

Now to host the Webapi and SignalR, we add a file named Startup.cs and add the routing template for the Webapi and register both the webapi and SignalR to the OWIN pipeline. When we start the application, it will result in hosting of the Webapi at the back-end.

  1. public void Configuration(IAppBuilder appBuilder)   
  2. {  
  3.     // Configure the SignalR hosting    
  4.     appBuilder.MapSignalR();  
  5.   
  6.     HttpConfiguration config = new HttpConfiguration();  
  7.     config.Routes.MapHttpRoute(  
  8.     name: "DefaultApi",  
  9.     routeTemplate: "{controller}/{action}");  
  10.   
  11.     // Configure the WebAPI hosting    
  12.     appBuilder.UseWebApi(config);  
  13. }  

Now to add the real-time functionality to the application we add a class named RealtimeDataHub.cs, derived from the Hub class and will be used as a middleware between the database and the HTML page (that is used to display the data). This class will have a method named GetUsers() that will get the data from the database and broadcast it to the connected users. Inside this method, the hub also subscribes to the SQL for getting notifications for a change in the database, using the OnDependency change event of the SqlDependency class. See the code below:

  1. public class RealtimeDataHub: Hub   
  2. {  
  3.     public void GetUsers()   
  4.     {  
  5.         List < User > _lst = new List < User > ();  
  6.         using(var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ADOEntities"].ConnectionString)) {  
  7.             String query = "SELECT UserId, FirstName, LastName, Age FROM dbo.Users";  
  8.             connection.Open();  
  9.             using(SqlCommand command = new SqlCommand(query, connection))   
  10.             {  
  11.                 command.Notification = null;  
  12.                 DataTable dt = new DataTable();  
  13.                 SqlDependency dependency = new SqlDependency(command);  
  14.   
  15.                 dependency.OnChange += dependency_OnChange;  
  16.   
  17.                 if (connection.State == ConnectionState.Closed) connection.Open();  
  18.   
  19.                 SqlDependency.Start(connection.ConnectionString);  
  20.                 var reader = command.ExecuteReader();  
  21.                 dt.Load(reader);  
  22.                 if (dt.Rows.Count > 0)   
  23.                 {  
  24.                     for (int i = 0; i < dt.Rows.Count; i++)   
  25.                     {  
  26.                         _lst.Add(new User {  
  27.                             UserId = Int32.Parse(dt.Rows[i]["UserId"].ToString()),  
  28.                             FirstName = dt.Rows[i]["FirstName"].ToString(),  
  29.                             LastName = dt.Rows[i]["LastName"].ToString(),  
  30.                             Age = Convert.ToInt32(dt.Rows[i]["Age"])  
  31.                         });  
  32.                     }  
  33.                 }  
  34.             }  
  35.         }  
  36.         IHubContext context = GlobalHost.ConnectionManager.GetHubContext < RealtimeDataHub > ();  
  37.         context.Clients.All.displayUsers(_lst);  
  38.     }  
  39.   
  40.     void dependency_OnChange(object sender, SqlNotificationEventArgs e)  
  41.     {  
  42.         if (e.Type == SqlNotificationType.Change) {  
  43.             RealtimeDataHub _dataHub = new RealtimeDataHub();  
  44.             _dataHub.GetUsers();  
  45.         }  
  46.     }  
  47. }  

Create data feed application: Create a new empty project and add an HTML page to it. This HTML page will have 3 textboxes and a button to store the data in the database, by calling the WebApi created in Step 1 above. We will call it as a data feeder application. In a real scenario, we can have any Windows service that is fetching data using some API and storing it in the database. So our HTML mark-up will be as in the following:

  1. <!DOCTYPE html>    
  2. <html xmlns="http://www.w3.org/1999/xhtml">    
  3. <head>    
  4.     <title></title>    
  5. </head>    
  6. <body>    
  7.     <table>    
  8.         <tr>    
  9.             <td>First Name: </td>    
  10.             <td>    
  11.                 <input type="text" id="txtFirstName" />    
  12.             </td>    
  13.         </tr>    
  14.         <tr>    
  15.             <td>Last Name: </td>    
  16.             <td>    
  17.                 <input type="text" id="txtLastName" />    
  18.             </td>    
  19.         </tr>    
  20.         <tr>    
  21.             <td>Age: </td>    
  22.             <td>    
  23.                 <input type="text" id="txtAge" />    
  24.             </td>    
  25.         </tr>    
  26.         <tr>    
  27.             <td></td>    
  28.             <td>    
  29.                 <input type="button" id="btnSave" value="Save Data" /></td>    
  30.         </tr>    
  31.     </table>    
  32.     
  33. </body>    
  34. </html>    

Next, use the ajax call to send the data to the webapi controller that stores it in the database.

  1. <script type="text/javascript" src="Scripts/jquery-1.7.1.min.js"></script>
  2. <script type="text/javascript">    
  3.     $("#btnSave").click(function ()   
  4.     {    
  5.         var url = "http://localhost:24010/DataFeed/PostUserdata";    
  6.            
  7.         var userData =   
  8.         {    
  9.             FirstName: $("#txtFirstName").val(),    
  10.             LastName: $("#txtLastName").val(),    
  11.             Age: $("#txtAge").val()    
  12.         };    
  13.     
  14.         $.ajax({    
  15.             type: "POST",    
  16.             cache: false,    
  17.             url: url,    
  18.             data: JSON.stringify(userData),    
  19.             contentType: "application/json",    
  20.             async: true,    
  21.             success: function (response)   
  22.             {    
  23.                 alert('Data Saved successfully...!!!');    
  24.             },    
  25.             error: function (err)   
  26.             {    
  27.                 alert('Call failed');    
  28.             }    
  29.         });    
  30.     })    
  31. </script>  
Note that here we have used the localhost URL to refer to the location where the web API is hosted. You can replace it with the actual URL of the WebApi location.

So our setup is complete now. To start the application, first run the HTML page of the main application that displays the data from the database. When this application is started, its corresponding web API is also hosted. The first time, there will be no data. So let's start the data feeder application also and add some data. Add the new data and save it. As soon as the new data is added, it is immediately reflected in the main applications home page. See below:

 

So now we need to use timer-based calls. Use the SignalR functionality and create real applications. I hope you enjoyed reading this and it will be helpful for you. Happy coding!


Similar Articles