SignalR: Add real-time to your applications

1,448 views
1,394 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,448
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

SignalR: Add real-time to your applications

  1. 1. SignalR: Add real-time to yourapplicationsby Eugene Zharkov
  2. 2. Why to use SignalR?https://github.com/SignalR/SignalR2
  3. 3. Transports• WebSockets (the only true full-duplex)• Server Sent Events• Forever Frame• Long polling
  4. 4. WebSockets• html5 feature• works on top of TCP protocol• full-duplex• low latency, low overhead• you need >IE10 and IIS8• not all browsers / servers/ proxies support it
  5. 5. Server Sent Events• requires a single connection between client-server• uses JS API EventSource through which client can request a particularURL to receive data stream• no need to reconnect• works in server-2-client direction only
  6. 6. Forever Frames• only for IE• hidden iframes are in the load loop with chunks of data6
  7. 7. Long Polling• high overhead on requests/response (headers etc.)• medium latency7
  8. 8. Transports8
  9. 9. Clients• iOS (SignalR-ObjC)• Android (SignalA)• Windows RT / Phone• jQuery, Backbone• Mono• QT (SignalR-QT)9
  10. 10. Requirements• Windows Server 2008 R2/2012, Windows 7/8• IIS 8. IIS 7 and 7.5. Support for extensionless URLs is required.• >= .NET 3.5, WinRT, Silverlight10
  11. 11. Does NOT support IE 6/711
  12. 12. Persistent Connection• the raw connection• IHTTP handler (OnConnection, OnDisconnection, OnReconnection)• custom logic must be implemented12
  13. 13. Hub• high-level API on top of Persistent Connection• route automatically mapped• clients can be divided by groups13
  14. 14. Add to project14
  15. 15. New References and Scripts15
  16. 16. Hub registration : C#public class MvcApplication : System.Web.HttpApplication{protected void Application_Start(){RouteTable.Routes.MapHubs();AreaRegistration.RegisterAllAreas();WebApiConfig.Register(GlobalConfiguration.ConfiguratFilterConfig.RegisterGlobalFilters(GlobalFilters.Fil01.02.03.04.05.06.07.08.16
  17. 17. Hub object | C#public class Documents : Hub{public void Send(string documentName){Clients.All.addMessage(documentName);}}01.02.03.04.05.06.07.17
  18. 18. Subscribe to Hub messages | JSvar documentHub = $.connection.documents;documentHub.client.addMessage = function (documentName) {$("#documents").append("<li>" + documentName + "</li>");};01.02.03.04.18
  19. 19. Hub object | JS$.connection.hub.logging = true;$.connection.hub.start().done(function () {$("#addDocument").click(function () {documentHub.server.send($("#document").val());});});01.02.03.04.05.06.19
  20. 20. Working with Hub | WinRTvar connection = new HubConnection("http://localhost:59576");var hub = connection.CreateHubProxy("documents");hub.On("addMessage", message =>Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() =>{DocumentsCounter.Text = (++_newDocuments).ToString();}));01.02.03.04.05.06.07.20
  21. 21. Working with Hub | Android (SignalA)HubConnection con = new HubConnection("http://localhost:59576this, new LongPollingTransport())IHubProxy hub = con.CreateHubProxy("documents");hub.On("addMessage", new HubOnDataCallback() {@Overridepublic void OnReceived(.. args) {{ //... }});con.Start();01.02.03.04.05.06.07.21
  22. 22. Thats all folks• @2j2e• eu.zharkov@gmail.com• http://bit.ly/signalr022

×