Home Contact

Jeroen Bourdeaud'hui

web application developer

SignalR: example chat application

I remember myself a tweet from Scott Hanselman a couple of months ago, where he asked us to check out a small application he wrote. As one of  the first testers of his new "thing", I was interested but there were no documentation or libs available yet.

A couple of days a go, I read a blogpost from Maarten Balliauw about SignalR. (Maarten's blog). Because I allready heard about SignalR a couple of months ago, I had to develop a very small application to test how it really works.

1. Install SignalR package

You also have to update the jquery package to 1.6.2 or higher

2. Create a chat class

We only want 1 chat instance so use the Singleton pattern, lazy creation because that's thread safe.

    public class Chat
        private readonly static Lazy _instance = new Lazy(() => new Chat());
                public static Chat Instance
                return _instance.Value;


3. Create a (chat)hub

Create a class ChatHub which will inherit from Hub

Have a look at the HubName attribute. That's the name you will use in javascript to define the connection.

    public class ChatHub : Hub
        private readonly int TimeoutInSeconds = 30;
        private readonly Chat _chat;

        public ChatHub() : this(Chat.Instance) { }

        public ChatHub(Chat chat)
            _chat = chat;

4. Javascript connection

Create a connection between the client and the server. When the connection has been started, we want to call the join method on the server.
var chatHubClient = $.connection.chatHub;
// Start the connection
$.connection.hub.start(function () {

5. Model

Define a callback method on the server in the Chathub class

        public void Join(string myName)
            if (Chat.Clients.Where(x => x.Name.Equals(myName)).Count().Equals(0))
                Chat.Clients.Add(new Client() { Name = myName, LastResponse=DateTime.Now });
                SendMessageServer(string.Format("{0} entered the chat", myName));
                Caller.Naam = myName;
                throw new Exception("This login is allready in use");

Download the Chat Application Example

Get more information on GitHub
Listen to a podcast about SignalR on DotNetRocks
Wednesday, December 14, 2011 8:44 AM


# re: SignalR: example chat application

Definitely a geek's blog. That is not an insult, rather an appreciation of the contributions of these group of people to the development of technology and society as a whole. - Dr. Jerry M. Foster
3/1/2012 1:08 AM | Dr. Jerry M. Foster

# re: SignalR: example chat application

Hello, I can't source example 5/18/2012 5:56 PM | Quang

# re: SignalR: example chat application

Hi, I have the problem
private void BroadCastMessage(string message)
var clients = Hub.GetClients<ChatHub>();//Hub did not have this (GetClient)
//var clients = Hub.GetValue<ChatHub>();
//var clients = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
} 6/4/2012 12:22 PM | aung

# re: SignalR: example chat application

can i have the app in asp.net web form? plz 7/17/2012 7:46 PM | mks

# re: SignalR: example chat application

Thanks, this is exactly what I need! Precious little documentation on this. 7/31/2012 8:21 PM | Mark

# re: SignalR: example chat application

It's great! but i can't download Sample from above link. Can you help me to send sample source to my email.
Thanks so much! 1/25/2013 5:15 AM | Minh Tho

# re: SignalR: example chat application

it's very good. Thanks 3/28/2013 8:24 AM | lamwebsiteredep

# re: SignalR: example chat application

signalr has a problem with seo 10/17/2013 8:42 AM | meet

# re: SignalR: example chat application

Thanks you, can you reupload this project? 3/22/2014 9:14 AM | HelloWorld

# re: SignalR: example chat application

System.Web.Routing.SignalRRouteExtensions.MapHubs(System.Web.Routing.RouteCollection)' is obsolete: 'Use IAppBuilder.MapSignalR in an Owin Startup class.

this line actually creating the problem
please let me suggest to sort out this problem 5/19/2014 4:22 PM | pank

Post A Comment