Mashing up JavaScript – Advanced Techniques for modern Web Apps

97
Mashing up JavaScript Advanced techniques for modern web applications Bastian Hofmann VZnet Netzwerke Ltd.

description

My Mashing up JavaScript presentation from OSCON

Transcript of Mashing up JavaScript – Advanced Techniques for modern Web Apps

Page 1: Mashing up JavaScript – Advanced Techniques for modern Web Apps

Mashing up JavaScriptAdvanced techniques for modern web applications

Bastian HofmannVZnet Netzwerke Ltd.

Page 2: Mashing up JavaScript – Advanced Techniques for modern Web Apps

Wtf?

Page 3: Mashing up JavaScript – Advanced Techniques for modern Web Apps
Page 4: Mashing up JavaScript – Advanced Techniques for modern Web Apps
Page 5: Mashing up JavaScript – Advanced Techniques for modern Web Apps
Page 6: Mashing up JavaScript – Advanced Techniques for modern Web Apps
Page 7: Mashing up JavaScript – Advanced Techniques for modern Web Apps
Page 8: Mashing up JavaScript – Advanced Techniques for modern Web Apps
Page 9: Mashing up JavaScript – Advanced Techniques for modern Web Apps
Page 10: Mashing up JavaScript – Advanced Techniques for modern Web Apps

• JavaScript Apps

• CORS and OAuth2

• Local Storage

• OEmbed and Caja

• WebSockets, ActivityStrea.ms and PubsubHubbub

Page 11: Mashing up JavaScript – Advanced Techniques for modern Web Apps
Page 12: Mashing up JavaScript – Advanced Techniques for modern Web Apps
Page 13: Mashing up JavaScript – Advanced Techniques for modern Web Apps
Page 14: Mashing up JavaScript – Advanced Techniques for modern Web Apps
Page 17: Mashing up JavaScript – Advanced Techniques for modern Web Apps

Let‘s write a JS App

Page 18: Mashing up JavaScript – Advanced Techniques for modern Web Apps

History & Bookmarking

Page 19: Mashing up JavaScript – Advanced Techniques for modern Web Apps

www.example.com#Page

Page 20: Mashing up JavaScript – Advanced Techniques for modern Web Apps

http://sammyjs.org/

Page 21: Mashing up JavaScript – Advanced Techniques for modern Web Apps

API Access

Page 22: Mashing up JavaScript – Advanced Techniques for modern Web Apps
Page 23: Mashing up JavaScript – Advanced Techniques for modern Web Apps

Same Origin Policy

Page 24: Mashing up JavaScript – Advanced Techniques for modern Web Apps

Cross-Origin Resource Sharing

Backendapi.twitter.com

Client client.net

AJAX

Access-Control-Allow-Origin: *

http://www.w3.org/TR/cors/

Page 25: Mashing up JavaScript – Advanced Techniques for modern Web Apps

var html="<ul>";for (var i=0; i < viewers.length; i++) {   html += "<li>" + viewers[i].displayName + "</li>";}html += "<ul>";document.getElementById("#div").innerHTML = html;

Where is the error?

Page 26: Mashing up JavaScript – Advanced Techniques for modern Web Apps

Templates

Page 27: Mashing up JavaScript – Advanced Techniques for modern Web Apps

Mustache.JS

https://github.com/janl/mustache.js

}

Page 28: Mashing up JavaScript – Advanced Techniques for modern Web Apps

var feed = [];var app = Sammy('#main', function() { this.use(Sammy.Mustache, 'ms'); this.get('#/', function() { this.trigger('getFeed'); }); ...});

jQuery(function() { app.run('#/');});

Page 29: Mashing up JavaScript – Advanced Techniques for modern Web Apps

var feed = [];var app = Sammy('#main', function() { this.use(Sammy.Mustache, 'ms'); this.get('#/', function() { this.trigger('getFeed'); }); ...});

jQuery(function() { app.run('#/');});

Page 30: Mashing up JavaScript – Advanced Techniques for modern Web Apps

var feed = [];var app = Sammy('#main', function() { this.use(Sammy.Mustache, 'ms'); this.get('#/', function() { this.trigger('getFeed'); }); ...});

jQuery(function() { app.run('#/');});

Page 31: Mashing up JavaScript – Advanced Techniques for modern Web Apps

var feed = [];var app = Sammy('#main', function() { this.use(Sammy.Mustache, 'ms'); this.get('#/', function() { this.trigger('getFeed'); }); ...});

jQuery(function() { app.run('#/');});

Page 32: Mashing up JavaScript – Advanced Techniques for modern Web Apps

var feed = [];var app = Sammy('#main', function() { this.use(Sammy.Mustache, 'ms'); this.get('#/', function() { this.trigger('getFeed'); }); ...});

jQuery(function() { app.run('#/');});

Page 33: Mashing up JavaScript – Advanced Techniques for modern Web Apps

this.bind('getFeed', function() { var that = this; $.ajax({ url: 'http://..._timeline.json', success: function(response) { feed = response; that.trigger('renderFeed'); } }); }); this.bind('renderFeed', function() { this.feed = feed; this.partial('js/templates/feed.ms'); });

Page 34: Mashing up JavaScript – Advanced Techniques for modern Web Apps

this.bind('getFeed', function() { var that = this; $.ajax({ url: 'http://..._timeline.json', success: function(response) { feed = response; that.trigger('renderFeed'); } }); }); this.bind('renderFeed', function() { this.feed = feed; this.partial('js/templates/feed.ms'); });

Page 35: Mashing up JavaScript – Advanced Techniques for modern Web Apps

this.bind('getFeed', function() { var that = this; $.ajax({ url: 'http://..._timeline.json', success: function(response) { feed = response; that.trigger('renderFeed'); } }); }); this.bind('renderFeed', function() { this.feed = feed; this.partial('js/templates/feed.ms'); });

Page 36: Mashing up JavaScript – Advanced Techniques for modern Web Apps

this.bind('getFeed', function() { var that = this; $.ajax({ url: 'http://..._timeline.json', success: function(response) { feed = response; that.trigger('renderFeed'); } }); }); this.bind('renderFeed', function() { this.feed = feed; this.partial('js/templates/feed.ms'); });

Page 37: Mashing up JavaScript – Advanced Techniques for modern Web Apps

this.bind('getFeed', function() { var that = this; $.ajax({ url: 'http://..._timeline.json', success: function(response) { feed = response; that.trigger('renderFeed'); } }); }); this.bind('renderFeed', function() { this.feed = feed; this.partial('js/templates/feed.ms'); });

Page 38: Mashing up JavaScript – Advanced Techniques for modern Web Apps

this.bind('getFeed', function() { var that = this; $.ajax({ url: 'http://..._timeline.json', success: function(response) { feed = response; that.trigger('renderFeed'); } }); }); this.bind('renderFeed', function() { this.feed = feed; this.partial('js/templates/feed.ms'); });

Page 39: Mashing up JavaScript – Advanced Techniques for modern Web Apps

<ul>{{#feed}} <li> <p>{{{statusnet_html}}}</p> <p>{{created_at}} {{#user}} {{name}} {{/user}} </p> </li>{{/feed}}</ul>

Page 40: Mashing up JavaScript – Advanced Techniques for modern Web Apps

<ul>{{#feed}} <li> <p>{{{statusnet_html}}}</p> <p>{{created_at}} {{#user}} {{name}} {{/user}} </p> </li>{{/feed}}</ul>

Page 41: Mashing up JavaScript – Advanced Techniques for modern Web Apps

<ul>{{#feed}} <li> <p>{{{statusnet_html}}}</p> <p>{{created_at}} {{#user}} {{name}} {{/user}} </p> </li>{{/feed}}</ul>

Page 42: Mashing up JavaScript – Advanced Techniques for modern Web Apps

<ul>{{#feed}} <li> <p>{{{statusnet_html}}}</p> <p>{{created_at}} {{#user}} {{name}} {{/user}} </p> </li>{{/feed}}</ul>

Page 43: Mashing up JavaScript – Advanced Techniques for modern Web Apps

DEMO

Page 44: Mashing up JavaScript – Advanced Techniques for modern Web Apps

Authorization

Page 45: Mashing up JavaScript – Advanced Techniques for modern Web Apps
Page 46: Mashing up JavaScript – Advanced Techniques for modern Web Apps

+----------+ Client Identifier +----------------+ | |>---(A)-- & Redirection URI --->| | | | | | End <--+ - - - +----(B)-- User authenticates -->| Authorization | User | | | Server | | |<---(C)--- Redirect URI -------<| | | Client | with Access Token | | | in | in Fragment +----------------+ | Browser | | | +----------------+ | |>---(D)--- Redirect URI ------->| | | | without Fragment | Web Server | | | | with Client | | (F) |<---(E)--- Web Page with ------<| Resource | | Access | Script | | | Token | +----------------+ +----------+

OAuth 2

http://tools.ietf.org/html/draft-ietf-oauth-v2

Page 47: Mashing up JavaScript – Advanced Techniques for modern Web Apps

this.bind('getFeed', function() { oauth2.retrieveAccessToken(); if (! oauth2.store['access_token']) { this.redirect('#Login'); return; } ... });

Page 48: Mashing up JavaScript – Advanced Techniques for modern Web Apps

this.bind('getFeed', function() { oauth2.retrieveAccessToken(); if (! oauth2.store['access_token']) { this.redirect('#Login'); return; } ... });

Page 50: Mashing up JavaScript – Advanced Techniques for modern Web Apps

this.get('#Login', function() { var consumerKey = 'abc....'; window.open('http://status.net/api/oauth2/authorize?response_toke=token&client_id=' + consumerKey);});

Page 51: Mashing up JavaScript – Advanced Techniques for modern Web Apps

<script type="text/javascript"> var fragment = location.hash.substr(1); opener.parent.oauth2.storeToken(fragment); window.close();</script>

Page 52: Mashing up JavaScript – Advanced Techniques for modern Web Apps

<script type="text/javascript"> var fragment = location.hash.substr(1); opener.parent.oauth2.storeToken(fragment); window.close();</script>

Page 53: Mashing up JavaScript – Advanced Techniques for modern Web Apps

<script type="text/javascript"> var fragment = location.hash.substr(1); opener.parent.oauth2.storeToken(fragment); window.close();</script>

Page 54: Mashing up JavaScript – Advanced Techniques for modern Web Apps

<form action="#Entry" method="post"> <textarea name="comment"></textarea> <input type="submit" value="send"/></form>

Page 55: Mashing up JavaScript – Advanced Techniques for modern Web Apps

<form action="#Entry" method="post"> <textarea name="comment"></textarea> <input type="submit" value="send"/></form>

Page 56: Mashing up JavaScript – Advanced Techniques for modern Web Apps

<form action="#Entry" method="post"> <textarea name="comment"></textarea> <input type="submit" value="send"/></form>

Page 57: Mashing up JavaScript – Advanced Techniques for modern Web Apps

this.post('#Entry', function() { var that = this; $.ajax({ url: 'http://status.net/.../update.json?oauth_token=' + oauth2.store['access_token'], type: 'POST', data: { 'status': that.params['comment'] }, success: function() { that.redirect('#/'); } }); });

Page 58: Mashing up JavaScript – Advanced Techniques for modern Web Apps

this.post('#Entry', function() { var that = this; $.ajax({ url: 'http://status.net/.../update.json?oauth_token=' + oauth2.store['access_token'], type: 'POST', data: { 'status': that.params['comment'] }, success: function() { that.redirect('#/'); } }); });

Page 59: Mashing up JavaScript – Advanced Techniques for modern Web Apps

this.post('#Entry', function() { var that = this; $.ajax({ url: 'http://status.net/.../update.json?oauth_token=' + oauth2.store['access_token'], type: 'POST', data: { 'status': that.params['comment'] }, success: function() { that.redirect('#/'); } }); });

Page 60: Mashing up JavaScript – Advanced Techniques for modern Web Apps

this.post('#Entry', function() { var that = this; $.ajax({ url: 'http://status.net/.../update.json?oauth_token=' + oauth2.store['access_token'], type: 'POST', data: { 'status': that.params['comment'] }, success: function() { that.redirect('#/'); } }); });

Page 61: Mashing up JavaScript – Advanced Techniques for modern Web Apps

this.post('#Entry', function() { var that = this; $.ajax({ url: 'http://status.net/.../update.json?oauth_token=' + oauth2.store['access_token'], type: 'POST', data: { 'status': that.params['comment'] }, success: function() { that.redirect('#/'); } }); });

Page 62: Mashing up JavaScript – Advanced Techniques for modern Web Apps

this.post('#Entry', function() { var that = this; $.ajax({ url: 'http://status.net/.../update.json?oauth_token=' + oauth2.store['access_token'], type: 'POST', data: { 'status': that.params['comment'] }, success: function() { that.redirect('#/'); } }); });

Page 63: Mashing up JavaScript – Advanced Techniques for modern Web Apps

Storing the access token

Page 64: Mashing up JavaScript – Advanced Techniques for modern Web Apps

Local Storage

http://www.w3.org/TR/webstorage/

Page 65: Mashing up JavaScript – Advanced Techniques for modern Web Apps

localStorage.setItem("key", value);

Page 66: Mashing up JavaScript – Advanced Techniques for modern Web Apps

localStorage.getItem("key");

Page 67: Mashing up JavaScript – Advanced Techniques for modern Web Apps

DEMO

Page 68: Mashing up JavaScript – Advanced Techniques for modern Web Apps

Mash it up!

Page 70: Mashing up JavaScript – Advanced Techniques for modern Web Apps

OEmbed

http://oembed.com/

Page 72: Mashing up JavaScript – Advanced Techniques for modern Web Apps
Page 74: Mashing up JavaScript – Advanced Techniques for modern Web Apps

{  "provider_url":"http:\/\/www.youtube.com\/",  "title":"Jupiter Jones - Das Jahr in dem ich schlief (Musik Video)",  "html":"\u003cobject width=\"500\" height=\"306\"\u003e\u003cparam name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/OyJd2qsRkNk?version=3\"\u003e\u003c\/param\u003e\u003cparam name=\"allowFullScreen\" value=\"true\"\u003e\u003c\/param\u003e\u003cparam name=\"allowscriptaccess\" value=\"always\"\u003e\u003c\/param\u003e\u003cembed src=\"http:\/\/www.youtube.com\/v\/OyJd2qsRkNk?version=3\" type=\"application\/x-shockwave-flash\" width=\"500\" height=\"306\" allowscriptaccess=\"always\" allowfullscreen=\"true\"\u003e\u003c\/embed\u003e\u003c\/object\u003e",  "author_name":"St182",  "height":306,  "thumbnail_width":480,  "width":500,  "version":"1.0",  "author_url":"http:\/\/www.youtube.com\/user\/Stinkfist182",  "provider_name":"YouTube",  "thumbnail_url":"http:\/\/i4.ytimg.com\/vi\/OyJd2qsRkNk\/hqdefault.jpg",  "type":"video",  "thumbnail_height":360}

Page 75: Mashing up JavaScript – Advanced Techniques for modern Web Apps

cool video:

Page 76: Mashing up JavaScript – Advanced Techniques for modern Web Apps

http://embed.ly/

Page 77: Mashing up JavaScript – Advanced Techniques for modern Web Apps
Page 78: Mashing up JavaScript – Advanced Techniques for modern Web Apps

Caja

http://code.google.com/p/google-caja/

Page 79: Mashing up JavaScript – Advanced Techniques for modern Web Apps

html_sanitize(‘<script>alert(“foo“);</script>‘)

Page 80: Mashing up JavaScript – Advanced Techniques for modern Web Apps

DEMO

Page 81: Mashing up JavaScript – Advanced Techniques for modern Web Apps

Instant updates without reloading

Page 82: Mashing up JavaScript – Advanced Techniques for modern Web Apps

PubSubHubbubretrieves Atom feed with Hub URL

Hub

posts sthpings everysubscriber

subscribes for feed

ackssubscription

http://code.google.com/p/pubsubhubbub/

Page 84: Mashing up JavaScript – Advanced Techniques for modern Web Apps

<entry> <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type> <id>http://status.net.xyz:8061/index.php/notice/20</id> <title>hello from client</title> <content type="html">hello from client</content> <link rel="alternate" type="text/html" href="http://status.net.xyz:8061/index.php/notice/20"/> <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb> <published>2011-05-23T21:07:33+00:00</published> <updated>2011-05-23T21:07:33+00:00</updated> <link rel="ostatus:conversation" href="http://status.net.xyz:8061/index.php/conversation/20"/> <georss:point>52.52437 13.41053</georss:point> <link rel="self" type="application/atom+xml"href="http://status.net.xyz:8061/index.php/api/statuses/show/20.atom"/> <link rel="edit" type="application/atom+xml"href="http://status.net.xyz:8061/index.php/api/statuses/show/20.atom"/> <statusnet:notice_info local_id="20" source="api" favorite="false"repeated="false"></statusnet:notice_info></entry>

Page 85: Mashing up JavaScript – Advanced Techniques for modern Web Apps

http://activitystrea.ms/

Page 88: Mashing up JavaScript – Advanced Techniques for modern Web Apps

http://nodejs.org/

Page 89: Mashing up JavaScript – Advanced Techniques for modern Web Apps

WebSockets

http://dev.w3.org/html5/websockets/

Page 90: Mashing up JavaScript – Advanced Techniques for modern Web Apps

socket.io

http://socket.io/

Page 91: Mashing up JavaScript – Advanced Techniques for modern Web Apps

Browser Notifications

Page 92: Mashing up JavaScript – Advanced Techniques for modern Web Apps

webkitNotifications.createNotification(image, title, text).show();

Page 93: Mashing up JavaScript – Advanced Techniques for modern Web Apps

webkitNotifications.requestPermission();

Page 94: Mashing up JavaScript – Advanced Techniques for modern Web Apps

retrieve Stream with Hub

Ajax: request Subscription

WebSockets:new Post

subscribe at hubchallenge

acknew post

Notification

new post

Page 95: Mashing up JavaScript – Advanced Techniques for modern Web Apps

DEMO

Page 96: Mashing up JavaScript – Advanced Techniques for modern Web Apps

http://bit.ly/oscon_js_mashup

Rate and Comment