SlideShare a Scribd company logo
www.arrabiata.co.uk
HTML5/JavaScript Communication APIs
Dutch PHP Conference, 27.06.2014
Christian Wenz
christian.wenz@arrabiata.co.uk
Arrabiata Solutions
::
As a specialist for digital solutions we offer our clients all services from
conception to realization, support and optimization. Our focus is international
with offices in Munich and London. Arrabiata Solutions was founded in 2006
and has more than 30 employees with a combined experience of over 100
successful projects. ::
Arrabiata is a full-service digital agency.
AGENDA
STATUS QUO
WORKING AROND SOP
MESSAGING API
SSE & WEB SOCKETS
CONCLUSION
Status Quo
• GET requests via anything with an src attribute
– No restrictions
– Only possible to provide URL
• Any (browser-supported) HTTP verb via
XMLHttpRequest
– Can set headers and HTTP body
– Restricted by SOP (same-origin policy)
• Protocol, domain, port
• Still using HTTP
Working around SOP: JSONP
• JSON with padding
• Ajax call:
– <script src="http://domain/file.ashx?jsonp=func">
</script>
• Return value:
– func([1, 2, 3]);
• „Padding“ is „func“
• Works in virtually any browser
• Still is kinda hackish
CORS
• Cross-Origin Resource Sharing
• Works around the Same Origin Policy
• Restrictions must be met, though
– Specific Content-type header
– Origin header
– Server uses Access-Control-Allow-Origin header
(value must be * or Origin header for the browser
to proceed)
• Advanced approach: preflighted requests (e.g.
for POST requests to avoid CSRF)
Messaging API
• Simple cross-domain mechanism to
send/receive data
• Works everywhere except IE7-, and limited in
IE8+
• Sending:
– Access other window (e.g. contentWindow
property of an iframe)
– Use postMessage() method to send data (1st
argument)
– For security reasons, use origin of target site as
2nd argument
Messaging API (2)
• Receiving:
– Wait for window‘s message event.
– Event arguments contain the data sent (data
property) and the origin of the sender (origin
property)
– Use postMessage() to send data back to the
origin
– Sender may use the message event as well to
process the data from the receiver.
Server-Sent Events
• JavaScript API for long polling requests
• Server continously sends data, the client just
receives and processes them
• Step 1: subscribe to source
– var es = new EventSource("polling.ext");
• Step 2: listen to message event
– es.onmessage = function(ev) {
console.log(ev.data);
};
• Works almost everywhere except IE
Stream Format
• Content-type: text/event-stream
• Fields: id, data, event, retry (all optional!)
• Format: <field>: <value>
• Blank lines between fields
• id: 123
data: abc
id: 456
event: def
Reconnecting to the Server
• Browser reconnects the connection every
few seconds (unless changed by retry
stream value)
• Browser sends Last-Event-ID HTTP header if
server sent ID before
• Allows server to only send new events
Web Sockets
• Full duplex communication
• Circumvents a few of the disadvantages of
HTTP (metadata sent with each request, re-
establishment of the connection, etc.)
• Works in all recent browsers except IE9-
HTTP Handshake
• Request:
GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
Origin: http://example.com
• Response:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
Sec-WebSocket-Protocol: chat
API for Web Sockets
• Server: depends on the technology used; node.js is the
poster child of the month
• Client:
– var w = new WebSocket("ws://server:1234");
– w.onopen = function(ev) {
w.send("Hi!");
}
w.onmessage = function(ev) {
console.log(ev.data);
}
Websocket Server with node.js
var server = require('http');
var webSocketServer = require("websocket").server;
server.listen(1234);
var wsServer = new webSocketServer({
httpServer: server
});
wsServer.on('request', function(request) {
…
});
Socket.IO
• There are countless protocol versions for Web Sockets
• Better use an abstraction layer that also provides polyfills
for browsers that use an older API version, e.g. Socket.IO
(http://socket.io/).
• Server:
– var io = require("socket.io").listen(1234);
• Client:
– var ws = io.connect("http://127.0.0.1:1234");
Decision Chart
Technique X-Domain Return data Backchannel Long running High Performance
src     
XHR     
JSONP     
CORS     
Messaging     
Workers     
Server-Sent
Events
    
Web Sockets     
Thank You!
• christian.wenz@arrabiata.co.uk
• http://www.arrabiata.co.uk/
• @chwenz
• Rate this session:
http://joind.in/talk/view/10857

More Related Content

PDF
ZaloPay Merchant Platform on K8S on-premise
PDF
PHP and Web Services
PPTX
Html5 websockets
PDF
Linux Hosting Training Course Level 1-2
PDF
Linux Hosting Training Course [Level 1] - Lec 8
PPT
JUG louvain websockets
PPTX
PDF
Php & web server performace
ZaloPay Merchant Platform on K8S on-premise
PHP and Web Services
Html5 websockets
Linux Hosting Training Course Level 1-2
Linux Hosting Training Course [Level 1] - Lec 8
JUG louvain websockets
Php & web server performace

What's hot (19)

ODP
IT Operations for Web Developers
PDF
Are we security yet
PPTX
20180714 workshop - Ethereum decentralized application with truffle framework
PDF
JavaScript Service Worker Design Patterns for Better User Experience
PPTX
Intro to Web Sockets
PDF
Ускоряем загрузку картинок вебсокетами
PDF
My adventure with WebSockets
PDF
CNIT 124: Ch 8: Exploitation
PPTX
PPTX
Eclipse Dirigible WebIDE - Deep Dive
PDF
Cloud Hosted mongodb
PDF
Communicating on the web
PPTX
Http/2 lightning
PDF
O'Reilly Fluent Conference: HTTP/1.1 vs. HTTP/2
PPT
Excellent rest using asp.net web api
PPTX
Build RPC for PHP
PPTX
ASP.NET WEB API
PDF
What HTTP/2.0 Will Do For You
IT Operations for Web Developers
Are we security yet
20180714 workshop - Ethereum decentralized application with truffle framework
JavaScript Service Worker Design Patterns for Better User Experience
Intro to Web Sockets
Ускоряем загрузку картинок вебсокетами
My adventure with WebSockets
CNIT 124: Ch 8: Exploitation
Eclipse Dirigible WebIDE - Deep Dive
Cloud Hosted mongodb
Communicating on the web
Http/2 lightning
O'Reilly Fluent Conference: HTTP/1.1 vs. HTTP/2
Excellent rest using asp.net web api
Build RPC for PHP
ASP.NET WEB API
What HTTP/2.0 Will Do For You
Ad

Similar to HTML5/JavaScript Communication APIs - DPC 2014 (20)

PDF
Using communication and messaging API in the HTML5 world - GIl Fink, sparXsys
PDF
Using Communication and Messaging API in the HTML5 World
PPT
Synapseindia dot net development web applications with ajax
PDF
Lecture 6 Web Sockets
PPTX
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
PDF
Computer Network notes Application Layer.pdf
PDF
Presentation on Application layer_201.pdf
PDF
2. application layer
PPTX
Chapter_2 jaringan komputer informatika.pptx
PPTX
Websocket vs SSE - Paris.js - 24/06/15
PDF
Dev con kolkata 2012 websockets
PPTX
Webservices
ODP
Building interactivity with websockets
PPTX
Websocket technology for XPages
PPTX
Computer Network presentation chapter two
PDF
11.Open Data Protocol(ODATA)
PDF
Building Next Generation Real-Time Web Applications using Websockets
PPTX
Windows Phone 8 - 12 Network Communication
PPTX
signalr
PDF
009577496.pdf
Using communication and messaging API in the HTML5 world - GIl Fink, sparXsys
Using Communication and Messaging API in the HTML5 World
Synapseindia dot net development web applications with ajax
Lecture 6 Web Sockets
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
Computer Network notes Application Layer.pdf
Presentation on Application layer_201.pdf
2. application layer
Chapter_2 jaringan komputer informatika.pptx
Websocket vs SSE - Paris.js - 24/06/15
Dev con kolkata 2012 websockets
Webservices
Building interactivity with websockets
Websocket technology for XPages
Computer Network presentation chapter two
11.Open Data Protocol(ODATA)
Building Next Generation Real-Time Web Applications using Websockets
Windows Phone 8 - 12 Network Communication
signalr
009577496.pdf
Ad

Recently uploaded (20)

PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
Empathic Computing: Creating Shared Understanding
PDF
Encapsulation theory and applications.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
KodekX | Application Modernization Development
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
MIND Revenue Release Quarter 2 2025 Press Release
Big Data Technologies - Introduction.pptx
Spectroscopy.pptx food analysis technology
Empathic Computing: Creating Shared Understanding
Encapsulation theory and applications.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Understanding_Digital_Forensics_Presentation.pptx
cuic standard and advanced reporting.pdf
Review of recent advances in non-invasive hemoglobin estimation
Programs and apps: productivity, graphics, security and other tools
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Diabetes mellitus diagnosis method based random forest with bat algorithm
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
KodekX | Application Modernization Development
Advanced methodologies resolving dimensionality complications for autism neur...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Per capita expenditure prediction using model stacking based on satellite ima...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?

HTML5/JavaScript Communication APIs - DPC 2014

  • 1. www.arrabiata.co.uk HTML5/JavaScript Communication APIs Dutch PHP Conference, 27.06.2014 Christian Wenz christian.wenz@arrabiata.co.uk
  • 2. Arrabiata Solutions :: As a specialist for digital solutions we offer our clients all services from conception to realization, support and optimization. Our focus is international with offices in Munich and London. Arrabiata Solutions was founded in 2006 and has more than 30 employees with a combined experience of over 100 successful projects. :: Arrabiata is a full-service digital agency.
  • 3. AGENDA STATUS QUO WORKING AROND SOP MESSAGING API SSE & WEB SOCKETS CONCLUSION
  • 4. Status Quo • GET requests via anything with an src attribute – No restrictions – Only possible to provide URL • Any (browser-supported) HTTP verb via XMLHttpRequest – Can set headers and HTTP body – Restricted by SOP (same-origin policy) • Protocol, domain, port • Still using HTTP
  • 5. Working around SOP: JSONP • JSON with padding • Ajax call: – <script src="http://domain/file.ashx?jsonp=func"> </script> • Return value: – func([1, 2, 3]); • „Padding“ is „func“ • Works in virtually any browser • Still is kinda hackish
  • 6. CORS • Cross-Origin Resource Sharing • Works around the Same Origin Policy • Restrictions must be met, though – Specific Content-type header – Origin header – Server uses Access-Control-Allow-Origin header (value must be * or Origin header for the browser to proceed) • Advanced approach: preflighted requests (e.g. for POST requests to avoid CSRF)
  • 7. Messaging API • Simple cross-domain mechanism to send/receive data • Works everywhere except IE7-, and limited in IE8+ • Sending: – Access other window (e.g. contentWindow property of an iframe) – Use postMessage() method to send data (1st argument) – For security reasons, use origin of target site as 2nd argument
  • 8. Messaging API (2) • Receiving: – Wait for window‘s message event. – Event arguments contain the data sent (data property) and the origin of the sender (origin property) – Use postMessage() to send data back to the origin – Sender may use the message event as well to process the data from the receiver.
  • 9. Server-Sent Events • JavaScript API for long polling requests • Server continously sends data, the client just receives and processes them • Step 1: subscribe to source – var es = new EventSource("polling.ext"); • Step 2: listen to message event – es.onmessage = function(ev) { console.log(ev.data); }; • Works almost everywhere except IE
  • 10. Stream Format • Content-type: text/event-stream • Fields: id, data, event, retry (all optional!) • Format: <field>: <value> • Blank lines between fields • id: 123 data: abc id: 456 event: def
  • 11. Reconnecting to the Server • Browser reconnects the connection every few seconds (unless changed by retry stream value) • Browser sends Last-Event-ID HTTP header if server sent ID before • Allows server to only send new events
  • 12. Web Sockets • Full duplex communication • Circumvents a few of the disadvantages of HTTP (metadata sent with each request, re- establishment of the connection, etc.) • Works in all recent browsers except IE9-
  • 13. HTTP Handshake • Request: GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw== Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13 Origin: http://example.com • Response: HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk= Sec-WebSocket-Protocol: chat
  • 14. API for Web Sockets • Server: depends on the technology used; node.js is the poster child of the month • Client: – var w = new WebSocket("ws://server:1234"); – w.onopen = function(ev) { w.send("Hi!"); } w.onmessage = function(ev) { console.log(ev.data); }
  • 15. Websocket Server with node.js var server = require('http'); var webSocketServer = require("websocket").server; server.listen(1234); var wsServer = new webSocketServer({ httpServer: server }); wsServer.on('request', function(request) { … });
  • 16. Socket.IO • There are countless protocol versions for Web Sockets • Better use an abstraction layer that also provides polyfills for browsers that use an older API version, e.g. Socket.IO (http://socket.io/). • Server: – var io = require("socket.io").listen(1234); • Client: – var ws = io.connect("http://127.0.0.1:1234");
  • 17. Decision Chart Technique X-Domain Return data Backchannel Long running High Performance src      XHR      JSONP      CORS      Messaging      Workers      Server-Sent Events      Web Sockets     
  • 18. Thank You! • christian.wenz@arrabiata.co.uk • http://www.arrabiata.co.uk/ • @chwenz • Rate this session: http://joind.in/talk/view/10857