Streamdata.io logo Human Talks

WebSockets & Server-Sent Events

Pourquoi ? Comment ?

WebSockets

  • Push techno
  • W3C
  • 2008

Server-Sent Events

  • Push techno
  • W3C
  • 2006

WebSockets

bi-directional / full-duplex protocol

Server-Sent Events

Server push to client protocol

WebSockets

  • TCP
  • low-level protocol
  • HTTP upgrade handshake
								
GET /chat HTTP/1.1
Host: example.com:8000
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
								
							

Server-Sent Events

HTTP/1.1, what else?
								
GET /stream HTTP/1.1 1
Host: example.com
Accept: text/event-stream
								
							

WebSockets

proxies and load balancers reconfiguration

proxies reconfiguration

be kind with the ops for the reconfiguration

Server-Sent Events


No need to reconfigure!
(it's HTTP)

WebSockets

no reconnection mechanism

Server-Sent Events

reconnection mechanism + retry

WebSockets

binary + text

native support browsers

Server-Sent Events

text

API Chrome Firefox IE Safari Opera
SSE 6.0 6.0 not
supported
5.0 11.5
not supported
not supported

… but polyfills

https://goo.gl/cCMIGf Edge needs you: vote for SSE support! https://goo.gl/HjJDIv
Vote for SSE support in Edge Vote here too for SSE support in Edge Tweet to vote for SSE support in Edge

Maximum browser connections per server/proxy

WebSockets

Chrome Firefox IE Safari Opera
3237 200 128 2970 900

beware of DOS!

Server-Sent Events

Chrome Firefox IE Safari Opera
HTTP
SSE
6 6 8 6 6
a solution is coming: multiplexing HTTP connections with HTTP/2

WebSockets

Server-Sent Events

custom headers not supported

=> use query params

WebSockets

var websocket =
  new WebSocket('ws://mywebsocketserver/echo');
websocket.onopen = function () {
	...
};

websocket.onmessage = function (e) {
	...
};

websocket.onerror = function (error) {
	...
};

Server-Sent Events

var eventSource =
  new EventSource('http://mysseserver/echo');
eventSource.onopen = function () {
	...
};

eventSource.onmessage = function (e) {
	...
};

eventSource.onerror = function (error) {
	...
};
source.addEventListener('foo', function(e) {
  ...
}, false);
	

Some Java servers

Undertow, Tomcat, Jetty, Ratpack, Netty, Spring supports WebSockets and SSE implementations

Client libraries supports

Node.js, Java, Ruby, Python, Haskell, Go, Rust, C, ObjectiveC, ... have WebSockets and SSE implementations thanks to third-party libraries

"Perfs"

Use case: Préchargement de 500 Tweets sur une page web (nginx configuré en tant que proxy)

  Safari Chrome Firefox
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/

Use cases

WebSockets

  • Chat, chat, chat (*)
  • Share living editing
  • GPS GoogleMap-like
  • Games
Chat application

(*) WebRTC might be even more suitable

Server-Sent Events

  • Fintech / Trading
  • Betting
  • Games
  • Realtime timetables
  • Animated data apps (charts, monitoring, etc.)
  • etc.

Conclusion

Use the right tools for the right things.

Use the right tools for the right things!

Thank you!

References

JS Polyfills:

References

WebSockets / SSE:

References

Connection limits: