Home Contact

Jeroen Bourdeaud'hui

web application developer

News

Twitter












Archives

Post Categories

Image Galleries

.NET url

Friends/Other developers

Syndication:

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
        {
            get
            {
                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.

    [HubName("chatHub")]
    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 () {
chatHubClient.join('@Model.Name');
});

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));
                GetClients();
                Caller.Naam = myName;
            }
            else
                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

Feedback

# 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

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: