SignalR What?

Barend Bootha

http://drop.barendb.com/talks/signalr/index.html
Demo Code
QRCode

Who Am I?

Barend Bootha

Lead Developer at iTICKET

www.iticket.co.nz

@barendb

Pronounced

Barend: /Bar'rin'd/

For all you kiwi's :)

Agenda

  • What is SignalR?
  • Use Cases
  • Code Demo
  • Understanding the ARRR
  • SignalR on Azure
  • Questions
  • Links
  • Web
    +

  • Full-Duplex communication over HTTP
  • SignalR is...

  • Cross client wrapper for WebSockets, abstracts transport

  • Fallback transport for lesser browsers
  • Dashboards: Stock Tickers / Analytics / Live Scores / Auctions
  • Monitoring: Gather Analytics / Insights
  • Real time notifications [Push like]
  • Collabrative apps
  • OMG Chat! ...no it's lame!
  • I've seen this all before...

    Forever Frame

  • Server tells client response is chucked
  • Client keeps the connection open till the client closes it
  • Server pushes data to client followed by \0
  • Consumes server threads
  • Periodic Polling

  • Server is polled from time to time via AJAX
  • Delay in communication due to polling interval
  • Wastes bandwidth & latency
  • Consumes server threads
  • Long Polling

  • Poll but do not respond until there is data
  • Poll again after data is recieved or connection times out
  • Consumes server threads and resources
  • Demo - ShootR

    http://shootr.signalr.net/

    Demo

    Getting SignalR

    PM> Install-Package Microsoft.AspNet.SignalR -pre

    Code deep dive

    Persistant connection(s)
    Can communicate with 1..N clients
    Is an IHttpHandler
    Requires a route to be defined
    Limited to sending messages
    You define the protocol
    public class MyEndPoint : PersistentConnection 
    {
        // blah code
    }
    				

    using System;
    using System.Web.Routing;
    using Microsoft.AspNet.SignalR;
    
    namespace WebApplication1
    {
        public class Global : System.Web.HttpApplication
        {
            void Application_Start(object sender, EventArgs e)
            {
                RouteTable.Routes.MapConnection<MyEndPoint>("echo", 
                    "echo/{*operation}");
            }
        }
    }
    				

    PersistentConnection API

    Task OnConnectedAsync(IRequest request, string connectionId)
    
    Task OnReconnectedAsync(IRequest request, IEnumerable groups, 
    					string connectionId)
    
    IEnumerable OnRejoiningGroups(IRequest request, IEnumerable groups, 
    					string connectionId)
    
    Task OnReceivedAsync(IRequest request, string connectionId, string data)
    
    Task OnDisconnectAsync(IRequest request, string connectionId)
    				

    Code deep dive

    Hub(s)
    Abstraction over PersistantConnection
    Route automatically mapped (/signalr/hubs)
    Can send messages and call methods
    SignalR defines the protocol
    Target all / group / individual client(s)
    public class MyHub : Hub
    {
        // blah code
    }
    				

    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            // Register the default hubs route: ~/signalr/hubs
            RouteTable.Routes.MapHubs();
        }
    }
    				
    C#
    public class MyHub : Hub
    {
         public void Send(string message)
         {
             Clients.All.myMethodOnClient(message);
         }
    }
    				

    JS
    <script src="Scripts/jquery-1.6.2.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.signalR.min.js" type="text/javascript"></script>
    <script src="signalr/hubs" type="text/javascript"></script>
    
    var myHub = $.connection.myHub;
    					
    myHub.server.myMethodOnServer('Hello World from SignalR');
    				

    Windows Azure

    PM> Install-Package Microsoft.AspNet.SignalR.ServiceBus -Pre

    public class MvcApplication : System.Web.HttpApplication
    {
        private string topicPathPrefix = "your_topic";
    
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
    
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
    
            GlobalHost.DependencyResolver.UseServiceBus(
                            ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"], partitionCount: 5, nodeCount: getRoleInstanceCount(), nodeId: getRoleInstanceNumber(), topicPrefix: topicPathPrefix);
        }
    
        private int getRoleInstanceCount()
        {
            return RoleEnvironment.CurrentRoleInstance.Role.Instances.Count;
        }
        private int getRoleInstanceNumber()
        {
                
            var roleInstanceId = RoleEnvironment.CurrentRoleInstance.Id;
            var li1 = roleInstanceId.LastIndexOf(".");
            var li2 = roleInstanceId.LastIndexOf("_");
            var roleInstanceNo = roleInstanceId.Substring(Math.Max(li1, li2) + 1);
            return Int32.Parse(roleInstanceNo);
        }
    
    }
    

    Bleeding Edge!

  • Still a draft
  • Not all browsers support it... duh
  • Not all webservers support it [IIS8 Required]
  • Raw sockets - direct TX/RX of packets
  • Windows Server 2012 / Windows 8
  • IIS8
  • ASP.NET 4.5
  • Questions?

    Links

  • SignalR.net
  • SignalR on Github
  • SignalR on Nuget
  • ShootR
  • Code Project - Group Notifications
  • JabbR SignalR powered chat
  • Damian Edwards @damianedwards
  • David Fowler @davidfowl [very usefull for latest release alpha 2]
  • Fireworks Demo - Setup details for aplha 2 on Azure
  • Thanks... slides borrowed

  • Maarten Balliauw - @maartenballiauw
  • Adam Mokan - @adammokan
  • The End

    Consume Beer!