Click here to Skip to main content
11,923,517 members (70,342 online)
Click here to Skip to main content
Add your own
alternative version


183 bookmarked

Asp.Net SignalR Chat Room

, 14 Mar 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This article helps you to understand the ASP.NET SignalR API and create a chat room using it.


Recently Microsoft added some new features in its ASP.NET Web Application Framework. SignalR is one of the prominent feature to build real time application e.g. social application, multiuser games, news weather etc. In real time applications server pushes the content to connected clients instantly it become available. It provides a simple ASP.NET API for creating server-to-client remote procedure calls (RPC) that calls JavaScript functions in client browsers from server-side .NET code. 

The web application works in Request-Response model. Browsers and other user agents make requests and web server provide a response to that request. The response is sent to the delivery address provided in the request by user agent. In this model server can’t make responses without a request. So in real time application server pushed newly available contents/data to the client. You can achieve this feature using ASP.NET SignalR API.

Get Started

For explaining the SignalR Web API, in this article we will create a chat application where you can do group chat and private chat with single user. You require Visual Studio 2012 Express Development Tool for creating this project. You can get more details on

Create a new web application project in visual studio and In Solution Explorer, Right-click the project, select Add > New Item, and select the SignalR Hub Class item. Name the class and press the Add button. It will add the Hub class, required references and scripts.

You can see added script and references by expanding ‘Scripts’ and ‘References’ nodes.

Register Hub URL

For using SignalR API, we need to register ~/signalr/hubs URL. Add global.asax file in your solution and in the Application_Start method register hub url using RouteTable.Routes.MapHubs() command.

public class Global : System.Web.HttpApplication

   protected void Application_Start(object sender, EventArgs e)
       // Register the default hubs route: ~/signalr/hubs


Hub Class

ChatHub class must be inherited from the Microsoft.AspNet.SignalR.Hub class. The Hub class exposes some properties and methods. Using Clients property you can communicate with connected clients. You can get the information of client who is calling method using Context and you can also manage groups using Groups property.

public abstract class Hub : IHub, IDisposable

  public HubConnectionContext Clients { get; set; }
  public HubCallerContext Context { get; set; }
  public IGroupManager Groups { get; set; }

  public virtual Task OnConnected();        
  public virtual Task OnDisconnected();       
  public virtual Task OnReconnected();


Derived class can override OnConnected, OnDisconnected, which are very useful to perform some actions on these events. ChatHub class is derived from the Hub class and we will discuss its method in next comming sections.

public class ChatHub : Hub

   public void Connect(string userName);

   public void SendMessageToAll(string userName, string message);

   public void SendPrivateMessage(string toUserId, string message);

   public override System.Threading.Tasks.Task OnDisconnected();



Client calls Connect method when he wants to join Chat room. For sending message in chat room to all connected clients, it calls SendMessageToAll , For private chat with single user client calls SendPrivateMessage method.

Proxy at Client side

Add new page index.html in the solution and set the references of JQuery, SignalR and autogenerated hubs.

<script src=""></script>

<!--Reference the SignalR library. -->
<script src=""></script>

<!--Reference the autogenerated SignalR hub script. -->
<script src=""></script>

At the client side you need to create the hub proxy and start that proxy. You can create hub proxy using $.connection.yourHubClass and then start hub using $.connection.hub.start() method.

