Using Communication and Messaging API in the HTML5 World

Gil Fink
Gil FinkFounder and Owner at sparXys
Using Communication and 
Messaging API in The HTML5 
World 
Gil Fink 
CEO, sparXys 
@gilfink, http://www.gilfink.net
Agenda 
• HTML5: Recap 
• HTML5 Messaging APIs 
• HTML5 Communication APIs 
• Q&A 
• Summary
HTML5: Recap 
• HTML5 ~= HTML + CSS3 + new JavaScript API 
• The present and future of the web 
• W3C Recommendation!
What’s Under the HTML5 
Umbrella?
Communication and Messaging
Cross-Document Messaging 
• Simple API to send and receive in-window 
messages 
• Send messages using the postMessage function 
• Wire handlers to the message event 
6 
window.postMessage(message, domain); 
window.addEventListener("message", function () { 
// do something 
}, false);
DEMO 
Cross-Document Messaging 
Web Worker
Web Application APIs Support 
– Cross-Document Messaging 
http://caniuse.com
Cross-Origin Resource Sharing 
(CORS) 
• Browsers prevent cross-domain JavaScript requests 
• CORS enables cross-domain requests as long as: 
o Response includes Access-Control-Allow-Origin header 
o XMLHttpRequest supports CORS 
• XDomainRequest in IE8 and IE9
CORS – Client Side 
var xhr = new XMLHttpRequest(); 
xhr.open(“get”, url, true); 
xhr.onload = function () { 
// instead of using onreadystatechange 
} 
xhr.send();
CORS – Client Side API 
• Functions: 
o abort() – stops a request that is already in progress 
o send() – sends a request to the server 
• Event handlers: 
o onerror – handles request errors 
o onload – handles request success
CORS – Access Control Flow 
• Client sends ‘Access-Control’ headers during 
request preflight 
o Using the OPTIONS HTTP request 
• Server checks whether the requested resource is 
allowed 
• Client checks the preflight response and decides 
whether to allow the request
CORS – Server Side 
• Use Access-Control headers to allow 
o Origin – Specific request URI 
o Method – request method (GET, POST and etc.) 
o Headers – optional custom headers 
o Credentials – request credentials
DEMO 
CORS
Web Application APIs Support 
– CORS 
http://caniuse.com
Server-Sent Events 
• Enables servers to push data over HTTP using push 
protocols 
• Use the EventSource JavaScript API 
var source = new EventSource(url); 
source.onmessage = function (e) { 
console.log(e.data); 
}
Server-Sent Events - Server 
• Strict server protocol: 
data: This is a message. 
event: add 
data: 73857293 
• Response content type should be text/event-stream 
• Keeps the connection open
DEMO 
Server-Sent Events
Web Application APIs Support 
– Server-Sent Events 
http://caniuse.com
Real Time Web?
Existing Solutions
Web Sockets 
• Bidirectional communications channels, over a 
single TCP socket 
• Don’t allow raw access to the underlying network 
• Can replace old techniques such as: 
o Forever frames 
o Long-polling/comet
Web Sockets – Client Request 
• The client sends a WebSocket handshake request 
GET /chat HTTP/1.1 
Host: server.example.com 
Upgrade: websocket 
Connection: Upgrade 
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== 
Sec-WebSocket-Origin: http://example.com 
Sec-WebSocket-Protocol: chat, superchat
Web Sockets – Server Response 
• The server responses with a WebSocket handshake 
response 
HTTP/1.1 101 Switching Protocols 
Upgrade: websocket 
Connection: Upgrade 
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= 
Sec-WebSocket-Protocol: chat
Web Sockets – API 
• Create a WebSocket object using a URL 
• Use the WebSocket object’s built-in events: 
o onopen – fired when a web socket has opened 
o onmessage – fired when a message has been received 
o onclose – fired when a web socket has been closed
DEMO 
Web Sockets
Web Application APIs Support 
– Web Sockets 
http://caniuse.com
Questions?
Summary 
• HTML5 is a W3C recommendation 
• In this session we explored communication and 
messaging APIs 
• You have a lot more to cover
Resources 
• APIs specs – 
http://www.w3.org/TR/html5/comms.html 
http://www.w3.org/TR/cors/ 
http://www.w3.org/TR/eventsource/ 
http://www.w3.org/TR/websockets/ 
• My Blog – http://www.gilfink.net 
• Follow me on Twitter – @gilfink
Thank You!
1 of 31

More Related Content

What's hot(20)

Web Servers(IIS, NGINX, APACHE)Web Servers(IIS, NGINX, APACHE)
Web Servers(IIS, NGINX, APACHE)
Reza Jebeli630 views
Realtime web application with javaRealtime web application with java
Realtime web application with java
JeongHun Byeon4.4K views
Websockets at tossugWebsockets at tossug
Websockets at tossug
clkao3.5K views
HTTP/2 PrioritizationHTTP/2 Prioritization
HTTP/2 Prioritization
Patrick Meenan3.1K views
How fast is it?How fast is it?
How fast is it?
Patrick Meenan3.8K views
Web sockets in JavaWeb sockets in Java
Web sockets in Java
Pance Cavkovski4.6K views
Http/2Http/2
Http/2
Adrian Cardenas1.5K views
Resource PrioritizationResource Prioritization
Resource Prioritization
Patrick Meenan1.4K views
Web sockets IntroductionWeb sockets Introduction
Web sockets Introduction
Sudhir Bastakoti635 views
Server sideServer side
Server side
philipsinter592 views
HTTP - The Protocol of Our LivesHTTP - The Protocol of Our Lives
HTTP - The Protocol of Our Lives
Brent Shaffer1.1K views
Revisiting HTTP/2Revisiting HTTP/2
Revisiting HTTP/2
Fastly2.5K views
ZN27112015ZN27112015
ZN27112015
Denis Kolegov2.7K views
Server architectureServer architecture
Server architecture
Masters Academy137 views
signalrsignalr
signalr
Owen Chen787 views

