This presentation aggregates common approaches of real-time client-server communications provided by Web Standards. It focuses on comparison of different techniques like polling, comet, Web Sockets, Server-Sent Events.
19. Long Polling
Characteristics
+ Reduced latency
+ Reduced server workload
+ Reduced protocol overhead (HTTP headers)
- Tricky server configuration
- Possible difficulties with intermediaries
- Can cause stoppage of all requests until long polling returns
20. HTTP Streaming
Comet technique similar to long polling
but instead of closing connection, infinitely
pushing data into it
25. HTTP Streaming
Characteristics
-
Patchy browser support (Issue 249132)
Tricky server configuration
Possible difficulties with intermediaries
Can cause stoppage of all requests until long polling returns
+ Reduced latency
+ Reduced server workload
+ Reduced protocol overhead (HTTP headers)
26. HTML5
Pave the Cowpaths
When a practice is already widespread among
authors, consider adopting it rather than
forbidding it or inventing something new.
28. Server-Sent Events
API
var source = new EventSource(‘/events');
source.addEventListener('message', function (e) {
console.log(e.data);
});
source.addEventListener('open', function (e) {
// Connection was opened.
});
source.addEventListener('error', function (e) {
if (e.readyState == EventSource.CLOSED) {
// Connection was closed.
}
});
34. Pull & push model
Flaws
-
HTTP one request – one resource semantics
Semi-duplex communications
Some degree of non-networking latency
Protocol overhead (HTTP headers)
37. Web Sockets
Full-duplex socket connection
Web Socket protocol v13 (RFC 6455) instead of HTTP
Uses HTTP for connection establishment
38. Web Sockets
Connection
var connection = new WebSocket('ws://html5rocks.websocket.org/echo');
Connection established by “upgrading” from HTTP to WebSocket
protocol
Runs via port 80/443 to simplify firewalls traversal
Pseudo schemes: ws, wss
39. Web Sockets
Connection handshake
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
Client sends GET or CONNECT
request to Web Socket endpoint
Upgrade header indicates willing to
upgrade connection from HTTP to
Web Socket
40. Web Sockets
Connection handshake
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
Server responds with 101 status
code and connection upgrade
header
From now on Web Socket protocol
will be used instead of HTTP
41. Web Sockets
API
var connection = new WebSocket('ws://html5rocks.websocket.org/echo');
// When the connection is open, send some data to
the server.
connection.onopen = function () {
// Send the message 'Ping' to the server.
connection.send('Ping');
};
// Log errors
connection.onerror = function (error) {
// Log messages from the server
console.log('WebSocket Error ' + error);
};
connection.onmessage = function (e) {
console.log('Server: ' + e.data);
};
45. Web Sockets
Characteristics
+
+
+
+
Low latency
Low server workload
Low protocol overhead
Full-duplex
- Multiple versions of protocol to support
- Possible difficulties with intermediaries
- Requires up-to-date browser
46. What to choose?
Bleeding Edge
Polling
Simplicity
Comet /
Server-Sent
Events
Web Sockets
WebRTC
Efficiency
47. All in one
It is possible to abstract communication
details away from developer into libraries
49. Caveats
1. Some network topologies may prohibit long-lived connections
2. Intermediaries are still barely aware of Web Sockets
3. Long-lived connections are subject to spontaneous shutdown
4. Long-lived connections can prevent some browsers from spanning
parallel HTTP requests
5. Web Sockets spec has bunch of legacy versions