<script type="text/javascript">

 $(function () {

    // Declare a proxy to reference the hub.
    var chatHub = $.connection.chatHub;


    // Start Hub
    $.connection.hub.start().done(function () {





The import thing to be noticed here is the naming convention is being used in auto generated Hub proxy. We named our class ChatHub at server in camel case but at client side we are getting it in lower camel case like $.connection.chatHub

Connect to the Chat Room

In our Chat room user connects to the chat room by passing his name and on the successful connection, we send him a list of connected clients and some recent chat history which we saves in our application. Our first method in the ChatHub class is the Connect.

public void Connect(string userName)

   var id = Context.ConnectionId;

   if (ConnectedUsers.Count(x => x.ConnectionId == id) == 0)

       ConnectedUsers.Add(new UserDetail { ConnectionId = id, UserName = userName });

       // send to caller
       Clients.Caller.onConnected(id, userName, ConnectedUsers, CurrentMessage);

       // send to all except caller client
       Clients.AllExcept(id).onNewUserConnected(id, userName);



Get the connection id of the client who is calling the Connect method using Context.ConnectionId property. Check the id in existing connected clients list. If it does not exist then add it into the list. Now we need to do two more steps. First we have to send list of already connected clients and recent message list to the client who wants to connect and second we have to inform the other connected clients about new joiner.

So we can easily call the client side method of single client who want to connect using Clients.Caller property.

// send to caller
Clients.Caller.onConnected(id, userName, ConnectedUsers, CurrentMessage);

Inform the other connected clients about the new client, but we don’t want to call the method of the newly connected client. Use AllExcept property of the Clients, which can excludes some client as per your wish.

// send to all except caller client
Clients.AllExcept(id).onNewUserConnected(id, userName);     

Define/Expose your client side methods which are being called by server side code by statements Clients.Caller.onConnected(...) and Clients.AllExcept(id).onNewUserConnected(...). You can define your methods using chatHub.client.yourMethodName at client side.

// Calls when user successfully logged in
chatHub.client.onConnected = function (id, userName, allUsers, messages) {



// On New User Connected
chatHub.client.onNewUserConnected = function (id, name) {

   AddUser(chatHub, id, name);

From the client side you can Call the server side methods using chatHub.server.yourMethod statement in your web page.  


Send Message in Chat room

In main chat room message typed by user is broadcast to all connected clients. On the server side in ChatHub class write SendMessageToAll and broadcast message using Clients.All.messageReceived method. messageReceived is a client side method.

public void SendMessageToAll(string userName, string message)
   // store last 100 messages in cache
   AddMessageinCache(userName, message);

   // Broad cast message
   Clients.All.messageReceived(userName, message);

On client side expose the messageReceived method and in this method simply add message in the chat area using JQuery.

chatHub.client.messageReceived = function (userName, message) {

   AddMessage(userName, message);


After writing client side and server side methods, now we have to register button click event on client side and when user will click button we will call server side method sendMessageToAll, which broadcast message to the all connected clients.

$('#btnSendMsg').click(function () {

    var msg = $("#txtMessage").val();
    if (msg.length > 0) {

        var userName = $('#hdUserName').val();

        chatHub.server.sendMessageToAll(userName, msg);


Private Chat

You can also initiate private chat by double clicking on the connected client name. In private chat we do not send message to all connected client. In Private chat only two clients can send/receive each other message. So for sending private message sender will call server side sendPrivateMessage method. And in this method we send message to other client using Clients.Client(toUserId).sendPrivateMessage(..) method.

public void SendPrivateMessage(string toUserId, string message)

   string fromUserId = Context.ConnectionId;

   var toUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == toUserId) ;
   var fromUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == fromUserId);

   if (toUser != null && fromUser!=null)
       // send to
       Clients.Client(toUserId).sendPrivateMessage(fromUserId, fromUser.UserName, message);

       // send to caller user
       Clients.Caller.sendPrivateMessage(toUserId, fromUser.UserName, message);


On Disconnect

On the close of the browser SignalR API calls OnDisconnected method. Override this method in your ChatHub class and in this method remove disconnected client from the cache list and send notification to other connected clients.

public override System.Threading.Tasks.Task OnDisconnected()
    var item = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
    if (item != null)

        var id = Context.ConnectionId;
        Clients.All.onUserDisconnected(id, item.UserName);


    return base.OnDisconnected();


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


About the Author

Shakeel Iqbal
Software Developer (Senior) TEO
Pakistan Pakistan
Passion and positive dedication is essential part of success. I believe on hardworking and sharing knowledge with others. I always try to be a better than I am and think positive for positive result.

My Blogs

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
jacksparrow_4725-Aug-13 8:52
memberjacksparrow_4725-Aug-13 8:52 
GeneralMy vote of 5 Pin
psov10-Aug-13 21:57
memberpsov10-Aug-13 21:57 
QuestionNo list of connected clients generated Pin
Member 1016736230-Jul-13 0:31
memberMember 1016736230-Jul-13 0:31 
AnswerRe: No list of connected clients generated Pin
Shakeel Iqbal30-Jul-13 10:10
memberShakeel Iqbal30-Jul-13 10:10 
GeneralRe: No list of connected clients generated Pin
Member 1084981511-Jun-14 10:03
memberMember 1084981511-Jun-14 10:03 
QuestionGood Project - Works Well Pin
fchateau29-Jul-13 0:04
memberfchateau29-Jul-13 0:04 
Questionwindows phone app Pin
Member 832614323-Jul-13 23:30
memberMember 832614323-Jul-13 23:30 
AnswerRe: windows phone app Pin
Shakeel Iqbal24-Jul-13 2:32
memberShakeel Iqbal24-Jul-13 2:32 
GeneralMy Vote of 5 Pin
Mohsen_Malekan3-Jul-13 17:26
memberMohsen_Malekan3-Jul-13 17:26 
QuestionBUTTON Pin
Member 101352332-Jul-13 5:13
memberMember 101352332-Jul-13 5:13 
AnswerRe: BUTTON Pin
Joseph!24-Nov-13 15:55
memberJoseph!24-Nov-13 15:55 
GeneralMy vote of 5 Pin
YanZhiwei16-Jun-13 23:23
memberYanZhiwei16-Jun-13 23:23 
GeneralMy vote of 5 Pin
Pratik.Patel13-Jun-13 22:25
memberPratik.Patel13-Jun-13 22:25 
GeneralMy vote of 5 Pin
rohitvermasrt18-May-13 2:36
memberrohitvermasrt18-May-13 2:36 
Best sample application of SignalR to use with Chat Application.
GeneralMy vote of 5 Pin
RajuMab10-May-13 6:43
memberRajuMab10-May-13 6:43 
GeneralMy vote of 3 Pin
rohitvermasrt10-May-13 4:49
memberrohitvermasrt10-May-13 4:49 
QuestionRegrading for logged in user Pin
sumit115725-Apr-13 20:31
membersumit115725-Apr-13 20:31 
GeneralMy vote of 5 Pin
Meringue23-Apr-13 15:41
memberMeringue23-Apr-13 15:41 
GeneralMy vote of 5 Pin
duongbkse23-Apr-13 5:58
memberduongbkse23-Apr-13 5:58 
QuestionReally great Pin
claudiocas18-Apr-13 4:21
memberclaudiocas18-Apr-13 4:21 
Question$.connection.chatHub is coming undefind Pin
AnujAthawal13-Apr-13 0:51
memberAnujAthawal13-Apr-13 0:51 
Suggestion2012 version Pin
hiphopper01234-Apr-13 2:23
memberhiphopper01234-Apr-13 2:23 
GeneralRe: 2012 version Pin
Prateek_Saini3-May-13 0:32
professionalPrateek_Saini3-May-13 0:32 
GeneralRe: 2012 version Pin
mynkow30-May-13 2:56
membermynkow30-May-13 2:56 
GeneralRe: 2012 version Pin
Prateek_Saini30-May-13 4:05
professionalPrateek_Saini30-May-13 4:05 
GeneralMy vote of 5 Pin
frazzle-me22-Mar-13 5:10
memberfrazzle-me22-Mar-13 5:10 
QuestionDeployment problem Pin
ugurceng20-Mar-13 3:58
memberugurceng20-Mar-13 3:58 
AnswerRe: Deployment problem Pin
Shakeel Iqbal21-Mar-13 20:25
memberShakeel Iqbal21-Mar-13 20:25 
GeneralRe: Deployment problem Pin
daniel.colaianni6-Aug-13 1:13
memberdaniel.colaianni6-Aug-13 1:13 
AnswerRe: Deployment problem Pin
abhi shinde25-Jul-13 11:12
memberabhi shinde25-Jul-13 11:12 
GeneralRe: Deployment problem Pin
Joseph!24-Nov-13 15:57
memberJoseph!24-Nov-13 15:57 
GeneralMy vote of 5 Pin
Burak Ozdiken16-Mar-13 0:09
memberBurak Ozdiken16-Mar-13 0:09 
GeneralMy vote of 5 Pin
S. M. Ahasan Habib14-Mar-13 23:37
memberS. M. Ahasan Habib14-Mar-13 23:37 
QuestionRegarding your chat system Pin
Tridip Bhattacharjee14-Mar-13 22:26
memberTridip Bhattacharjee14-Mar-13 22:26 
AnswerRe: Regarding your chat system Pin
Shakeel Iqbal15-Mar-13 8:59
memberShakeel Iqbal15-Mar-13 8:59 
GeneralRe: Regarding your chat system Pin
Tridip Bhattacharjee17-Mar-13 21:53
memberTridip Bhattacharjee17-Mar-13 21:53 
GeneralMy vote of 5 Pin
i_islamian14-Mar-13 20:04
memberi_islamian14-Mar-13 20:04 
GeneralMy vote of 5 Pin
TechnoGeek00114-Mar-13 19:46
memberTechnoGeek00114-Mar-13 19:46 

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
Web03 | 2.8.151125.3 | Last Updated 14 Mar 2013
Article Copyright 2013 by Shakeel Iqbal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid