Search code examples
asp.netsignalrsignalr-hub

signalR first demo project


I am new to SignalR and I am looking for a sample application in Asp.net not mvc that does real time notifications so that I can start working. Can any one guide me to a working sample because I have downloaded many samples that do not work


Solution

  • Please note that this answer was for SignalR version 0.5.3 and is now out of date with the latest version of SignalR

    It is really simple just to set up your own little Demo. There really isn't much to it. Just create a new project, install SignalR via the NuGet package manager console, and then do the very basics to get it running.

    I blogged about how to do it over at my site http://timjames.me/creating-your-first-signalr-mvc-project

    Hub

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using SignalR;
    using SignalR.Hubs;
    
    namespace MySignalR
    {
        public class SNRL : Hub
        {
            public void SendMessage(string msg)
            {
                Clients.sendMessage(msg);
            }
        }
    }
    

    Javscript

    <script>
        $(function () {
            var myHub = $.connection.sNRL;
            myHub.sendMessage = function (data) {
                $('#mySpanTag').html(data);
            };
            $.connection.hub.start({ transport: 'auto' }, function () {
                myHub.sendMessage("Hello World!");
            });
        });
    </script>
    

    html

    <span id="mySpanTag"></span>
    

    And make sure you reference the correct script files

    <script src="~/Scripts/jquery-1.6.4.min.js"></script>
    <script src="~/Scripts/jquery.signalR-0.5.3.min.js"></script>
    <script src="/signalr/hubs"></script>