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

SignalR: Add real-time to your applications

SignalR: Add real-time to your applications

Is SignalR a rocket science? Nope. It's fast to integrate, simple to use, on any platform.

Avatar for Eugene Zharkov

Eugene Zharkov

June 06, 2013

Other Decks in Programming

Transcript

  1. 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
  2. Server Sent Events • requires a single connection between client-server

    • uses JS API EventSource through which client can request a particular URL to receive data stream • no need to reconnect • works in server-2-client direction only
  3. Forever Frames • only for IE • hidden iframes are

    in the load loop with chunks of data 6
  4. Clients • iOS (SignalR-ObjC) • Android (SignalA) • Windows RT

    / Phone • jQuery, Backbone • Mono • QT (SignalR-QT) 9
  5. 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, Silverlight 10
  6. Persistent Connection • the raw connection • IHTTP handler (OnConnection,

    OnDisconnection, OnReconnection) • custom logic must be implemented 12
  7. Hub • high-level API on top of Persistent Connection •

    route automatically mapped • clients can be divided by groups 13
  8. Hub registration : C# p u b l i c

    c l a s s M v c A p p l i c a t i o n : S y s t e m . W e b . H t t p A p p l i c a t i o n { p r o t e c t e d v o i d A p p l i c a t i o n _ S t a r t ( ) { R o u t e T a b l e . R o u t e s . M a p H u b s ( ) ; A r e a R e g i s t r a t i o n . R e g i s t e r A l l A r e a s ( ) ; W e b A p i C o n f i g . R e g i s t e r ( G l o b a l C o n f i g u r a t i o n . C o n f i g u r a t F i l t e r C o n f i g . R e g i s t e r G l o b a l F i l t e r s ( G l o b a l F i l t e r s . F i l 0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 16
  9. Hub object | C# p u b l i c

    c l a s s D o c u m e n t s : H u b { p u b l i c v o i d S e n d ( s t r i n g d o c u m e n t N a m e ) { C l i e n t s . A l l . a d d M e s s a g e ( d o c u m e n t N a m e ) ; } } 0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 17
  10. Subscribe to Hub messages | JS v a r d

    o c u m e n t H u b = $ . c o n n e c t i o n . d o c u m e n t s ; d o c u m e n t H u b . c l i e n t . a d d M e s s a g e = f u n c t i o n ( d o c u m e n t N a m e ) { $ ( " # d o c u m e n t s " ) . a p p e n d ( " < l i > " + d o c u m e n t N a m e + " < / l i > " ) ; } ; 0 1 . 0 2 . 0 3 . 0 4 . 18
  11. Hub object | JS $ . c o n n

    e c t i o n . h u b . l o g g i n g = t r u e ; $ . c o n n e c t i o n . h u b . s t a r t ( ) . d o n e ( f u n c t i o n ( ) { $ ( " # a d d D o c u m e n t " ) . c l i c k ( f u n c t i o n ( ) { d o c u m e n t H u b . s e r v e r . s e n d ( $ ( " # d o c u m e n t " ) . v a l ( ) ) ; } ) ; } ) ; 0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 19
  12. Working with Hub | WinRT v a r c o

    n n e c t i o n = n e w H u b C o n n e c t i o n ( " h t t p : / / l o c a l h o s t : 5 9 5 7 6 " ) ; v a r h u b = c o n n e c t i o n . C r e a t e H u b P r o x y ( " d o c u m e n t s " ) ; h u b . O n ( " a d d M e s s a g e " , m e s s a g e = > D i s p a t c h e r . R u n A s y n c ( C o r e D i s p a t c h e r P r i o r i t y . N o r m a l , ( ) = > { D o c u m e n t s C o u n t e r . T e x t = ( + + _ n e w D o c u m e n t s ) . T o S t r i n g ( ) ; } ) ) ; 0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 20
  13. Working with Hub | Android (SignalA) H u b C

    o n n e c t i o n c o n = n e w H u b C o n n e c t i o n ( " h t t p : / / l o c a l h o s t : 5 9 5 7 6 t h i s , n e w L o n g P o l l i n g T r a n s p o r t ( ) ) I H u b P r o x y h u b = c o n . C r e a t e H u b P r o x y ( " d o c u m e n t s " ) ; h u b . O n ( " a d d M e s s a g e " , n e w H u b O n D a t a C a l l b a c k ( ) { @ O v e r r i d e p u b l i c v o i d O n R e c e i v e d ( . . a r g s ) { { / / . . . } } ) ; c o n . S t a r t ( ) ; 0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 21