Taking A Quantum Leap With HTML 5 WebSocket
Taking A Quantum Leap With HTML 5 WebSocket
Html 5 WebSocket:
Taking bi-directional communication on the web to the next
level
Shahriar Hyder
Kaz Software Ltd.
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
Half-Duplex Architecture
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
Polling, Long-Polling, Streaming … Comet —
Headache 2.0
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
Specification Stage
Yawn…
… so why do we need
WebSockets?
Source: http://www.slideshare.net/goberoi/intro-to-websockets
2 good
reasons
Source: http://www.slideshare.net/goberoi/intro-to-websockets
Desire for real-time
Want low latency 2-way communication for:
Multiplayer online games (pong)
Collaboration (live wikis)
Dashboards (financial apps)
Tracking (watch user actions)
Presence (chat with customer support)
Live sports ticker
Updating social streams / Social Networking (Twitter Feed)
Smart power grid
More!
Source: http://www.slideshare.net/goberoi/intro-to-websockets
HTTP doesn’t deliver
People hack around this (see “Comet”)
Polling, long-polling, stream via hidden iframe
BUT these are slow, complex, and bulky
Or rely on plugins:
Flash, SilverLight, Java applets
BUT these don’t work everywhere (phones)
Source: http://www.slideshare.net/goberoi/intro-to-websockets
Damn, this is hairy:
Source: http://www.slideshare.net/ismasan/websockets-and-ruby-eventmachine
Vs. HTTP hacks, WebSockets provide:
Less traffic: since clients don’t need to poll, messages only sent when we have
data
Source: http://www.slideshare.net/goberoi/intro-to-websockets
What are
WebSockets?
+ =?
Source: http://www.slideshare.net/goberoi/intro-to-websockets
Definition
The WebSocket specification—developed as part of the
HTML5 initiative—introduced the WebSocket JavaScript
interface, which defines a full-duplex, bi-directional
communication channel over a single TCP socket over
which messages can be sent between client and server.
The WebSocket standard simplifies much of the
complexity around bi-directional web communication and
connection management.
This allows web developers to establish real time two way
communications with a server using simple JavaScript
without resorting to Flash, Java, Ajax long polling, comet,
forever iframe, or other current workarounds.
HTML5 WebSocket
• W3C API and IETF Protocol
• Full-duplex, single socket
• Enables web pages to communicate with a
remote host
• Traverses firewalls, proxies, and routers
seamlessly
• Leverages Cross-Origin Resource Sharing
(CORS)
• Share port with existing HTTP content
• Dramatic overhead reduction
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
HTML5 WebSocket Schemes
HTML5 WebSocket Schemes
• WebSocket
ws://www.websocket.org/text
• WebSocket Secure
wss://www.websocket.org/encrypted
-text
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
Possible WebSocket Architecture
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
HTML5 WebSocket
• Connection established by upgrading from
the HTTP protocol to the WebSocket
protocol using the same TCP connection
• Once upgraded, WebSocket data frames
can be sent back and forth between the
client and the server in full-duplex mode
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
HTML5 WebSocket Handshake
Client
Client
GET
GET /text
/text HTTP/1.1
HTTP/1.1
Upgrade: WebSocket
Upgrade: WebSocket
Connection:
Connection: Upgrade
Upgrade
Host: www.example.com
Host: www.example.com
Origin:
Origin: http://example.com
http://example.com
WebSocket-Protocol:
WebSocket-Protocol: sample
sample
…\r\n
…\r\n
Server
Server
HTTP/1.1
HTTP/1.1 101
101 WebSocket
WebSocket Protocol
Protocol Handshake
Handshake
Upgrade:
Upgrade: WebSocket
WebSocket
Connection:
Connection: Upgrade
Upgrade
WebSocket-Origin:
WebSocket-Origin: http://example.com
http://example.com
WebSocket-Location:
WebSocket-Location: ws://example.com/demo
ws://example.com/demo
WebSocket-Protocol:
WebSocket-Protocol: sample
sample
…\r\n
…\r\n
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
HTML5 WebSocket Frames
• Frames can be sent full-duplex, in either
direction at the same time
• Each frame of data:
• Starts with a 0x00 byte
• Ends with a 0xFF byte
• Contains UTF-8 data in between
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
Example
• \x00Hello, WebSocket\0xff
• There is no defined maximum size
o If the user agent has content that is too
large to be handled, it must fail the Web Socket
connection
o JavaScript does not allow >4GB of data, so
that is a practical maximum
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
Dramatic Reduction in Network Traffic
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
Overheard…
“Reducing kilobytes of data to 2 bytes…
and reducing latency from 150ms to 50ms
is far more than marginal. In fact, these
two factors alone are enough to make
WebSocket seriously interesting to
Google.”
—Ian Hickson (Google, HTML5 spec lead)
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
Using the WebSocket API
JavaScript
JavaScript
//Checking for browser support
if (window.WebSocket) {
document.getElementById("support").innerHTML =
"HTML5 WebSocket is supported";
} else {
document.getElementById("support").innerHTML =
"HTML5 WebSocket is not supported";
}
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
JavaScript
JavaScript
//Create new WebSocket
var mySocket = new
WebSocket(“ws://www.websocket.org”);
// Associate listeners
mySocket.onopen = function(evt) {
alert(“Connection open…”);
};
mySocket.onmessage = function(evt) {
alert(“Received message: “ + evt.data);
};
mySocket.onclose = function(evt) {
alert(“Connection closed…”);
};
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
JavaScript
JavaScript
// Sending data
mySocket.send(“HTML5 WebSocket Rocks!”);
//Close WebSocket
mySocket.close();
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
Extending WebSocket
• Once you have WebSocket, you can communicate with
WebSocket Servers and back-end servers and directly with
message brokers
• You can extend client-server protocols to the web:
• XMPP, Jabber
• Pub/Sub (Stomp/AMQP)
• Gaming protocols
• Any TCP-based protocol
• Browser becomes a first-class network communication citizen
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
WebSocket Server Support
• Socket.IO (server side implementations for Java and node.js)
• node-websocket-server
• Jetty WebSocketServlet (Java)
• Ruby: Event Machine + em-websocket
• Python: Twisted + txWebSocket
• JavaScript: Node.js + WebSocket module
• Kaazing WebSocket Gateway (production since April 2009)
• phpwebsockets
• web-socket-ruby
• Apache mod-pywebsocket
• JWebSocket
• Yaws (Erlang)
Browser Support
• Chrome 4.0
• Safari 5.0
• Firefox 4 (Beta)
• Opera 10.70
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
DEMO TIME!
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
Quake II Game
http://code.google.com/p/quake2-gwt-port
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
Proxy Servers
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
Proxy server traversal decision tree
Source: http://www.slideshare.net/peterlubbers/html5-web-workersunleashed
Summary
Low latency is the mother of interactivity, and in no place is this
more apparent than on the Web. Every slip of a millisecond equates
to a slower end-user experience, which in turn translates into
elevated risk that a user's eyes will avert elsewhere. Both AJAX and
Comet attempt to obscure latency problems, and certainly address
the issue of user-perceived latency. However, Web Socket removes
the need to obscure such problems and introduces a real solution, one
that does not play tricks on the perception of our end users, but
delivers content in real time with real results.
HTML5 Web Socket provides an enormous step forward in the
scalability of the real-time web. As you have seen here, HTML5 Web
Sockets can provide a 500:1 or - depending on the size of the HTTP
headers - even a 1000:1 reduction in unnecessary HTTP header
traffic and 3:1 reduction in latency. That is not just an incremental
improvement; that is a revolutionary jump - a quantum leap.
Concluding statement
If HTTP did not restrict your creativity, what Web application would YOU create?