40
Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com | Developing “Real-Time” Web Applications with SignalR Israel JavaScript Conference Yaniv Yechezkel [email protected] http://www.UpStruct .net

Developing real time web apps with signalR - js-il.com

Embed Size (px)

DESCRIPTION

js-il.com

Citation preview

Page 1: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Developing “Real-Time” Web Applications

with SignalR

Israel JavaScript Conference

Yaniv [email protected]://www.UpStruct.net

Page 2: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Israel JavaScript Conference

Yaniv [email protected]://www.UpStruct.net

Hello.

Page 3: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Agenda Introduction to SignalR Real-Time Web Getting Started with SignalR Simplicity Reach Performance Extensibility Security Reference Project

Page 4: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Introduction to SignalR

Page 5: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

What is ASP.NET SignalR? ASP.NET SignalR is a library for ASP.NET

developers that simplifies the process of adding real-time web functionality to applications.

Real-time web functionality is the ability to have server code push content to connected clients instantly as it becomes available, rather than having the server wait for a client to request new data.

Page 6: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Who is Behind SignalR? Damian Edwards & David Fowler,

Both from Microsoft, started it as a “Side Project”…

Now officially part of ASP.NET.

Open-Source.

Page 7: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Real-Time Web

Page 8: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Real-Time Web But we know the web, and by that we

mean HTTP, is based on the Request/Reply Paradigm, right?

Response

Request

Page 9: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Real-Time Web Real-Time: Web UI that updates in real-

time

Server Push: Push messages to connect clients

COMET: An umbrella for existing HTTP push techniques

HTTP Streaming: Another name for COMET

Page 10: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

HTML 5 Changes The Rules of The Game

Page 11: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Web Socket Protocol Handshake

Page 12: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Getting Started

Page 13: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Get The Bytes Get Visual Studio 2012 Update 2

(VS2012.2)http://www.microsoft.com/en-us/download/details.aspx?id=38188

Download via NuGet

Or, get the Source from GitHubhttps://github.com/SignalR/SignalR

install-package Microsoft.AspNet.SignalR

Page 14: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Stock Ticker Sample

Page 15: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Moving Shape

Page 16: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

What is ASP.NET SignalR? Real-Time, persistent connection

abstractionover HTTP for .NET.

Supported Transports/Techniques

- Web Sockets- Server Sent Events (Event Source)- Forever Frame- Ajax Long Polling

3 Pillars – Simplicity, Reach, Performance

Page 17: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Not Just for Chat Dashboards and Monitoring Collaboration of any kind Job Progress Gaming More…

Page 18: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Examples ShootR (Shooting Game)

http://shootr.signalr.net

JabbR (Chat)https://jabbr.net

BromeLard (Collaborative Maps)http://www.bromelard.com

Morehttps://github.com/SignalR/SignalR/wiki/Projects-Using-SignalR

Page 19: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Simplicity: Two Level API Connections

- Low Level

Hubs- Built on top of connections- RPC from client-server and server-client- Automatic Proxy generation

Choose the level of abstraction that works for you.

Page 20: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Hubs – An RPC IllusionPublisher (Caller)

Subscriber (Others)

hub.server.moveShape(x, y);

hub.client.shapeMoved = function (x, y) { shape.css({left: x,top: y});}

Hub

[HubMethodName("moveShape")]public void MoveShape(int x, int y){ Clients.Others.shapeMoved(x, y);}

Page 21: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Auto-Generated Proxy<script src="/ signalr/hubs"></script>

HubConfiguration cfg = new HubConfiguration(){ EnableJavaScriptProxies = true};

RouteTable.Routes.MapHubs(“~/signalr”, cfg );

Page 22: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Reach: What about Web Sockets? ASP.NET 4.5 on Windows Server 2012 IE 10 or Latest Chrome, FF, Safari Load Balancer/Reverse Proxy supports it Client Proxy server/NAT supports it Everybody in between supports it You find programming raw sockets is fun You manage scaleout yourself

Page 23: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Reach: What about Web Sockets?

Web Browser Transport Requirements

http://www.asp.net/signalr/overview/getting-started/supported-platforms

Page 24: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Reach: SignalR Works Everywhere Tries Web Sockets, but then fallback to:

- Server Sent Events (Event Source)- Forever Frame- Ajax Long Polling

Powerful API

Holds the connection opened.

Scales-Out

Page 25: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Reach: Not Just Web Clients JavaScript (jQuery) .NET 4.0/4.5 Silverlight 5 Windows Store Apps Windows Phone 8 iOS More coming…

Page 26: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Transports

Page 27: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Performance

Page 28: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Performance: Throughput & Scale On a single Extra Large Windows Azure

Box~500,000 Msg/Sec

On a single Box>100,000 Concurrent Connections

Client Load Generatorhttps://github.com/SignalR/SignalR/wiki/Performance

Page 29: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Performance: Scale-Out

3 Supported Backplanes- Windows Azure Service Bus- Redis (In-Memory Pub/Sub)- SQL Server

Page 30: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Extensibility

Page 31: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Dependency Injection Derive from DefaultDependencyResolver

Found In Microsoft.AspNet.SignalR.

Replace Resolver before initializing the Hub

GlobalHost.DependencyResolver = new SignalRUnityResolver(m_Container);

RouteTable.Routes.MapHubs(cfg );

Page 32: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Hub Pipeline Much like MVC Filters, or HTTP Modules,

You can intercept the Hub Pipeline…public class TraceModule: HubPipelineModule{ protected override void OnAfterConnect(IHub hub) { Debug.WriteLine(hub.Context.ConnectionId); base.OnAfterConnect(hub); }}

GlobalHost.HubPipeline.AddModule( new TraceModule());

Page 33: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Security

Page 34: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

How to Handle Security? Implement the Authentication outside

the Hub. For example, in an MVC Controller.

As the Hub is actually running under same HttpContext, any Principal your place in the Pipeline will be available, and thus enforced. [Authorize(Roles = "LiveId")][HubMethodName("chat")]public void Chat(ChatMessage message){}

Page 35: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Reference Project

Page 36: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Mobile Social Online Game

Page 37: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Summary Easy API to get start with.

Supported on Windows Azure VM’s and Cloud Services.

Not limited just to Web Clients.

Real-Time becomes a real thing.

Page 38: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Resources Official Site

http://www.asp.net/signalr

WIKIhttps://github.com/SignalR/SignalR/wiki

JabbRhttps://jabbr.net

ASP.NET SignalR Book (free)http://www.campusmvp.net/signalr-ebook

Page 39: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

www.js-il.com

18.6.13

Page 40: Developing real time web apps with signalR - js-il.com

Israel JavaScript Conference | 03 – 6325707 | [email protected] | www.js-il.com |

Thanks

Yaniv [email protected]://www.UpStruct.net