GET /chat HTTP/1.1
Host: example.com:8000
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
GET /stream HTTP/1.1 1
Host: example.com
Accept: text/event-stream
proxies and load balancers reconfiguration
No need to reconfigure!
(it's HTTP)
binary + text
native support browsers
text
API | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
SSE | 6.0 | 6.0 | not supported |
5.0 | 11.5 |
![]() |
|||||
![]() |
… but polyfills
Maximum browser connections per server/proxy
![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|
3237 | 200 | 128 | 2970 | 900 |
beware of DOS!
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
HTTP SSE |
6 | 6 | 8 | 6 | 6 |
=> use query params
var websocket =
new WebSocket('ws://mywebsocketserver/echo');
websocket.onopen = function () {
...
};
websocket.onmessage = function (e) {
...
};
websocket.onerror = function (error) {
...
};
var eventSource =
new EventSource('http://mysseserver/echo');
eventSource.onopen = function () {
...
};
eventSource.onmessage = function (e) {
...
};
eventSource.onerror = function (error) {
...
};
source.addEventListener('foo', function(e) {
...
}, false);
Use case: Préchargement de 500 Tweets sur une page web (nginx configuré en tant que proxy)
![]() |
![]() |
![]() |
|
---|---|---|---|
WebSockets | 16s | 8s | 8s |
SSE | 7s | 5s | 6s |
diff | x2.2 | x1.6 | x13 |
Source http://matthiasnehlsen.com/blog/2013/05/01/server-sent-events-vs-websockets/
(*) WebRTC might be even more suitable
Use the right tools for the right things.
JS Polyfills:
WebSockets / SSE:
Connection limits: