Pushing the Web: Interesting things to Know

74
Pushing the web: Interesting things to know about upcoming web standards By Shwetank Dixit, Opera Software

Transcript of Pushing the Web: Interesting things to Know

Page 1: Pushing the Web: Interesting things to Know

Pushing the web: Interesting things to know about upcoming web standards

By Shwetank Dixit, Opera Software

Page 2: Pushing the Web: Interesting things to Know

about meWeb Evangelist, Opera Developer Relations Team

Member, W3C Mobile Web for Social Development Group

Member, W3C Web Education Community Group

twitter.com/shwetankemail: [email protected]

Page 3: Pushing the Web: Interesting things to Know

Front-end development - a lot of learn!

Page 4: Pushing the Web: Interesting things to Know

We’ll focus on a few things

Page 5: Pushing the Web: Interesting things to Know

We’ll focus on a few thingsHTML5 and friends

Page 6: Pushing the Web: Interesting things to Know

Same Origin PolicyOur story begins from here...

Page 7: Pushing the Web: Interesting things to Know

Same Origin Policy1. Let /A/ be the first origin being compared, and let /B/ be the second origin being compared.

2. If either /A/ or /B/ is not a scheme/host/port tuple, return an implementation-defined value.

3. If /A/ and /B/ have scheme components that are not identical, return false.

4. If /A/ and /B/ have host components that are not identical, return false.

5. If /A/ and /B/ have port components that are not identical, return false.

6. Return true.

Page 8: Pushing the Web: Interesting things to Know

Same Origin PolicyShould have the same...

Scheme / Host / Port

Page 9: Pushing the Web: Interesting things to Know

Same Origin PolicyThese will NOT match, and considered separate origins

http://www.example.orghttps://www.example.org

Page 10: Pushing the Web: Interesting things to Know

Same Origin PolicyThese will NOT match, and considered separate origins

http://www.example.orghttp://xyz.example.org

Page 11: Pushing the Web: Interesting things to Know

Same Origin PolicyThese will NOT match, and considered separate origins

http://www.example.orghttp://www.example.org:1337

Page 12: Pushing the Web: Interesting things to Know

Same Origin PolicyThese WILL match, and are considered the same origin

http://www.example.org/abc/http://www.example.org/xyz/

Page 13: Pushing the Web: Interesting things to Know

Storage: Web Storage

Page 14: Pushing the Web: Interesting things to Know

The problem with cookiesUnreliableNo programmatic APIs to manipulate itNot structured

Most of important of all ...Small file size, so very limited data can be stored.

Page 15: Pushing the Web: Interesting things to Know

Web StorageSession Storage and Local Storage

Page 16: Pushing the Web: Interesting things to Know

localStorage.setItem(yourkey, yourvalue); // Store the value

var item = localStorage.getItem(yourkey); // Retrieve the value and assign it to a variable

Example of using Web Storage to store and retrieve values in the browser’s local storageWith this, even if you close the browser and re-open the page again, the values should still load properly.

Page 17: Pushing the Web: Interesting things to Know

You can store images (and more) with localStorage

....BUT DON”T.

Page 18: Pushing the Web: Interesting things to Know

Automatically save entered form info locallyin case page crashes or closes, person can resume from where he left off

Page 19: Pushing the Web: Interesting things to Know

<textarea id="draft" rows="10" cols="30"></textarea>

...

...

function saveMessage(){

� var message = document.getElementById("draft");

� localStorage.setItem("message", message.value)

}

setInterval(saveMessage, 500);

STORE USER DATA OFFLINE PERIODICALLY

Page 20: Pushing the Web: Interesting things to Know

Or...You could save only when you detect a new keystroke (or a minimum number of them)

Page 21: Pushing the Web: Interesting things to Know

Gotcha Two tabs updating the same value

Page 22: Pushing the Web: Interesting things to Know

Storage eventsKnow if some other page has changed the value or not

Page 23: Pushing the Web: Interesting things to Know

addEventListener('storage', function(event){

� if (e.oldValue){

� alert('changed from \''+event.oldValue+'\' to \''+event.newValue+'\'');

� }

}, false);

GET NEW VALUE IF ITS BEEN CHANGED IN PARALLEL TAB

Page 24: Pushing the Web: Interesting things to Know

GotchaUsing a free hosting service - Don’t use local storage with it if they store users accounts on different directories.

e.g, freehosting.com/user1 and freehosting.com/user2

Page 25: Pushing the Web: Interesting things to Know

Cross Origin Resource Sharing (CORS)

Page 26: Pushing the Web: Interesting things to Know

Whats CORS?

CORS is a system of headers and rules that allow browsers and servers to communicate whether or not a given origin is allowed access to a resource stored on another.

Page 27: Pushing the Web: Interesting things to Know

Access-Control-Allow-Origin

Header to Let the referrer know whether it is allowed to use the target resource.

Page 28: Pushing the Web: Interesting things to Know

Access-Control-Allow-Origin

Access-Control-Allow-Origin: nullAccess-Control-Allow-Origin: *Access-Control-Allow-Origin: http://foo.example

Page 29: Pushing the Web: Interesting things to Know

Cross Domain XHR

var xhr = new XMLHttpRequest();var onLoadHandler = function(event) { /* do something with the response */}xhr.open('GET','http://url-of-other.server/and/path/to/script');

Page 30: Pushing the Web: Interesting things to Know

Capture JS errors ... with JS

Page 31: Pushing the Web: Interesting things to Know

window.onerror

Page 32: Pushing the Web: Interesting things to Know

window.onerror

Error MessageLine NumberFile URL in question

Page 33: Pushing the Web: Interesting things to Know

window.onerror

window.onerror = function(message, url, linenumber) {

alert("JavaScript error: " + message + " on line " + linenumber + " for " + url);

}

Page 34: Pushing the Web: Interesting things to Know

What could you do with it?

Page 35: Pushing the Web: Interesting things to Know

Better looking error messages.

Page 36: Pushing the Web: Interesting things to Know

Log errors in a flat file or DB.

Page 37: Pushing the Web: Interesting things to Know

WebSockets

Page 38: Pushing the Web: Interesting things to Know

Previous techniques

Continuous PollingLong Polling

Page 39: Pushing the Web: Interesting things to Know

WebSockets

Built Over HTTPFull DuplexBi-Directional

Page 40: Pushing the Web: Interesting things to Know

HTTP Server ‘upgrades’ to a WebSocket server

Page 41: Pushing the Web: Interesting things to Know

The initiating handshake from the client should look like this:

GET /chat HTTP/1.1

Host: server.example.com

Upgrade: WebSocket

Connection: Upgrade

Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==

Origin: http://example.com

Sec-WebSocket-Protocol: chat, superchat

Sec-WebSocket-Version: 13

And on the server

HTTP/1.1 101 Switching Protocols

Upgrade: WebSocket

Connection: Upgrade

Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

Sec-WebSocket-Protocol: chat

Page 42: Pushing the Web: Interesting things to Know

What happens1. The client sends the Sec-WebSocket-Key string dGhlIHNhbXBsZSBub25jZQ==

2. The server appends the magic string to form the string dGhlIHNhbXBsZSBub25jZQ== 258EAFA5-E914-47DA-95CA-C5AB0DC85B11

3. Now the server generates the SHA-1 hash for this longer string, which is b37a4f2cc0624f1690f64606cf385945b2bec4ea

4. Finally, the server base64-encodes the hash string to give s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

5. And this base64-encoded value is used in the Sec-WebSocket-Accept header in the server’s response.

Page 43: Pushing the Web: Interesting things to Know

WebSockets APIif ('WebSocket' in window){ /* WebSocket is supported. You can proceed with your code*/} else { /*WebSockets are not supported. Try a fallback method like long-polling etc*/}

Page 44: Pushing the Web: Interesting things to Know

WebSockets APIvar connection = new WebSocket('ws://example.org:12345/myapp');

or wss://, which is the secure socket variant to ws:// in the same way https is to http

var connection = new WebSocket('wss://secure.example.org:67890/myapp');

Page 45: Pushing the Web: Interesting things to Know

Handling an open connectionconnection.onopen = function(){ /*Send a small message to the console once the connection is established */ console.log('Connection open!');}

Page 46: Pushing the Web: Interesting things to Know

Sending Messagesconnection.send('Hey server, whats up?');

or

var message = {'name': 'bill murray','comment': 'No one will ever believe you'};connection.send(JSON.stringify(message));

Page 47: Pushing the Web: Interesting things to Know

Receiving Messagesconnection.onmessage = function(e){ var server_message = e.data; console.log(server_message);}

Page 48: Pushing the Web: Interesting things to Know

Use Cases

Any App which wants real time updating of info

1. High performance web based games2. Sport Scores3. Social Media real time updates4. Breaking news real time updates5. Chat applications

Page 50: Pushing the Web: Interesting things to Know

Device OrientationAccess to gyroscope, accelerometer info etc

Page 51: Pushing the Web: Interesting things to Know

Access gyroscope info

window.addEventListener("deviceorientation", function(event) {

// process event.alpha, event.beta and event.gamma

}, true);

Page 52: Pushing the Web: Interesting things to Know

Access accelerometer infowindow.addEventListener("devicemotion", function(event) {

// Process event.acceleration

}, true);

Page 53: Pushing the Web: Interesting things to Know

Another sneak peak

Page 54: Pushing the Web: Interesting things to Know

Check for accessvar options = {‘video’: true, ‘audio’: false};

if (navigator.getUserMedia){ navigator.getUserMedia(options, v_success, v_error); }

else{ not_supported(); }

Page 55: Pushing the Web: Interesting things to Know

Check for accessvar video_element = document.querySelector('video');......function v_success(stream){ video_element.src = stream; }

Page 56: Pushing the Web: Interesting things to Know

Use camera + <video> + <canvas> for new tricksvar button = document.querySelector('#button'); button.addEventListener('click',snapshot, false);......function snapshot(){ var c = document.querySelector('canvas'); var ctx = c.getContext('2d'); var cw = c.clientWidth; var ch = c.clientHeight; ctx.drawImage(video_element, 0, 0, cw, ch); }

Page 57: Pushing the Web: Interesting things to Know

Keep in mindWebRTC spec (containing getUserMedia) is still in flux. Not a mature standard yet.

Webkit has prefixed its version of getUserMedia.

Page 58: Pushing the Web: Interesting things to Know

Get it on:

Opera Labs BuildLatest Opera.NextOpera Mobile 12

Page 59: Pushing the Web: Interesting things to Know

Opera Dragonfly

Page 60: Pushing the Web: Interesting things to Know

(RIGHT-CLICK ON PAGE) -> ‘INSPECT ELEMENT’

Page 61: Pushing the Web: Interesting things to Know

Inspect the DOM

Page 62: Pushing the Web: Interesting things to Know

Debug JavaScript

Page 63: Pushing the Web: Interesting things to Know

Network inspector

Page 64: Pushing the Web: Interesting things to Know

Style profiler

Page 65: Pushing the Web: Interesting things to Know

Tools - Color Picker

Page 66: Pushing the Web: Interesting things to Know

Remote debugging

Page 67: Pushing the Web: Interesting things to Know

Opera Mobile Emulator

Page 68: Pushing the Web: Interesting things to Know

Opera Mobile Emulator

Page 69: Pushing the Web: Interesting things to Know

Opera Mobile Emulator

Page 70: Pushing the Web: Interesting things to Know

The Developer Briefcase

Page 71: Pushing the Web: Interesting things to Know

‘Edit the Page’ extension

Page 72: Pushing the Web: Interesting things to Know

- YSlow!- PageRank- Firebug Lite- LiveReload- Layers- ResizeMe- GitHub Notifierand more...

Other developer extensions for Opera

HTTPS://ADDONS.OPERA.COM/EN/EXTENSIONS/CATEGORY/WEB-DEVELOPMENT

Page 73: Pushing the Web: Interesting things to Know

Read up on

Dev.opera.com

Page 74: Pushing the Web: Interesting things to Know

Cheers!More questions? Ask me now or contact me at:[email protected] or, twitter.com/shwetank