As the definition stands, “SignalR is an async library for .NET to help build real-time, multi-user interactive web applications”.
I am going to attempt to give you a super quick introduction to getting started with SignalR.
Let’s start out by creating an empty ASP.NET MVC 4 web application and grab the SignalR nuget package.
In our _Layout.cshtml we are going to reference jquery as well as the SignalR library.
<script src="/Scripts/jquery-1.6.4.min.js" type="text/javascript"></script> <script src="/Scripts/jquery.signalR-0.5.2.min.js" type="text/javascript"></script> <script src="/signalr/hubs" type="text/javascript"></script>
you might notice an additional reference there to /signalr/hubs. This is a bit of javascript that will dynamically generate hubs declared on the server. Let’s get on to creating that hub of ours.
You might be asking though, what is this Hub you speak of? Hubs provide a higher level RPC framework over a persistent connection. i.e. It’s a central communication point for all clients. We are going to implement a IRCHub that will send a message to all clients connected when we call the server side SendMessage method.
class IRCHub : Hub
{
public void SendMessage(string msg)
{
Clients.addMessage(msg);
}
}
All that is left to do now is write a bit of javascript to send messages to the hub and when we receive messages to broadcast them to all the clients listening.
<script type="text/javascript">
$(document).ready(function () {
var ircHub = $.connection.iRCHub;
ircHub.addMessage = function< (msg) {
$("#messages").append("<li>" + msg + "</li>");
};
$("#btnSendMessage").click(function () {
ircHub.sendMessage($('#txtMessage').val());
});
$.connection.hub.start();
});
</script>
From the /signalr/hubs script reference that we added earlier, signalr will generate a proxy called iRCHub from our server hub. Notice that this object will be named according to the javascript standards, which is camelcase.
We then hook up the addMessage which is the method the hub on the server will assume we have defined for all the clients listening on the hub and furthermore, we call the server side sendMessage on the ircHub when we want to broadcast a message to all our listeners.
Last but not least we want to start the connection.
This is pretty much it.
If you want to view the complete sample, you can find the source code here.
Read More

