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

WebSockets Intro - Riverside Ruby Group - 03/2016

WebSockets Intro - Riverside Ruby Group - 03/2016

An introduction into WebSockets benefits and drawbacks

kickinbahk

March 17, 2016
Tweet

More Decks by kickinbahk

Other Decks in Technology

Transcript

  1. Standard Rest Transaction 1. Open a socket port to 80

    on meetup.com 2. Send an Http header request to the server (Apache/Nginx) 3. This buffers the message 4. Message is sent to server apppcation 5. Server application decides what to do with request
  2. Standard Rest Transaction (cont) 6. Fetches Data 7. Generates HTML

    8. Sends it back to the server (Apache/Nginx) 9. Appropriate HTTP headers are added to the body 10. Sent back to browser and connection closed
  3. v a r c o n n e c t

    i o n = n e w W e b S o c k e t ( u r l , [ p r o t o c o l ] ) ; Ex: v a r c o n n e c t i o n = n e w W e b S o c k e t ( ' w s : / / h t m l 5 r o c k s . w e b s o c k e t . o r g / e c h o ' , [ ' s o a p ' , ' x m p p ' ] ) ;
  4. Events / / W h e n t h e

    c o n n e c t i o n i s o p e n , / / s e n d s o m e d a t a t o t h e s e r v e r c o n n e c t i o n . o n o p e n = f u n c t i o n ( ) { / / S e n d t h e m e s s a g e ' P i n g ' t o t h e S e r v e r c o n n e c t i o n . s e n d ( ' P i n g ' ) ; } ; / / L o g e r r o r s c o n n e c t i o n . o n e r r o r = f u n c t i o n ( e r r o r ) { c o n s o l e . l o g ( ' W e b S o c k e t E r r o r ' + e r r o r ) ; } ;
  5. Events (cont.) / / R e c e i v

    e d a t a f r o m t h e s e r v e r c o n n e c t i o n . o n m e s s a g e = f u n c t i o n ( e ) { c o n s o l e . l o g ( ' S e r v e r : ' + e . d a t a ) ; } ; / / C l o s e c o n n e c t i o n c o n n e c t i o n . o n c l o s e = f u n c t i o n ( c ) { c o n s o l e . l o g ( ' C o n n e c t i o n : c l o s e d . . . ' + c ) } ;
  6. Sending Data / / S e n d i n

    g a s t r i n g c o n n e c t i o n . s e n d ( ' y o u r m e s s a g e ' ) ; / / S e n d i n g c a n v a s I m a g e D a t a a s A r r a y B u f f e r v a r i m g = c a n v a s _ c o n t e x t . g e t I m a g e D a t a ( 0 , 0 , 4 0 0 , 3 2 0 ) ; v a r b i n a r y = n e w U i n t 8 A r r a y ( i m g . d a t a . l e n g t h ) ; f o r ( v a r i = 0 ; i < i m g . d a t a . l e n g t h ; i + + ) { b i n a r y [ i ] = i m g . d a t a [ i ] ; } c o n n e c t i o n . s e n d ( b i n a r y . b u f f e r ) ; / / S e n d i n g f i l e a s B l o b v a r f i l e = d o c u m e n t . q u e r y S e l e c t o r ( ' i n p u t [ t y p e = " f i l e " ] ' ) . f i l e s [ c o n n e c t i o n . s e n d ( f i l e ) ;
  7. Difficult to Implement Difficult to Debug No Connection Limitation Complicated

    Load Balancing Illusion of Reliability Lack of Browser Support