Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Real time websites and mobile apps with SignalR

Real time websites and mobile apps with SignalR

My session about building real time websites and mobile apps using the SignalR framework. Delivered on Microsoft TechDays Netherlands 2013.

Roy Cornelissen

March 07, 2013
Tweet

More Decks by Roy Cornelissen

Other Decks in Programming

Transcript

  1. Real time websites and mobile apps with SignalR Roy Cornelissen

    Software Architect, Info Support @roycornelissen
  2. http pull interactions client server read file read file processing

    render page lifetime GET default.aspx GET a.jpg GET b.js client 2 server POST “message” client 1 process GET x.ashx?... process POST “ajax” Got msg? process Nope Got msg? process Nope process Got msg? process “message” polling
  3. today‘s push protocols • two way, persistent connection, initiated by

    client • W3C draft, worked on my IETF • support (partial) in some browsers • “pub/sub” like protocol over http • also still a W3C draft • one-way, client needs extra channel for send • proxies need to know about content-type:text/event-stream websocket server sent events
  4. but I want it now! Veruca Salt Willy Wonka &

    The Chocolate Factory, 1971
  5. other options client 2 server POST “message” client 1 Got

    msg? “message” long polling POST “message” Got msg? “message” client 2 server POST “message” client 1 GET /forever <script> display(“message”); </script> forever frame POST “message” <script> display(“message”); </script> <iframe src=“/forever”>
  6. SignalR’s layers of abstraction SignalR will abstract away the actual

    protocol used, and adds a couple of layers on top to make things even easier
  7. protocols persistent connection hub web sockets server events long polling

    forever frame • Unified programming model • Deals with connectivity issues (connection slow, reconnect, disconnects) • Available for multiple types of clients • Messaging bus • Utilizes Json.NET for serialization SignalR 1.0
  8. my connection persistent connection override Task OnConnected override Task OnReceived

    override Task OnDisconnected • Base class for your own connection class • Fires events when clients connect, disconnect or send data • Allows grouping of connections • Deals with “raw” (string) data: do your own serialization • Your class gets instantiated with each new http connection
  9. client/server boundaries fade • hubs let you provide a sematic

    API between client and server • SignalR creates a proxy between the two parties client (javascript) var chat = $.connection.chatHub; … chat.server.message(“hi!”); … chat.client.notify = function(text) { // do something with text } server class ChatHub: Hub { public void message(string text) { Clients.All.notify(text); } } proxy dynamic
  10. sharing state client (javascript) var chat = $.connection.chatHub; chat.message(“test”); ...

    alert(chat.state.yourTicket); server class ChatHub: Hub { public void message(string text) { Clients.Caller.yourTicket = “123”; } } dynamic
  11. app architecture SignalR proxy & data contracts ViewModels WinRT (data

    binding) WPF (data binding) iOS (INotifyPropertyChanged)
  12. beyond websites JavaScript .NET Windows Phone Silverlight WinRT iOS Android

    ASP.NET OWIN Custom SQL Redis Service Bus clients hosts backplanes