Geeks With Blogs
Jeroen Bourdeaud'hui web application developer

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 Posted on Wednesday, December 14, 2011 8:44 AM | Back to top


Comments on this post: SignalR: example chat application

# re: SignalR: example chat application
Requesting Gravatar...
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
Left by Dr. Jerry M. Foster on Mar 01, 2012 1:08 AM

# re: SignalR: example chat application
Requesting Gravatar...
Hello, I can't source example
Left by Quang on May 18, 2012 5:56 PM

# re: SignalR: example chat application
Requesting Gravatar...
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>();
//clients.Clients.says(message);
clients.newMessage(message);
clients.isAlive();
}
Left by aung on Jun 04, 2012 12:22 PM

# re: SignalR: example chat application
Requesting Gravatar...
can i have the app in asp.net web form? plz
Left by mks on Jul 17, 2012 7:46 PM

# re: SignalR: example chat application
Requesting Gravatar...
Thanks, this is exactly what I need! Precious little documentation on this.
Left by Mark on Jul 31, 2012 8:21 PM

# re: SignalR: example chat application
Requesting Gravatar...
It's great! but i can't download Sample from above link. Can you help me to send sample source to my email.
minhthohero@gmail.com
Thanks so much!
Left by Minh Tho on Jan 25, 2013 5:15 AM

# re: SignalR: example chat application
Requesting Gravatar...
it's very good. Thanks
Left by lamwebsiteredep on Mar 28, 2013 8:24 AM

# re: SignalR: example chat application
Requesting Gravatar...
signalr has a problem with seo
Left by meet on Oct 17, 2013 8:42 AM

# re: SignalR: example chat application
Requesting Gravatar...
Thanks you, can you reupload this project?
Left by HelloWorld on Mar 22, 2014 9:14 AM

# re: SignalR: example chat application
Requesting Gravatar...
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
RouteTable.Routes.MapHubs();
please let me suggest to sort out this problem
Left by pank on May 19, 2014 4:22 PM

Your comment:
 (will show your gravatar)


Copyright © Jeroen Bourdeaud'hui | Powered by: GeeksWithBlogs.net | Join free