SignalR. Code, not toothpaste

33
SignalR. Code, not toothpaste Realtime client/server communication Maarten Balliau w Technical Consultant RealDolmen @maartenballiau w

description

Almost all HTML5 and web applications today make use of AJAX to communicate between client and server. AJAX is great with asynchronous operations, but what about real-time communication? How would you push a message from server to client? HTML5 WebSockets offer a solution to all of these questions but are not supported in every browser and every network environment. SignalR is the new cool kid on the block: it leverages WebSockets when supported and falls back to different mechanisms when they aren’t. In this session, I’ll show you where and how to use SignalR to create rich real-time applications in the browser. No toothpaste. Just code.

Transcript of SignalR. Code, not toothpaste

Page 1: SignalR. Code, not toothpaste

SignalR. Code, not toothpasteRealtime client/server communication

Maarten BalliauwTechnical ConsultantRealDolmen

@maartenballiauw

Page 2: SignalR. Code, not toothpaste

R

Page 3: SignalR. Code, not toothpaste

Maarten Balliauw

@maartenballiauw

Who am I?

Web & cloudWindows Azure

Page 4: SignalR. Code, not toothpaste

Agenda

Why real-time & how?Meet SignalRConnections and HubsClientsQ&A

Page 5: SignalR. Code, not toothpaste

Why real-time & how?

Page 6: SignalR. Code, not toothpaste

Users want the latest info, NOW!Twitter – live searches/updates Stock streamersAuctionsLive scoresReal-time notificationsInteractive gamesCollaborative appsLive user analytics…

6

Page 7: SignalR. Code, not toothpaste

HTTP is an old beast…

Never designed for real-time communicationsWeb is request-responseWeb is stateless

HTML5 WebSockets to the rescue, right?

Page 8: SignalR. Code, not toothpaste

HTML5 WebsocketsExtension to HTTP

Provide raw sockets over HTTP

Full-duplex

Traverses proxies

It’s still a draft…

Not every proxy server supports it

Not every webserver supports it

Not every browser supports it

They are raw sockets!

Page 9: SignalR. Code, not toothpaste

HTTP/1.1 200 OKContent-Type: text/plainTransfer-Encoding: chunked

Forever Frame

Server tells client that response is chunckedClient keeps connection open untill server closes itServer pushes data to the client followed by \0Consumes server threads

Client<script>eval("... ")</script>\0

<script>eval("... ")</script>\0

Server

Page 10: SignalR. Code, not toothpaste

Periodic polling

Poll from time to time using AjaxDelay in communications due to polling intervalWastes bandwidth & latency

Polling interval

Client

Server

Fiddlerhootsuite.com

Page 11: SignalR. Code, not toothpaste

Long polling

Poll but don’t respond untill there’s dataPoll again after data received or after the connection times outConsumes server threads & connection resources

Client

Server

Fiddlerfacebook.com

Page 12: SignalR. Code, not toothpaste

Options!Forever FramePeriodic pollingLong pollingHTML5 WebSockets(Server-Sent events)

Page 13: SignalR. Code, not toothpaste

Meet SignalR

ARRR!

Page 14: SignalR. Code, not toothpaste

SignalR?

Three-in-one!“Persistent” client/server connection over best transportAbstracts away the transportProvides just one programming model

Page 15: SignalR. Code, not toothpaste

SignalR?David Fowler and Damian Edwards(two guys on the ASP.NET team)

Not an official Microsoft project (yet?)OSS project on Github, MIT licensedhttp://github.com/signalr/signalr

Simple to setup & just worksDepends on C# (not VB.NET), .NET 4+ and jQuery

Page 16: SignalR. Code, not toothpaste

Where do I get it?

where else!

http://amzn.to/xrzS6j

Page 17: SignalR. Code, not toothpaste

Hello, SignalRdemo

Page 18: SignalR. Code, not toothpaste

BONUS DEMO!Knockout.js & SignalR

Page 19: SignalR. Code, not toothpaste

What just happened?The server is broadcasting a message every few secondsClients are receiving messagesCode looks easyNo polling or whatsoever (at least not in my code)

Page 20: SignalR. Code, not toothpaste

Connections and Hubs

Page 21: SignalR. Code, not toothpaste

Two connection modelsPersistentConnectionCan communicate with 1..N clientsIs an IHttpHandlerRequires a route to be definedLimited to sending messagesYou define the “protocol”

HubsCan communicate with 1..N clientsAbstraction over PersistentConnectionRoute automatically mapped (/signalr/hubs)Can send messages and call methodsSignalR defines the protocol

Page 22: SignalR. Code, not toothpaste

Hello, SignalR hubsdemo

Page 23: SignalR. Code, not toothpaste

HubsHub methods can be called from clientClient methods can be called from hubTarget individual clientTarget all clientsTarget group of clients

http://jabbr.net

Page 24: SignalR. Code, not toothpaste

Clients

Page 25: SignalR. Code, not toothpaste

So far we’ve used…

On the server sideHost in any ASP.NET application (SignalR.Server)

On the client sideJavaScript (SignalR.JS)

But there’s more…

Page 26: SignalR. Code, not toothpaste

There’s a lot more!

On the server sideHost in any ASP.NET application (SignalR.Server)Use “SelfHost” - https://github.com/SignalR/SignalR/tree/master/SignalR.SelfHost Use Windows Azure

On the client sideJavaScript (SignalR.JS)Any .NET client (SignalR.Client)Any WP7 device (SignalR.Client.WP7)iOSAndroid

Is this a replacement for WCF?

Page 27: SignalR. Code, not toothpaste

DeckCastdemo

Page 28: SignalR. Code, not toothpaste

Session Summary

SignalR is three-in-one!“Persistent” client/server connection over best transportAbstracts away the transportProvides just one programming model

Connections & HubsConnect various clients

Page 29: SignalR. Code, not toothpaste

Make the web realtime!Install-Package SignalR

Page 30: SignalR. Code, not toothpaste

Be what’s next• Find everything here

http://aka.ms/mbl-tech• Visual Studio Developer Preview Downloads

http://aka.ms/mbl-tech/devprev• MSDN HTML5 Developer Center

http://aka.ms/mbl-tech/html5

Page 31: SignalR. Code, not toothpaste

Maarten Balliauw

@maartenballiauwQ&A

Page 32: SignalR. Code, not toothpaste

© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Page 33: SignalR. Code, not toothpaste

ResourcesMy bloghttp://blog.maartenballiauw.be SignalR on Githubhttp://github.com/signalr/signalr SignalR on NuGethttp://www.nuget.org/packages?q=signalr Websocketshttp://github.com/signalr/signalr.websocketsScale-out on Windows Azurehttp://vasters.com/clemensv/2012/02/13/SignalR+Powered+By+Service+Bus.aspxObjective C clienthttps://github.com/DyKnow/SignalR-ObjCAndroid client (using MonoDroid)https://github.com/SignalR/SignalR/pull/127