Similar to Using Communication and Messaging API in the HTML5 World(20)

2. application layer2. application layer
2. application layer
TageleBerihun184 views
Web Real-time CommunicationsWeb Real-time Communications
Web Real-time Communications
Alexei Skachykhin1.4K views
Websocket vs SSE - Paris.js - 24/06/15Websocket vs SSE - Paris.js - 24/06/15
Websocket vs SSE - Paris.js - 24/06/15
streamdata.io1.4K views
WebsocketWebsocket
Websocket
艾鍗科技2.2K views
Building real-time-collaborative-web-applicationsBuilding real-time-collaborative-web-applications
Building real-time-collaborative-web-applications
Harbinger Systems - HRTech Builder of Choice2.5K views
The ASP.NET Web API for BeginnersThe ASP.NET Web API for Beginners
The ASP.NET Web API for Beginners
Kevin Hazzard4.4K views
Unit v Unit v
Unit v
APARNA P43 views
Web-01-HTTP.pptxWeb-01-HTTP.pptx
Web-01-HTTP.pptx
AliZaib7116 views
Http - All you need to knowHttp - All you need to know
Http - All you need to know
Gökhan Şengün758 views
SFDC Outbound IntegrationsSFDC Outbound Integrations
SFDC Outbound Integrations
Sujit Kumar34 views

Recently uploaded(20)

Using Communication and Messaging API in the HTML5 World

  • 1. Using Communication and Messaging API in The HTML5 World Gil Fink CEO, sparXys @gilfink, http://www.gilfink.net
  • 2. Agenda • HTML5: Recap • HTML5 Messaging APIs • HTML5 Communication APIs • Q&A • Summary
  • 3. HTML5: Recap • HTML5 ~= HTML + CSS3 + new JavaScript API • The present and future of the web • W3C Recommendation!
  • 4. What’s Under the HTML5 Umbrella?
  • 6. Cross-Document Messaging • Simple API to send and receive in-window messages • Send messages using the postMessage function • Wire handlers to the message event 6 window.postMessage(message, domain); window.addEventListener("message", function () { // do something }, false);
  • 8. Web Application APIs Support – Cross-Document Messaging http://caniuse.com
  • 9. Cross-Origin Resource Sharing (CORS) • Browsers prevent cross-domain JavaScript requests • CORS enables cross-domain requests as long as: o Response includes Access-Control-Allow-Origin header o XMLHttpRequest supports CORS • XDomainRequest in IE8 and IE9
  • 10. CORS – Client Side var xhr = new XMLHttpRequest(); xhr.open(“get”, url, true); xhr.onload = function () { // instead of using onreadystatechange } xhr.send();
  • 11. CORS – Client Side API • Functions: o abort() – stops a request that is already in progress o send() – sends a request to the server • Event handlers: o onerror – handles request errors o onload – handles request success
  • 12. CORS – Access Control Flow • Client sends ‘Access-Control’ headers during request preflight o Using the OPTIONS HTTP request • Server checks whether the requested resource is allowed • Client checks the preflight response and decides whether to allow the request
  • 13. CORS – Server Side • Use Access-Control headers to allow o Origin – Specific request URI o Method – request method (GET, POST and etc.) o Headers – optional custom headers o Credentials – request credentials
  • 15. Web Application APIs Support – CORS http://caniuse.com
  • 16. Server-Sent Events • Enables servers to push data over HTTP using push protocols • Use the EventSource JavaScript API var source = new EventSource(url); source.onmessage = function (e) { console.log(e.data); }
  • 17. Server-Sent Events - Server • Strict server protocol: data: This is a message. event: add data: 73857293 • Response content type should be text/event-stream • Keeps the connection open
  • 19. Web Application APIs Support – Server-Sent Events http://caniuse.com
  • 22. Web Sockets • Bidirectional communications channels, over a single TCP socket • Don’t allow raw access to the underlying network • Can replace old techniques such as: o Forever frames o Long-polling/comet
  • 23. Web Sockets – Client Request • The client sends a WebSocket handshake request GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== Sec-WebSocket-Origin: http://example.com Sec-WebSocket-Protocol: chat, superchat
  • 24. Web Sockets – Server Response • The server responses with a WebSocket handshake response HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= Sec-WebSocket-Protocol: chat
  • 25. Web Sockets – API • Create a WebSocket object using a URL • Use the WebSocket object’s built-in events: o onopen – fired when a web socket has opened o onmessage – fired when a message has been received o onclose – fired when a web socket has been closed
  • 27. Web Application APIs Support – Web Sockets http://caniuse.com
  • 29. Summary • HTML5 is a W3C recommendation • In this session we explored communication and messaging APIs • You have a lot more to cover
  • 30. Resources • APIs specs – http://www.w3.org/TR/html5/comms.html http://www.w3.org/TR/cors/ http://www.w3.org/TR/eventsource/ http://www.w3.org/TR/websockets/ • My Blog – http://www.gilfink.net • Follow me on Twitter – @gilfink