Click here to Skip to main content
13,895,858 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


55 bookmarked
Posted 30 Apr 2014
Licenced CPOL

SignalR Simplified

, 3 May 2014
Rate this:
Please Sign up or sign in to vote.
SignalR demo

What is SignalR

  • Real-time HTTP-based asynchronous persistent connection communication framework for ASP.NET applications
  • Runs in all browsers that support JavaScript
  • Can be self-hosted
  • Provides a rich server and client side APIs set

SignalR makes it easy to build real-time multi-user connected applications.

The goal of this article is not to provide a detailed explanation of SignalR architecture and internals, but rather to help you to jump start writing SignalR applications.

There are 3 steps involved in writing SignalR applications:

  • Add SignalR components either through the new Visual Studio template or the NuGet package.
  • Write the server code in your preferred .NET language.
  • Write the client code in HTML and JavaScript.

Add SignalR to the project

In Solution Explorer, right-click the project and select Manage NuGet Packages. Type SignalR in the search area, click Search and select Microsoft ASP.NET SignalR package. After you click Install a set of script files and assembly references that support SignalR will be added to the project.

Server Code

Startup Class

The server needs to know which URL to intercept and direct to SignalR. To do that, we add an OWIN startup class. Right-click your project in Solution Explorer, select Add and Owin Startup Class. By convention the name of the new class should be Startup but it's not required. A new class will be created for you and no more changes are required.

[assembly: OwinStartup(typeof(SignalRDemo.Startup))] 
public class Startup
    public void Configuration(IAppBuilder app)
        // Any connection or hub wire up and configuration should go here

Notice the assembly attribute. It will set the startup class to the <code>Startup class in the SignalRDemo namespace.

Hub Class

Hubs are classes to implement push services in SignalR. They provide the abstraction on top of persistent connection and they are responsible for sending messages between server and client. Public methods defined in the hub are callable by the clients. Hub sends messages to clients by invoking client-side methods.

Also it detects and handles clients' connections.

public class ChatHub : Hub
    public void Send(string name, string message)
        // Call the broadcastMessage method to update clients.
        Clients.All.broadcastMessage(name, message);

Note that the Send() function defined on the server will be called by the client code.

Client Code

It connects HTML with JavaScript to send and receive data from the server.

Declare a proxy to reference the ChatHub (defined in the server code).

var chat = $.connection.chatHub;

Create a callback function in the script. The hub class on the server calls this function to push content updates to each client.

chat.client.broadcastMessage = function (name, message) {
    // Code the show the content pushed by the server

Call the Send method on the hub.

chat.server.send(name, message);

The last step is to start the connection and you're done.


Demo Project

The demo project contains the complete code that implements the following SignalR models:

  • Peer-to-peer - communications sent to clients are initiated by one of the clients
  • Server broadcast - communications sent to clients are initiated by the server
  • Group broadcast - sending a message to a group of users
  • Calculate the number of clients connected to the server

The demo project was developed with Visual Studio 2013 and .NET framework 4.5.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Igor Vigdorchik
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Question[My vote of 1] Copied From Microsoft Pin
Kevin Marois7-Feb-17 8:37
professionalKevin Marois7-Feb-17 8:37 
GeneralMy vote of 3 Pin
maynard_jk19-Dec-14 2:57
membermaynard_jk19-Dec-14 2:57 
GeneralMy vote of 5 Pin
Mahsa Hassankashi25-Oct-14 1:41
memberMahsa Hassankashi25-Oct-14 1:41 
GeneralRe: My vote of 5 Pin
Igor Vigdorchik25-Oct-14 4:02
memberIgor Vigdorchik25-Oct-14 4:02 
SuggestionNot Bad Pin
Pheonyx29-Sep-14 1:40
professionalPheonyx29-Sep-14 1:40 
GeneralRe: Not Bad Pin
Igor Vigdorchik29-Sep-14 7:14
memberIgor Vigdorchik29-Sep-14 7:14 
QuestionVery Good Articles Pin
VineshPatel199011-Jul-14 20:32
memberVineshPatel199011-Jul-14 20:32 
AnswerRe: Very Good Articles Pin
Igor Vigdorchik12-Jul-14 4:33
memberIgor Vigdorchik12-Jul-14 4:33 
SuggestionGot my 5... Pin
pl.26-May-14 2:56
memberpl.26-May-14 2:56 
GeneralRe: Got my 5... Pin
Igor Vigdorchik6-May-14 6:54
memberIgor Vigdorchik6-May-14 6:54 
QuestionGood, but would be better Pin
kiquenet.com4-May-14 23:47
professionalkiquenet.com4-May-14 23:47 
AnswerRe: Good, but would be better Pin
Igor Vigdorchik5-May-14 7:54
memberIgor Vigdorchik5-May-14 7:54 
QuestionVery nice article ! Pin
Volynsky Alex3-May-14 21:35
professionalVolynsky Alex3-May-14 21:35 
AnswerRe: Very nice article ! Pin
Igor Vigdorchik4-May-14 3:14
memberIgor Vigdorchik4-May-14 3:14 
GeneralRe: Very nice article ! Pin
Volynsky Alex4-May-14 21:09
professionalVolynsky Alex4-May-14 21:09 
GeneralMy vote of 5 Pin
Darshan.Pa2-May-14 18:54
professionalDarshan.Pa2-May-14 18:54 
GeneralRe: My vote of 5 Pin
Igor Vigdorchik3-May-14 3:11
memberIgor Vigdorchik3-May-14 3:11 
GeneralMy vote of 3 Pin
phil.o1-May-14 2:00
mvephil.o1-May-14 2:00 
GeneralRe: My vote of 3 Pin
Igor Vigdorchik1-May-14 7:36
memberIgor Vigdorchik1-May-14 7:36 
Question[My vote of 2] Hm... Pin
Nicholas Marty30-Apr-14 21:16
professionalNicholas Marty30-Apr-14 21:16 
AnswerRe: [My vote of 2] Hm... Pin
Igor Vigdorchik1-May-14 7:35
memberIgor Vigdorchik1-May-14 7:35 
GeneralRe: [My vote of 2] Hm... Pin
Gustav Brock4-May-14 23:19
professionalGustav Brock4-May-14 23:19 
QuestionVisual Studio 2013 Pin
Dewey30-Apr-14 14:43
memberDewey30-Apr-14 14:43 
AnswerRe: Visual Studio 2013 Pin
Igor Vigdorchik30-Apr-14 15:10
memberIgor Vigdorchik30-Apr-14 15:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190306.1 | Last Updated 4 May 2014
Article Copyright 2014 by Igor Vigdorchik
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid