Polling. Use a javascript timer and make XMLHttpRequests (XHR) every few seconds to look for changes. • New TCP Connections (Overhead) • Full Request (Cookies, Headers, etc) • Time Lag • Tracking which messages have already been processed
the client and server. • Steaming ◦ Hidden IFrame on page ("forever frame") which fills up with script tags that are processed incrementally. ◦ XMLHttpRequest accepting multipart response from server--callback processes each part that is sent. • AJAX with Long Polling ◦ XMLHttpRequest made and waits until it receives data. Rinse and Repeat. Hacky.
full-duplex communications channels over a single TCP connection." - Wikipedia • Initiated using HTTP then "upgraded" • Its own protocol -- based on TCP ◦ Sends messages instead of bytes ◦ Bi-directional
Node (non-blocking, event-driven) handles the websocket connections better than the PHP based solutions (blocking). • Socket.io (http://socket.io/). A Node.js library that abstracts away the WebSocket implementation and provides fallback for browsers with partial (or no) support. • Free • Easy
Event-driven I/O server-side JavaScript environment based on the V8 engine. Redis. Advanced key-value store that offers publish/subcribe functionality.
Page returned with socket.io js script tag. 3. Node.js web server serves the content of this file. 4. Javascript file sent back to the client. 5. Create websocket connection!
state of our application changes (ie. Todos are created, updated, or deleted). Redis is primarily a key-value data store. Most importantly it allows us to listen for certain events (subscribe) and publish events (well, uh, publish).
the Redis Server 1. User makes a request to the ToDo app (create, update, delete) 2a. ToDo app responds with an updated list. 2b. ToDo app PUBLISHES changes to Redis Server. 3. Node.js receives the message it was waiting for.
> io.sockets.send('Hey!'); emit - send custom event with data & callback > io.sockets.emit('type', data, function (data) { console.log('they got it'); }); broadcast - send to all the other sockets. > io.socket.broadcast.send('Hey!', data); volatile - it's ok if a message is dropped. > io.socket.volatile.send('It is ok if you miss this', data); "Rooms" (Projects, Boards, Lists) > io.socket.join("todo-list-12") (or 'leave') > io.sockets.in("todo-list-12").emit('itemAdded', data );
and handle messages sent back down the socket. var Socket = io.connect('http://localhost: 8080'); Socket.on('message', function (message) { console.log('Hello. Yes, This is dog.'); $('body').html('Message received'); });
access? ▪ Does user have access to Board # 12? ◦ Managing different channels (rooms) ▪ Each socket only concerned with their Board. ◦ Collision ▪ If user adds item, don't add it again. ▪ Broadcast vs. Emit ◦ SSL • Client ◦ Backbone ◦ Ember ◦ Angular