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

Socket.io talk

kperch
November 02, 2011

Socket.io talk

This is for the Austin Node.js User group- I'm giving a talk about socket.io.

kperch

November 02, 2011
Tweet

More Decks by kperch

Other Decks in Programming

Transcript

  1. ...well then, you’d be right. Metaphorically speaking. • Think of

    each operator as an app, and the switchboard as a set of sockets. • They wait for codes, with messages attached • These come from either clients or servers • Using the code, they send the message on to the proper place
  2. So this is the basic picture of socket.io [Socket.io] OR

    Other web app/ socket-based technology.
  3. Socket.io isn’t just a node library... • Client Side •

    Provides a shim for sockets • Meaning you can use it with non- html5 browsers. • It does, however, work best on html5 browsers • Node Side • Provides a nice library for socket communication • Easy to use/simple API • API is similar to client side
  4. When would I use socket.io? • When real-time communication in

    your application is necessary: • ...in IE6 or other older browsers • ...in other situations where WebSockets API may not be supported, or when using WebSockets alone is not a solid plan. • Even when WebSockets are being supported, socket.io adds some functionality to your app. • When you want cross-domain communication on any browser
  5. What makes socket.io better than just using the WebSocket API?

    • Features such as heartbeats, timeouts, and disconnection support • These are not in the WebSockets API natively • Polyfills when the WebSocket API is not accessible or supported
  6. Polyfill, you say? • Supports IE 5.5+, Safari 3+, Chrome

    4+, FF 3+ and most mobile browsers • When the client-side library can’t get to web sockets, it moves to things like Flash sockets, AJAX long polling, and JSONP polling, without affecting the API • Meaning you can go about your day without having to worry about supporting several types of communication!
  7. Example Application • Community drawing app • Log in, pick

    a color, start drawing rectangles, and they will show up in all users’ browsers. • URL: http://multiboard.kperch.c9.io • Code: https://github.com/kperch/multi_canvas
  8. A little more about the API • Send() vs Emit()

    • Send() data is treated as a ‘message’ event, emit () data is treated as arguments for a specific event emitter. • .set() and .get() store and retrieve session- relevant data. • .volatile.emit() - it’s ok if the message doesn’t get there (twitter feeds, etc)
  9. Even more about the API • .broadcast added to a

    .emit or .send call sends the message to all sockets except the one that initiated the broadcast • You can pass a callback to any .send or .emit call as the last parameter for acknowledgements, etc.