SignalR with MVC. A match, “almost” made in heaven

 

What is this Article all about?

This article attempts to explain the things that you need to know about SignalR and MVC prior to starting to set it up in your MVC projects. Yes I am aware that the new VS2012 and MVC 4 will make it easier for us and install it automatically with the templates and all will be right with the world. Unfortunately, not all of us are starting a project from scratch when using SignalR or aren’t allowed to be on “ the cutting technology edge”. So I wanted to briefly explain SignalR and then let everyone know what the things you should watch out for while setting up your project. It is not a tutorial on using SignalR, I will be doing a separate article for that.

 

What is SignalR?

The 10 million dollar question right? What is SignalR? SignalR is JavaScript based (on the client end) and a .NET based on the Server end. It allows for constant Pub Sub interaction / updates between a browser and some server entity that has dynamically changing data. Some of these applications can include, but not limited to:

  1. Stock Tickers
  2. Ticket Queues
  3. Chat room sites (Jabber website chat hosts with SignalR)
  4. Credit Card Information

The list can go on, but I digress. So the question becomes, what does all this? A man by the name of David Fowl (https://github.com/davidfowl) created SignalR in his spare time. It has gained traction in the past year and is spreading like wild fire with the help of Microsoft, specifically, Scott Hanselman. He is a speaker / consultant for Microsoft and speaks on SignalR at many of his conferences and sees great promise in the tool. His blog is Here. You will hear me talk about him a lot because I follow his blog quite often as he sees many technologies for what they are. So enough about what SignalR, how does one use it in an MVC project?

 

Using SignalR in an MVC project

So now you know what SignalR is, how does one use it. Well, the best way to  add it to your project is through the NuGet Gallery. If you aren’t already familiar with NuGet, I will be creating a separate blog for how to add it to VS and what it is used for. This blog assumes you have used it before.

Opening Package Manager, at the console prompt type the following:

PM> Install-Package SignalR

This will install all the server hub and JavaScript required to get started with SignalR. With that said, before you begin, there are several things that should be added / changed for MVC projects to save later headaches.

1. On the SiteMaster page ( or wherever you are referencing the main JQuery script), make sure you are on the proper JQuery version for the SignalR version being used. Visual Studio adds jquery to the project for you, which is nice, but it happened to be 1.4.4, when it turned out I needed 1.6.4.

2. When using your references to the JQuery signalR scripts (especially the signalR/hubs folder) be sure to use the MVC HTML helper URL.Content(); routine. This allows MVC to decide where you are in relation to the said file instead of you trying to guess every time you use it. Examples of this are:

 <script src="<%: Url.Content("~/Scripts/jquery.signalR-0.5.2.js") %>" type="text/javascript"></script>
<%-- Dynamic Code created on the fly by Signal R--%>       script src="<%: Url.Content("~/signalr/hubs") %>" type="text/javascript" ></script>

These  scripts are being added the proper way so that MVC can handle it’s routing magic. The reason this is especially important for the signalr/hubs file is, as you can see in the comments, this folder is dynamically created on the fly as many of SignalR is done. This folder defines all the hubs you have declared and what methods JavaScript is allowed to call through the connection.

If you keep these two things in mind, you should have a much easier time implementing your first SignalR and will be on your way to real-time data websites and applications.

Advertisements

About Gregg Coleman

I am Senior-level Software Engineer working primarily these days with .NET. I have a good working knowledge of ASP.NET MVC, Web Forms, WCF web services and Windows Services. I spend much of my time in the Web Services (SOAP and REST) world in my current job designing and implementing various SOA architectures. I have been in the software engineering industry for about 6 years now and will not now nor ever consider myself an "expert" in programming because there is always so much to learn. My favorite thing about designing software is there are always new emerging technologies and something to learn every day! My current job has me spending much of my job on the bleeding edge of technologies and changing gears all the time, so I'm never bored and always challenged. On my spare time I enjoy weight training, reading and venturing to new places near by. Of course programing and learning new technologies are another hobby of mine.
This entry was posted in ASP.NET, C#, Long Polling, MVC, Programming, SignalR and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s