Here, I am using SQLDependency which notifies us when any changes are done to the data in the database. If you don't know about SQLDependency, refer to the link shown below.
First, you have to add an empty MVC project and after that, add the SignalR Package which can be added by using NuGet Package Manager.
Add a Controller in your project.
- public class HomeController : Controller
- {
-
- public ActionResult Index()
- {
- return View();
- }
- public JsonResult GetMessages()
- {
- List<data> messages = new List<data>();
- Repository r = new Repository();
- messages = r.GetAllMessages();
- return Json(messages,JsonRequestBehavior.AllowGet);
- }
- }
Now, add a new class and name it as Repository.
- public class Repository
- {
- SqlConnection co = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["con"].ConnectionString);
- public List<data> GetAllMessages()
- {
- var messages = new List<data>();
- using (var cmd = new SqlCommand(@"SELECT [id],
- [name],[adres] FROM [dbo].[userdata]", co))
- {
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- var dependency = new SqlDependency(cmd);
- dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
- DataSet ds = new DataSet();
- da.Fill(ds);
- for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
- {
- messages.Add(item: new data
- {
- id = int.Parse(ds.Tables[0].Rows[i][0].ToString()),
- name = ds.Tables[0].Rows[i][1].ToString(),
- adres = ds.Tables[0].Rows[i][2].ToString()
- });
- }
- }
- return messages;
- }
-
- private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
- {
- if (e.Type == SqlNotificationType.Change)
- {
- MyHub.SendMessages();
- }
- }
- }
In Models folder, add a class and name it as data.
- public class data
- {
- public int id { get; set; }
- public string name { get; set; }
- public string adres { get; set; }
- }
Here, you have to add a SignalR Hub Class to you project and name it as MyHub.
- [HubName("MyHub")]
- public class MyHub : Hub
- {
-
- [HubMethodName("sendMessages")]
- public static void SendMessages()
- {
- IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
- context.Clients.All.updateMessages();
- }
- }
Again, you have to add another class named OWIN Startup Class.
- public class Startup
- {
- public void Configuration(IAppBuilder app)
- {
-
- app.MapSignalR();
- }
- }
View(Index)
- @{
- Layout = null;
- }
-
- <!DOCTYPE html>
-
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- <title>Index</title>
- <script type="text/javascript" src="~/Scripts/jquery-1.6.4.min.js"></script>
- <script type="text/javascript" src="~/Scripts/jquery.signalR-2.2.2.js"></script>
- <script type="text/javascript" src="/signalr/hubs"></script>
- <script type="text/javascript">
- $(function () {
- // Declare a proxy to reference the hub.
- var notifications = $.connection.MyHub;
-
- //debugger;
- // Create a function that the hub can call to broadcast messages.
- notifications.client.updateMessages = function () {
- getAllMessages()
-
- };
- // Start the connection.
- $.connection.hub.start().done(function () {
- alert("connection started")
- //notifications.onconn();
- getAllMessages();
- }).fail(function (e) {
- alert(e);
- });
- });
-
- function getAllMessages()
- {
- var tbl = $('#messagesTable');
- $.ajax({
- url: '/Home/GetMessages',
- contentType: 'application/html ; charset:utf-8',
- type: 'GET',
- dataType: 'html'
- }).success(function (result) {
- var a2 = JSON.parse(result);
- tbl.empty();
- $.each(a2, function (key, value)
- {
- tbl.append('<tr>' + '<td>' +value.id + '</td>' + '<td>' + value.name + '</td>' + '<td>' + value.adres + '</td>' + '</tr>');
- });
- })
- }
- </script>
- </head>
- <body>
- <div>
- <table id="tab">
-
- </table>
- </div><br/>
- <div class="row">
- <div class="col-md-12">
- <div >
- <table border="1">
- <thead>
- <tr>
- <th>id</th>
- <th>name</th>
- <th>address</th>
- </tr>
- </thead>
- <tbody id="messagesTable">
-
- </tbody>
- </table>
- </div>
- </div>
- </div>
- </body>
- </html>