Click here to Skip to main content
12,299,947 members (54,659 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

69.3K views
4.9K downloads
39 bookmarked
Posted

Introduction to SignalR

, 14 May 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This article gives you a basic idea about the new developer's API called SignalR.

Introduction

This article gives you a basic idea about the new developer's API called "SignalR".

Background

In earlier ASP.NET applications, user refreshes a web page to see new data or a page uses "long polling" to retrieve new data, i.e., when new data arrives to server it will not be directly transmitted to all clients or specific clients, for that the user has to refresh the page, and the page will be updated.

For example, you can go to cricinfo.com, there you can see a page is refreshed for 1 or 2 seconds and many times you see the score is not updated. In such situations there is the need of the new Microsoft API called "SignalR".

What is SignalR

SignalR is a new developer's API provided for ASP.NET web applications, used to add "real time" web functionality to ASP.NET applications. "Real Time" web functionality is the ability to have server code to push contents to connected clients.

SignalR supports "server push" or "broadcasting" functionality. It handles connection management automatically. In classic HTTP connections for client-server communication connection is re-established for each request, but SignalR provides persistent connection between the client and the server. In SignalR the server code calls out to a client code in the browser using Remote Procedure Calls (RPC), rather than request-response model today. SignalR is an open-source API, and is accessible through GitHub.

Where to use:

  1. Chat room applications
  2. Real-time monitoring applications
  3. Job progress updates
  4. Real time forms

You can see the use of SignalR for a chat room application in the following image:

In the above example as soon as user2 sends some message, it will be received by all other users.

API Details

SignalR provides two models for communication:

  1. Persistent Connections
  2. The Persistent Connection API gives developer direct access to the low level communication protocol that SignalR exposes. This API uses the format of the actual message sent that needs to be specified and if the developer prefers to work with messaging and dispatching model rather than a remote invocation.

  3. Hubs:
  4. It's a High Level API written over PersistentConnection. This API allows the client and server to call methods on each other directly. Hubs also allow you to pass strongly typed parameters to methods, enabling model binding.

Code

Steps:

  1. Open Visual Studio 2010.
  2. Create a new project, select ASP.NET Empty Web Application and name it 'ChatRoomApplication'.
  3. Go to Tools and open Package Manager Console.
  4. If you don't have a Nuget package manager console then download it. For that go to Tools-->Extension Manager, and in the Search textbox, enter 'NuGet Package Manager', and download it.
  5. Open Package Manager console (Tools --> Library Package Manager --> Package Manager Console).
  6. Give command 'Install-Package Microsoft.AspNet.SignalR'.
  7. It will add references of SignalR DLLs to your application.
  8. In Solution Explorer right click on ChatRoomApplication and add class "ChatHub". A ChatHub.cs file will be added.
  9. Derive the ChatHub class from 'Hub'. It should look like:
  10. public class ChatHub : Hub
    {
    }

    Write method Send in the ChatHub class as:

    public class ChatHub : Hub
    {
        public void Send(string name, string message)
        {
            Clients.All.sendMessage(name,message);
        }
    }
  11. Now add the Global.asax file to your application. And in the application_start event map your hub by RouteTable.Routes.MapHubs(). It should look like:
  12. protected void Application_Start(object sender,EventArgs e)
    {
        RouteTable.Routes.MapHubs();
    }
  13. Now add an ASPX page and name it default.aspx
  14. Copy the following code in the <head></head> section:
  15. <script src="http://code.jquery.com/jquery-1.8.2.min.js" type="text/javascript"></script>
  16. Copy the following code into the <body> part:
  17. <div>
    <input type="text" id="msg" />
    <input type="button" id="send" value="Send" />
    <input type="hidden" id="displayname" />
    <ul id="messages">
    </ul>
    </div>
  18. That's it, now run your application, open multiple instances of the browser and copy the same URL there...and type message in the text box, click on the Send button, and you can see the message will be displayed on all browsers.
  19. For more information, look at the submitted code.

License

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

Share

About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 4 Pin
Member 1144475213-Apr-16 0:41
memberMember 1144475213-Apr-16 0:41 
GeneralMy vote of 3 Pin
Member 106596262-Nov-15 20:59
memberMember 106596262-Nov-15 20:59 
GeneralMy Vote of 5 Pin
Umesh Bhosale6-Jul-15 18:32
memberUmesh Bhosale6-Jul-15 18:32 
QuestionOwin Startup Class Pin
tehtb22-Mar-15 21:44
membertehtb22-Mar-15 21:44 
QuestionComments Pin
AjayJamwal16-Jul-14 22:23
professionalAjayJamwal16-Jul-14 22:23 
AnswerRe: Comments Pin
Amol Shelar10115-Dec-14 20:27
memberAmol Shelar10115-Dec-14 20:27 
Questionhow to use signalR in vs2008 Pin
mahesh chikhale26-Mar-14 12:01
membermahesh chikhale26-Mar-14 12:01 
AnswerRe: how to use signalR in vs2008 Pin
Amol Shelar10115-Dec-14 20:26
memberAmol Shelar10115-Dec-14 20:26 
QuestionVersion Conflict Pin
Arunprasath Natarajan24-Mar-14 21:44
memberArunprasath Natarajan24-Mar-14 21:44 
GeneralMy vote of 4 Pin
Saeid198412-Feb-14 1:28
memberSaeid198412-Feb-14 1:28 
QuestionHow to Create a Visual Basic Project to connect with the Website? Pin
Miklo B2-Dec-13 12:15
memberMiklo B2-Dec-13 12:15 
GeneralMy vote of 3 Pin
Rohit Taralekar9-May-13 22:01
memberRohit Taralekar9-May-13 22:01 
QuestionMight want to fix the spelling of SignalR Pin
ednrg9-May-13 9:39
memberednrg9-May-13 9:39 
AnswerRe: Might want to fix the spelling of SignalR Pin
Rohit Taralekar9-May-13 18:15
memberRohit Taralekar9-May-13 18:15 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160525.2 | Last Updated 15 May 2013
Article Copyright 2013 by Rohit Taralekar
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid