browser apis for data exchange: types and application

60
BROWSER APIS FOR DATA EXCHANGE types and application Pavel Klimenko

Upload: pavel-klimiankou

Post on 20-Aug-2015

60 views

Category:

Internet


1 download

TRANSCRIPT

Page 1: Browser APIs for data exchange: types and application

BROWSER APISFOR DATA EXCHANGE

types and application

Pavel Klimenkov

Page 2: Browser APIs for data exchange: types and application

HOLY NETWORK BIBLE

Page 3: Browser APIs for data exchange: types and application
Page 4: Browser APIs for data exchange: types and application

AGENDA

Request

Response

Request

Response

Request

Response

Page 5: Browser APIs for data exchange: types and application

Request

Response

Page 6: Browser APIs for data exchange: types and application

MSXML2XMLHTTP

XMLHttpRequest

Classic XHR

Page 7: Browser APIs for data exchange: types and application

EXAMPLE

Page 8: Browser APIs for data exchange: types and application

FEATURESHTTP(S): GET, POST, PUT, DELETE

Text only (base64 for binary)

Access to certain http headers

Sync/async

GZip, deflate compression

Page 9: Browser APIs for data exchange: types and application

SUPPORT

EVERYWHERE!

Page 10: Browser APIs for data exchange: types and application

SOMETHING’S MISSING

Files and binary support

Cross domain requests

Convenient POST requests

Page 11: Browser APIs for data exchange: types and application

XML HTTP REQUEST LEVEL 2 (2011)

+ binary data and files

+ html forms support - FormData object

+ cross domain requests (CORS)

+ upload/download events

Page 12: Browser APIs for data exchange: types and application

CORSREQUEST HEADERS

GET /secret.html HTTP/1.1

Host: microsoft.com

Origin:

http://google.com

Page 13: Browser APIs for data exchange: types and application

CORSRESPONSE HEADERS

HTTP/1.1 200 OK

Access-Control-Allow-

Origin:

http://google.com

Page 14: Browser APIs for data exchange: types and application

FORM DATA

Page 15: Browser APIs for data exchange: types and application

SUPPORT

Page 16: Browser APIs for data exchange: types and application

FETCH()

Came from service worker

Promise-oriented

Almost like XMLHttpRequest, but simpler

Still in development

Page 17: Browser APIs for data exchange: types and application

FETCH()

Page 18: Browser APIs for data exchange: types and application

SUPPORT

Page 19: Browser APIs for data exchange: types and application

SUMMARY

There’s XMLHttpRequest & fetch()

They both work over HTTP(S)

Request - response - stop

Text and binary support

Cross domain requests

Page 20: Browser APIs for data exchange: types and application

WHAT’S WRONG

Minus one TCP connection (out of 6 per domain)

Not designed for real time server events streaming

> polling

> long polling

Page 21: Browser APIs for data exchange: types and application

LONG POLLING

Page 22: Browser APIs for data exchange: types and application

Request

Response

Page 23: Browser APIs for data exchange: types and application

SERVER-SENT EVENTS(2006)

SSE

Page 24: Browser APIs for data exchange: types and application

EVENT SOURCE

EventSource is an API that allows listening for server sent events in DOM manner

Page 25: Browser APIs for data exchange: types and application

FEATURES

HTTP(s)

Automatic reconnect

Tracking last delivered event id (supplements auto reconnect)

Simple polyfill

Page 26: Browser APIs for data exchange: types and application

SSE REQUEST

GET /users/updates HTTP/1.1

Host: myserver.com

Accept: text/event-stream

Last-Event-ID: 43

Page 27: Browser APIs for data exchange: types and application

SSE RESPONSE

HTTP/1.1 200 OK

Connection: keep-alive

Content-Type: text/event-stream

Transfer-Encoding: chunked

Page 28: Browser APIs for data exchange: types and application

SSE MESSAGESdata: Simple message

data: {“message”: “JSON payload”}

event: user.updatedata: {“changed”: “firstName”}

id: 42event: broadcastdata: shutdown

1

2

3

4

Page 29: Browser APIs for data exchange: types and application

USE CASES

GPS stream for real time vehicle animation on map

Stream of modified objects IDs for invalidating client cache

Page 30: Browser APIs for data exchange: types and application

SUPPORT

Page 31: Browser APIs for data exchange: types and application

WHAT’S BADNo IE support (yet polyfill is simple)

Text only (we still can use base64)

Initial request arguments cannot be changed

Page 32: Browser APIs for data exchange: types and application

Request

Response

Page 33: Browser APIs for data exchange: types and application

WEB SOCKET2010

Page 34: Browser APIs for data exchange: types and application

WEB SOCKET

WebSocket is an API, which allows sending text and binary messages between client and server in both directions

Page 35: Browser APIs for data exchange: types and application

FEATURES

Starts over HTTP(S), but falls back to own binary protocol after a handshake

ws:// and wss:// instead of http:// and https://

Text and binary

Page 36: Browser APIs for data exchange: types and application

USE CASES

Browser chat application

Server events stream with adjustable subscriptions list

Progressive media download

Page 37: Browser APIs for data exchange: types and application

SUPPORT

Page 38: Browser APIs for data exchange: types and application

WHAT TO CONSIDERMore complex protocol (if you ever going to implement it)

No compression (something experimental in Chrome)

Page 39: Browser APIs for data exchange: types and application

WHAT’S WRONG WITHXHR, SSE, WEBSOCKET

No request/response multiplexingCan’t use transport until current request/response ends

There’re only 6 TCP connections per domainEach API takes one of those

Page 40: Browser APIs for data exchange: types and application
Page 41: Browser APIs for data exchange: types and application
Page 42: Browser APIs for data exchange: types and application
Page 43: Browser APIs for data exchange: types and application

WEB RTC2010

WebRTC is an API peer-to-peer exchange of text and binary messages

Page 44: Browser APIs for data exchange: types and application

FEATURES

UDP instead of TCP

Text and binary

Built in audio/video transmission support

peer-to-peer

Mandatory encryption for non-media data

Page 45: Browser APIs for data exchange: types and application

IT’S NOT JUSTSKYPE KILLER

Page 46: Browser APIs for data exchange: types and application

USE CASES

Browser games

Exchange of data that is needed by clients, but not going to be saved on the server

Ok, better Skype

Page 47: Browser APIs for data exchange: types and application

WHY IS WEB RTC

SO DIFFICULT?

Page 48: Browser APIs for data exchange: types and application

2 REASONS

Clients have to find each other

UDP traffic must be delivered through NAT

Page 49: Browser APIs for data exchange: types and application

SIGNALING CHANNELBefore connection could start, peers must exchange Offer/Answer messages over some sort of signaling channel

Page 50: Browser APIs for data exchange: types and application

YOU HAVE TO BUILD ONE!

Offer/Answer can be delivered by

Courier

SMS

more realistically, dedicated server that

peers can connect to via XHR or WebSocket

Page 51: Browser APIs for data exchange: types and application

NAT TRAVERSALWhere UDP traffic should be sent to? You don’t know your public IP

Page 52: Browser APIs for data exchange: types and application

WORKAROUND: STUN, TURN

Page 53: Browser APIs for data exchange: types and application

WORKAROUND IS CALLED“ICE”

ICE - interactive connectivity establishment

Using network card, STUN and TURN servers, ICE gets pairs of IP:Port (ICE candidates), which can be sent to the second peer

Page 54: Browser APIs for data exchange: types and application

ALL TOGETHER1) We need signaling channel

2) We need addresses of STUN/TURN servers

3) RTCPeerConnection can create Offer and find ICE candidates

4) We should pass all from above through signaling channel to the second peer

5) Second peer will use signaling channel to send his Answer and ICE candidates to us

Page 55: Browser APIs for data exchange: types and application
Page 56: Browser APIs for data exchange: types and application

SUPPORT

Page 57: Browser APIs for data exchange: types and application

WHAT’S WRONG

No IE and Safari support

Complex polyfill

Difficult to use and understand

Cross browser issues and vendor prefixes

Page 58: Browser APIs for data exchange: types and application

WRAPPING UP

XMLHttpRequest Server-sent events

WebSocket WebRTC

fetch()

Page 59: Browser APIs for data exchange: types and application

ABOUT THE BOOKCovers all basic questions about the networkProvides full picture of browser networking APIs

Page 60: Browser APIs for data exchange: types and application

THANK YOU!

/pasha.klimenkov

/in/pavelklimenkov

DotsAndBrackets.com