Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

ASP.NET MVC 5 and SignalR 2

1,621 views

Published on

ASP.NET MVC 5 and SignalR 2

Published in: Technology
  • Be the first to comment

  • Be the first to like this

ASP.NET MVC 5 and SignalR 2

  1. 1. ASP.NET MVC 5 and SignalR 2 Jaliya Udagedara MVP – Visual Studio and Development Technologies
  2. 2. Line Up • What is SignalR • Transports • Supported Platforms • Connections and Hubs • Demo • Hubs API Guide • Server (C#) • Client (JavaScript) • Understanding Lifetime Events • Security • Performance • What’s Next: ASP.NET Core SignalR
  3. 3. What is SignalR • Library for adding REAL-TIME web functionality to applications • Candidates for SignalR, • User needs to do a refresh to see new data • Page implements long polling to retrieve new data etc. • Possible applications?
  4. 4. Transports • HTML 5 Transports • WebSocket • Server Sent Events (AKA EventSource) • Comet Transports (long-held HTTP requests) • Forever Frame • Ajax Long Polling
  5. 5. Transports contd. • SignalR starts as HTTP, then if possible promoting to WebSockets • Transport Selection Process • If the browser is IE 8 or earlier then Long Polling • If JSONP is configured then Long Polling • If Cross Domain AND Client Supports WebSockets AND Server Supports WebSockets then WebSockets Else then Long Polling • If JSONP is NOT configured AND NOT Cross Domain AND Client Supports WebSockets AND Server Supports WebSockets then WebSockets Else then Long Polling
  6. 6. Transports contd. • Transport Selection Process contd. • If Client or Server does NOT support WebSockets If Server Side Events is NOT Available If Forever Frame is NOT Available then Long Polling Else then Forever Frame Else then Server Side Events Else WebSockets
  7. 7. Transports contd. • Specifying a Transport // fallback order from left to right connection.start( { transport: ['webSockets’, 'longPolling’] }); // string constants: webSockets, serverSentEvents, foreverFrame, longPolling
  8. 8. Supported Platforms • Server System Requirements • Windows Server 2008 r2, Windows 7 or above • .NET Framework 4.5 or above • WebSockets • Windows Server 2012, Windows 8 or above • IIS 8 or IIS 8 Express • WebSocket must be enabled in IIS • Client System Requirements • Web Browsers • Windows Desktop and Silverlight Applications • Windows Store and Windows Phone Applications • Visit the documentation
  9. 9. Connections and Hubs • The SignalR API contains two models for communicating between clients and servers • Persistent Connections • Hubs • Choosing a communication model • Most applications should use the Hubs • More advance, then Persistent Connections • Today, we will be moving ahead with Hubs
  10. 10. Demo • Create a Server Application • Install SignalR • Map SignalR to app builder pipeline • Add a Hub • Creating a Client Application • Install SignalR Client (JS, .NET, Java etc.) and add references • Start the connection • Start Cracking
  11. 11. Hubs API Guide: Server (C#) • Adding SignalR to app builder pipeline • app.MapSignalR() • Overloads • Can configure URL • Enable CORS • Enable detailed error messages • Disable automatically generated JavaScript proxy files • Make sure that the SignalR route is added before the other routes
  12. 12. Hubs API Guide: Server (C#) contd. • Object Lifetime • Transient • How to maintain state • Static variable on the Hub class • Different class • Database • Defining Methods • Defining Overloads
  13. 13. Hubs API Guide: Server (C#) contd. • Call client methods from the Hub class • Selecting which clients will receive the RPC • All • Caller • Others • Client(connectionId) • Clients(ConnectionIds) • AllExcept(connectionId1, connectionId2) • Group(groupName) • Group(groupName, connectionId1, connectionId2) contd.
  14. 14. Hubs API Guide: Server (C#) contd. • Selecting which clients will receive the RPC contd. • OthersInGroup(groupName) • User(userid) • Groups(groupIds) • Client(userName) • Users(new string[] { "myUser", "myUser2" }) • Groups • A group can have any number of clients, and a client can be a member of any number of groups
  15. 15. Hubs API Guide: Server (C#) contd. • Connection lifetime events • OnConnected • OnReconnected • OnDisconnected • Passing state between Client and Server • Client Side (JavaScript) • myHub.state.userName = “John Smith"; • Hub • string userName = Clients.Caller.userName;
  16. 16. Hubs API Guide: Client (JavaScript) • Generated Proxy • {endpoint}/hubs => signalr/hubs • Dynamically generated JavaScript code, not to a physical file • Can create a physical file if you prefer to • You can still communicate with SignalR Service without a Proxy
  17. 17. Hubs API Guide: Client (JavaScript) contd. • Establishing a connection • Create a connection proxy var myHub = $.connection.myHub; • Register event handlers for methods that can be called from the server myHub.client.addMessageToPage = function() { … } • Start connection $.connection.hub.start().done(function(){ // will get called after OnConnected on Server })
  18. 18. Hubs API Guide: Client (JavaScript) contd. • Connection Lifetime Events • starting • received • connectionSlow • reconnecting • reconnected • stateChanged • disconnected
  19. 19. Understanding Lifetime Events • Connection Types • SignalR connection • Transport connection • Physical connection • Disconnection Scenarios • Transport disconnection scenarios • Client disconnection scenarios • Server disconnection scenarios
  20. 20. Security • Authorize hub or method • [Authorize] • [Authorize(Roles = "Admin,Manager")] • [Authorize(Users = "user1,user2")] • [Authorize(RequireOutgoing=false)] • Existing Authentication Mechanism • Connection Token (Connection ID and Username) • CSRF (Cross-Site Request Forgery) • CSRF mitigations taken by SignalR • Disable cross domain requests • Pass connection token in query string, not cookie • Verify connection token
  21. 21. Performance • Scale Up vs Scale Out • Scale Out • Azure Service Bus • Redis • SQL Server • Backplane
  22. 22. What’s Next: ASP.NET Core SignalR • Reimagined as ASP.NET Core Sockets • Removed: • No more jQuery dependency • No more auto-reconnect with message replay • No more Hub-state • No more multi hub endpoints • No more single-model scale-out • No more multi server ping-pong (now require sticky connections)
  23. 23. What’s Next: ASP.NET Core SignalR contd. • Introduced: • Binary data support • Host agnostic (Non HTTP Transports) • All new connection level “Endpoints” API • Multiple protocols/formats support, JSON, Protobuf, Custom etc. • Support “pure” WebSocket clients • Return results from client methods invocation • TypeScript client • Flexible scale-out extensibility
  24. 24. Resources • SignalR 2 Documentation • Sample Code
  25. 25. Q & A

×