Introduction
The entire procedure demands several steps to finally trigger SignalR Hub. Before starting the implementation let’s learn something basic about SignalR.
What is SignalR?
SignalR is a library for ASP.NET developers that makes the process of real-time web functionality simple. The definition of Real-Time Web Functionality is the ability to have server code push content to connected clients. It provides a simple API in order to create a Remote Procedure Call (RPC).
SignalR and Web Socket
The new web socket transport is used by SignalR where available and falls back to older transport where necessary. The most important thing is that we can code our application to take advantage of the web socket without thinking about a separate code path.
Implementation
Create a new ASP.NET MVC application.
Add a class named Message.cs in the Models folder and include the following properties to the class.
public class Message
{
public int ID { get; set; }
public string Text { get; set; }
public DateTime DateTime { get; set; }
}
Create another class called MessageRepository.cs in the Models folder containing the following code.
public class MessageRepository
{
readonly string _connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
public List<Message> GetAllMessages()
{
var messages = new List<Message>();
using (var connection = new SqlConnection(_connString))
{
connection.Open();
using (var command = new SqlCommand(@"SELECT * FROM [dbo].[Message]", connection))
{
command.Notification = null;
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
connection.Open();
var reader = command.ExecuteReader();
while (reader.Read())
{
messages.Add(new Message
{
ID = (int)reader["ID"],
Text = (string)reader["Text"],
DateTime = Convert.ToDateTime(reader["DateTime"])
});
}
}
}
return messages;
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
MessagesHub.SendMessages();
}
}
}
Add a folder and rename it to Hubs. Add a class called MessagesHub.cs in it.



Humayun Kabir MamunPosted Sep 30, 2015, 4:54 AM
Great work Gopal...
RakeshPosted Sep 6, 2015, 12:34 AM
Good one
Santhakumar MunuswamyPosted Sep 5, 2015, 4:05 AM
Good Article. Thanks for sharing us
Karthikeyan KPosted Sep 5, 2015, 12:58 AM
Good one sir...Thanks for sharing
Pankaj Kumar ChoudharyPosted Sep 4, 2015, 8:18 PM
Nice Explain.......
Rajeesh MenothPosted Sep 4, 2015, 3:47 PM
Nice Share
Mohammed IbrahimPosted Sep 4, 2015, 3:40 PM
Nice