message passing vs. data synchronization
DESCRIPTION
A Pivotal tech talk covering the advantages and disadvantages of using a message passing approach vs. data synchronization for building modern, distributed web applications. Video: http://pivotallabs.com/anant-narayanan-firebase/TRANSCRIPT
![Page 1: Message Passing vs. Data Synchronization](https://reader034.vdocuments.mx/reader034/viewer/2022051013/5483ebe1b07959330c8b4a4f/html5/thumbnails/1.jpg)
Message Passingvs.
Data SynchronizationAnant Narayanan
Pivotal Labs Tech TalkMay 14, 2013
![Page 2: Message Passing vs. Data Synchronization](https://reader034.vdocuments.mx/reader034/viewer/2022051013/5483ebe1b07959330c8b4a4f/html5/thumbnails/2.jpg)
Realtime Synchronization Network
• Build apps fast without managing servers
• Data persistence, but in realtime
• With authentication & security rules, can serve as the entire backend
![Page 3: Message Passing vs. Data Synchronization](https://reader034.vdocuments.mx/reader034/viewer/2022051013/5483ebe1b07959330c8b4a4f/html5/thumbnails/3.jpg)
Why?
• Users don’t like waiting
• Users want their data wherever they are
• Between all their devices
• And sometimes their friends
• & are quickly becoming obsolete
![Page 4: Message Passing vs. Data Synchronization](https://reader034.vdocuments.mx/reader034/viewer/2022051013/5483ebe1b07959330c8b4a4f/html5/thumbnails/4.jpg)
“AJAX”
![Page 5: Message Passing vs. Data Synchronization](https://reader034.vdocuments.mx/reader034/viewer/2022051013/5483ebe1b07959330c8b4a4f/html5/thumbnails/5.jpg)
Not Good Enough
• You still need to know when to fetch changes
• Or, you need to keep asking about what’s new
![Page 6: Message Passing vs. Data Synchronization](https://reader034.vdocuments.mx/reader034/viewer/2022051013/5483ebe1b07959330c8b4a4f/html5/thumbnails/6.jpg)
Long Polling
![Page 7: Message Passing vs. Data Synchronization](https://reader034.vdocuments.mx/reader034/viewer/2022051013/5483ebe1b07959330c8b4a4f/html5/thumbnails/7.jpg)
• Bidirectional channel between client & server
• Upgrade after initiating a HTTP connection
• Don’t close until you need to
![Page 8: Message Passing vs. Data Synchronization](https://reader034.vdocuments.mx/reader034/viewer/2022051013/5483ebe1b07959330c8b4a4f/html5/thumbnails/8.jpg)
How will you use it?
var channel = pusher.subscribe('my-channel');channel.bind('my-event', function(data) { alert('Received my-event with: ' + data.message);});
pusher.trigger('my-channel', 'my-event', { "message": "hello world" } );
PUBNUB.subscribe({ channel: "my_channel", message: function(m){alert(m)}});
PUBNUB.publish({ channel: "my_channel", message: "Hello World"});
![Page 9: Message Passing vs. Data Synchronization](https://reader034.vdocuments.mx/reader034/viewer/2022051013/5483ebe1b07959330c8b4a4f/html5/thumbnails/9.jpg)
Example: Chatvar box = PUBNUB.$('box'), input = PUBNUB.$('input');var channel = 'chat';
PUBNUB.subscribe({ channel: channel, callback: function(text) { box.innerHTML = (''+text).replace( /[<>]/g, '' ) + '<br>' + box.innerHTML; }});
PUBNUB.bind('keyup', input, function(e) { (e.keyCode || e.charCode) === 13 && PUBNUB.publish({ channel: channel, message: input.value, x: (input.value='') });});
![Page 10: Message Passing vs. Data Synchronization](https://reader034.vdocuments.mx/reader034/viewer/2022051013/5483ebe1b07959330c8b4a4f/html5/thumbnails/10.jpg)
An Alternative Approachvar chatRef = new Firebase('https://chat.firebaseio-demo.com/');
$('#messageInput').keypress(function(e) { (e.keyCode == 13) && chatRef.push({ name: $('#nameInput').val(), text: $('#messageInput').val() }) && $('#messageInput').val('');});
chatRef.limit(10).on('child_added', function(s) { var message = s.val(); $('<div/>').text(message.text).prepend($('<em/>') .text(message.name+': ')) .appendTo($('#messagesDiv'));});
![Page 11: Message Passing vs. Data Synchronization](https://reader034.vdocuments.mx/reader034/viewer/2022051013/5483ebe1b07959330c8b4a4f/html5/thumbnails/11.jpg)
Why Data Synchronization?
There’s a lot of complexity in turning astream of messages into state that is usable
Simpler Client Logic
![Page 12: Message Passing vs. Data Synchronization](https://reader034.vdocuments.mx/reader034/viewer/2022051013/5483ebe1b07959330c8b4a4f/html5/thumbnails/12.jpg)
Why Data Synchronization?
We have the flexibility to combine operations
Greater Efficiency
![Page 13: Message Passing vs. Data Synchronization](https://reader034.vdocuments.mx/reader034/viewer/2022051013/5483ebe1b07959330c8b4a4f/html5/thumbnails/13.jpg)
Why Data Synchronization?
With message passing, conflict resolution can gettricky, but with data synchronization it can be a
core primitive
Automatic Merge Behavior
![Page 14: Message Passing vs. Data Synchronization](https://reader034.vdocuments.mx/reader034/viewer/2022051013/5483ebe1b07959330c8b4a4f/html5/thumbnails/14.jpg)
Why Data Synchronization?
Message passing is simply a subset ofdata synchronization
Greater Flexibility
![Page 15: Message Passing vs. Data Synchronization](https://reader034.vdocuments.mx/reader034/viewer/2022051013/5483ebe1b07959330c8b4a4f/html5/thumbnails/15.jpg)
Example: CounterTransactions can be a built in primitive
var countRef = new Firebase( 'https://example.firebaseio.com/counter');
countRef.transaction(function(current_value) { return current_value + 1;});
![Page 16: Message Passing vs. Data Synchronization](https://reader034.vdocuments.mx/reader034/viewer/2022051013/5483ebe1b07959330c8b4a4f/html5/thumbnails/16.jpg)
Example: Counter
![Page 17: Message Passing vs. Data Synchronization](https://reader034.vdocuments.mx/reader034/viewer/2022051013/5483ebe1b07959330c8b4a4f/html5/thumbnails/17.jpg)
Latency Compensation
There may be several “incorrect” intermediate stepsbut the system as a whole is eventually consistent
eg: Events can always be optimistically triggered locally
![Page 18: Message Passing vs. Data Synchronization](https://reader034.vdocuments.mx/reader034/viewer/2022051013/5483ebe1b07959330c8b4a4f/html5/thumbnails/18.jpg)
Thank You
Write your app logic as a function ofcurrent state instead of deltas
Store state instead of passing messages
twitter @anantnwww kix.in