Nuts and Bolts of WebSocket Devoxx 2014

220
@arungupta @peterm_kaazing #Devoxx #WebSocket Nuts and Bolts of WebSocket Arun Gupta, Red Hat, @arungupta Peter Moskovits, Kaazing, @peterm_kaazing

description

Nuts and Bolts of WebSocket Devoxx 2014

Transcript of Nuts and Bolts of WebSocket Devoxx 2014

Page 1: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Nuts and Bolts of WebSocket

Arun Gupta, Red Hat, @arungupta Peter Moskovits, Kaazing, @peterm_kaazing

Page 2: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Agenda

• Introduction

• WebSocket and Node.js

• WebSocket using JSR 356

• Server

• Client

• Securing WebSocket

• Embedded WebSocket

• Load Balance WebSocket

• Pub/Sub over WebSocket

• STOMP over WebSocket

• MQTT over WebSocket

• REST and SSE

• Scalability

• Debugging

• Production Tips

2

Page 3: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

The “long” story of WebSocket

3

Page 4: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

“Limitations” of HTTP

4

Page 5: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

“Limitations” of HTTP

• Client-driven

4

Page 6: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

“Limitations” of HTTP

• Client-driven

• Half-duplex

4

Page 7: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

“Limitations” of HTTP

• Client-driven

• Half-duplex

• Verbose

4

Page 8: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

“Limitations” of HTTP

• Client-driven

• Half-duplex

• Verbose

• New TCP connection

4

Page 9: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

“Hello World” HTTPPOST /websocket-vs-rest-payload/webresources/rest HTTP/1.1\r\n Host: localhost:8080\r\n Connection: keep-alive\r\n Content-Length: 11\r\n User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36\r\n Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo\r\n Content-Type: text/plain \r\n Accept: */*\r\n Accept-Encoding: gzip,deflate,sdch\r\n Accept-Language: en-US,en;q=0.8\r\n \r\n

HTTP/1.1 200 OK\r\n Connection: keep-alive\r\n X-Powered-By: Undertow 1\r\n Server: Wildfly 8 \r\n Content-Type: text/plain\r\n Content-Length: 11 \r\n Date: Fri, 21 Feb 2014 21:27:53 GMT \r\n \r\n

5

Page 10: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

“Hello World” HTTPPOST /websocket-vs-rest-payload/webresources/rest HTTP/1.1\r\n Host: localhost:8080\r\n Connection: keep-alive\r\n Content-Length: 11\r\n User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36\r\n Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo\r\n Content-Type: text/plain \r\n Accept: */*\r\n Accept-Encoding: gzip,deflate,sdch\r\n Accept-Language: en-US,en;q=0.8\r\n \r\n

HTTP/1.1 200 OK\r\n Connection: keep-alive\r\n X-Powered-By: Undertow 1\r\n Server: Wildfly 8 \r\n Content-Type: text/plain\r\n Content-Length: 11 \r\n Date: Fri, 21 Feb 2014 21:27:53 GMT \r\n \r\n

663 bytes

5

Page 11: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How WebSocket solves it ?

6

Page 12: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How WebSocket solves it ?

• Bi-directional (client-driven)

6

Page 13: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How WebSocket solves it ?

• Bi-directional (client-driven)

• Full-duplex (half-duplex)

6

Page 14: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How WebSocket solves it ?

• Bi-directional (client-driven)

• Full-duplex (half-duplex)

• Lean protocol (verbose)

6

Page 15: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How WebSocket solves it ?

• Bi-directional (client-driven)

• Full-duplex (half-duplex)

• Lean protocol (verbose)

• Single TCP connection (new TCP)

6

Page 16: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What is WebSocket ?

7

Page 17: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What is WebSocket ?

• Bi-directional, full-duplex, communication channel over a single TCP connection

7

Page 18: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What is WebSocket ?

• Bi-directional, full-duplex, communication channel over a single TCP connection

• Originally proposed as part of HTML5

7

Page 19: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What is WebSocket ?

• Bi-directional, full-duplex, communication channel over a single TCP connection

• Originally proposed as part of HTML5

• IETF-defined Protocol: RFC 6455

7

Page 20: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What is WebSocket ?

• Bi-directional, full-duplex, communication channel over a single TCP connection

• Originally proposed as part of HTML5

• IETF-defined Protocol: RFC 6455

• W3C-defined JavaScript API

7

Page 21: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

8

Page 22: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

• Upgrade HTTP to WebSocket (single TCP connection)

8

Page 23: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

• Upgrade HTTP to WebSocket (single TCP connection)

• Send data frames in both direction (bi-directional)

8

Page 24: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

• Upgrade HTTP to WebSocket (single TCP connection)

• Send data frames in both direction (bi-directional)

• Send messages independent of each other (full-duplex)

8

Page 25: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

• Upgrade HTTP to WebSocket (single TCP connection)

• Send data frames in both direction (bi-directional)

• Send messages independent of each other (full-duplex)

• End the connection

8

Page 26: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

Client Server

9

Page 27: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

Client Server

HTTP

9

Page 28: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

Client Server

HTTP

HTTP

9

Page 29: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

Client Server

HTTP

HTTP

Handshake Request

9

Page 30: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

Client Server

HTTP

HTTP

Handshake Request

Handshake Response

9

Page 31: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Handshake Request

GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Origin: http://example.com Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13

10

Page 32: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Handshake Request

GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Origin: http://example.com Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13

10

Page 33: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Handshake Request

GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Origin: http://example.com Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13

10

Page 34: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Handshake Response

HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= Sec-WebSocket-Protocol: chat

11

Page 35: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Handshake Response

HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= Sec-WebSocket-Protocol: chat

11

Page 36: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Handshake Response

HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= Sec-WebSocket-Protocol: chat

11

Page 37: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

Handshake Request

Handshake Response

WebSocketClient Server

12

Page 38: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

Handshake Request

Handshake Response

WebSocketPeer (Client)

Peer (Server)

12

Page 39: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

WebSocket

Peer (Client)

Peer (Server)

13

Page 40: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

WebSocket

Peer (Client)

Peer (Server)

onOpen onOpen

13

Page 41: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

WebSocket

Peer (Client)

Peer (Server)

onMessage

onOpen onOpen

13

Page 42: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

WebSocket

Peer (Client)

Peer (Server)

onMessage

onOpen onOpen

onMessage

13

Page 43: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

WebSocket

Peer (Client)

Peer (Server)

onMessage

onOpen onOpen

onMessage

onError

13

Page 44: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

WebSocket

Peer (Client)

Peer (Server)

onMessage

onOpen onOpen

onMessage

onError

onMessage

13

Page 45: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

WebSocket

Peer (Client)

Peer (Server)

onMessage

onOpen onOpen

onMessage

onError

onMessage

onClose

13

Page 46: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

How does it work ?

WebSocket

Peer (Client)

Peer (Server)

onMessage

onOpen onOpen

onMessage

onError

onMessage

onClose

Disconnected

13

Page 47: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSocket Subprotocols

14

Page 48: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSocket Subprotocols

• Facilitates application layer protocols

14

Page 49: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSocket Subprotocols

• Facilitates application layer protocols

• Registered in a Subprotocol name registry

14

Page 50: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSocket Subprotocols

• Facilitates application layer protocols

• Registered in a Subprotocol name registry• Identifier, common name, definition

14

Page 51: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSocket Subprotocols

• Facilitates application layer protocols

• Registered in a Subprotocol name registry• Identifier, common name, definition

• www.iana.org/assignments/websocket/websocket.xml#subprotocol-name

14

Page 52: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSocket Subprotocols

• Facilitates application layer protocols

• Registered in a Subprotocol name registry• Identifier, common name, definition

• www.iana.org/assignments/websocket/websocket.xml#subprotocol-name

• STOMP, XMPP, MQTT, SOAP, …

14

Page 53: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSocket Extensions

15

Page 55: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSocket Extensions

• Add capabilities to the base protocol

• Multiplexinghttp://tools.ietf.org/html/draft-tamplin-hybi-google-mux

15

Page 56: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSocket Extensions

• Add capabilities to the base protocol

• Multiplexinghttp://tools.ietf.org/html/draft-tamplin-hybi-google-mux

• Compression: Only non-control frames/messages

15

Page 57: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSocket Extensions

• Add capabilities to the base protocol

• Multiplexinghttp://tools.ietf.org/html/draft-tamplin-hybi-google-mux

• Compression: Only non-control frames/messages• Per-frame

http://tools.ietf.org/html/draft-tyoshino-hybi-websocket-perframe-deflate

15

Page 58: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSocket Extensions

• Add capabilities to the base protocol

• Multiplexinghttp://tools.ietf.org/html/draft-tamplin-hybi-google-mux

• Compression: Only non-control frames/messages• Per-frame

http://tools.ietf.org/html/draft-tyoshino-hybi-websocket-perframe-deflate

• Per-messagehttp://tools.ietf.org/html/draft-ietf-hybi-permessage-compression

15

Page 59: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSocket JavaScript API

www.w3.org/TR/websockets16

Page 60: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Support in Browsers

caniuse.com/websockets

17

Page 61: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket 18

Developer Tools

Page 62: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket 19

Page 63: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Java API for WebSocket

20

Page 64: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Java API for WebSocket

• API for WebSocket server and client endpoint

20

Page 65: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Java API for WebSocket

• API for WebSocket server and client endpoint

• Annotated: @ServerEndpoint, @ClientEndpoint

20

Page 66: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Java API for WebSocket

• API for WebSocket server and client endpoint

• Annotated: @ServerEndpoint, @ClientEndpoint

• Programmatic: Endpoint

20

Page 67: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Java API for WebSocket

• API for WebSocket server and client endpoint

• Annotated: @ServerEndpoint, @ClientEndpoint

• Programmatic: Endpoint

• WebSocket opening handshake negotiation

20

Page 68: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Java API for WebSocket

• API for WebSocket server and client endpoint

• Annotated: @ServerEndpoint, @ClientEndpoint

• Programmatic: Endpoint

• WebSocket opening handshake negotiation

• Lifecycle Callback methods

20

Page 69: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Java API for WebSocket

• API for WebSocket server and client endpoint

• Annotated: @ServerEndpoint, @ClientEndpoint

• Programmatic: Endpoint

• WebSocket opening handshake negotiation

• Lifecycle Callback methods

• Integration with Java EE technologies

20

Page 70: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Annotated Endpointimport javax.websocket.*;

@ServerEndpoint("/hello") public class HelloBean { @OnMessage public String sayHello(String name) { return "Hello " + name;}

21

Page 71: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSocket Annotations

• Class-level annotations

• @ServerEndpoint: Turns a POJO in a server endpoint

• @ClientEndpoint: Turns a POJO in a client endpoint

22

Page 72: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSocket Annotations

• Method-level annotations

• @OnMessage: Intercepts WebSocket messages

• @OnOpen: Intercepts WebSocket open events

• @OnClose: Intercepts WebSocket close events

• @OnError: Intercepts WebSocket error events

23

Page 73: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSocket Annotations

• Parameter-level annotations

• @PathParam: Matches path segment of a URI-template

24

Page 74: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

@ServerEndpoint attributes

25

Page 75: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

@ServerEndpoint attributes

• value: Relative URI or URI template e.g. ‘/hello’ or ‘/chat/{subscriber-level}’

25

Page 76: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

@ServerEndpoint attributes

• value: Relative URI or URI template e.g. ‘/hello’ or ‘/chat/{subscriber-level}’

• decoders: list of message decoder classnames

25

Page 77: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

@ServerEndpoint attributes

• value: Relative URI or URI template e.g. ‘/hello’ or ‘/chat/{subscriber-level}’

• decoders: list of message decoder classnames

• encoders: list of message encoder classnames

25

Page 78: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

@ServerEndpoint attributes

• value: Relative URI or URI template e.g. ‘/hello’ or ‘/chat/{subscriber-level}’

• decoders: list of message decoder classnames

• encoders: list of message encoder classnames

• subprotocols: list of the names of the supported subprotocols

25

Page 79: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Chat Server@ServerEndpoint("/chat") public class ChatBean { static Set<Session> peers = Collections.synchronizedSet("...");

@OnOpen public void onOpen(Session peer) { peers.add(peer); }

@OnClose public void onClose(Session peer) { peers.remove(peer); }

@OnMessage public void message(String message) { for (Session peer : peers) { peer.getBasicRemote().sendObject(message); } } }

26

Page 80: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Chat Server Simplified@ServerEndpoint("/chat")public class ChatBean { @OnMessage public void message(String message, Session endpoint) { for (Session peer : endpoint.getOpenSessions()) { peer.getBasicRemote().sendObject(message); } }}

27

Page 81: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Hello there!

Howdy?

https://github.com/javaee-samples/javaee7-samples/tree/master/websocket/chat

28

Page 82: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Custom Payloads@ServerEndpoint( value="/hello", decoders={MyMessageDecoder.class}, encoders={MyMessageEncoder.class} )public class MyEndpoint { . . .}

29

Page 83: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Custom Payloads: Text decoderpublic class MyMessageDecoder implements Decoder.Text<MyMessage> { public MyMessage decode(String s) { JsonObject jsonObject = Json.createReader("...").readObject(); return new MyMessage(jsonObject); }

public boolean willDecode(String string) { . . . return true; }

. . .}

30

Page 84: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Custom Payloads: Text encoderpublic class MyMessageDecoder implements Encoder.Text<MyMessage> { public String encode(MyMessage myMessage) { return myMessage.jsonObject.toString(); }

. . .}

31

Page 85: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Custom Payloads: Binary decoderpublic class MyMessageDecoder implements Decoder.Binary<MyMessage> { public MyMessage decode(byte[] s) { . . . return myMessage; }

public boolean willDecode(byte[] string) { . . . return true; }

. . .}

32

Page 86: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Method Signatures

33

Page 87: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Method Signatures

• Exactly one of the following

• Text: String, boolean, Java primitive or equivalent class, Reader, any type for which there is a decoder

• Binary: byte[], ByteBuffer, byte[] and boolean, ByteBuffer and boolean, InputStream, any type for which there is a decoder

• Pong messages: PongMessage

33

Page 88: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Method Signatures

• Exactly one of the following

• Text: String, boolean, Java primitive or equivalent class, Reader, any type for which there is a decoder

• Binary: byte[], ByteBuffer, byte[] and boolean, ByteBuffer and boolean, InputStream, any type for which there is a decoder

• Pong messages: PongMessage

• An optional Session parameter

33

Page 89: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Method Signatures

• Exactly one of the following

• Text: String, boolean, Java primitive or equivalent class, Reader, any type for which there is a decoder

• Binary: byte[], ByteBuffer, byte[] and boolean, ByteBuffer and boolean, InputStream, any type for which there is a decoder

• Pong messages: PongMessage

• An optional Session parameter

• 0..n String parameters annotated with @PathParam

33

Page 90: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Method Signatures

• Exactly one of the following

• Text: String, boolean, Java primitive or equivalent class, Reader, any type for which there is a decoder

• Binary: byte[], ByteBuffer, byte[] and boolean, ByteBuffer and boolean, InputStream, any type for which there is a decoder

• Pong messages: PongMessage

• An optional Session parameter

• 0..n String parameters annotated with @PathParam

33

Page 91: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Sample Messages

34

Page 92: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Sample Messages

•void m(String s);

34

Page 93: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Sample Messages

•void m(String s);

•void m(Float f, @PathParam(“id”)int id);

34

Page 94: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Sample Messages

•void m(String s);

•void m(Float f, @PathParam(“id”)int id);

•Product m(Reader reader, Session s);

34

Page 95: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Sample Messages

•void m(String s);

•void m(Float f, @PathParam(“id”)int id);

•Product m(Reader reader, Session s);

•void m(byte[] b); or void m(ByteBuffer b);

34

Page 96: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Sample Messages

•void m(String s);

•void m(Float f, @PathParam(“id”)int id);

•Product m(Reader reader, Session s);

•void m(byte[] b); or void m(ByteBuffer b);

•Book m(int i, Session s, @PathParam(“isbn”)String isbn, @PathParam(“store”)String store);

34

Page 97: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket 35

Page 98: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket 35

Page 99: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket 35

Page 100: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket 35

Page 101: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket 35

Page 102: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket 35

Page 103: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

https://github.com/javaee-samples/javaee7-samples/tree/master/websocket/whiteboard

35

Page 104: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

URI Template Matching@ServerEndpoint("/chat/{roomid}") public class ChatServer { @OnMessage public void receiveMessage( @PathParam("roomid")String roomId) { . . . }}

36

Page 105: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Client Endpoint@ClientEndpointpublic class HelloClient { @OnMessage public void message( String message, Session session) { //. . . }}

37

Page 106: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Client Endpoint@ClientEndpointpublic class HelloClient { @OnMessage public void message( String message, Session session) { //. . . }}

WebSocketContainer c = ContainerProvider.getWebSocketContainer();c.connectToServer(HelloClient.class, "hello");

37

Page 107: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

lorem ipsum dolor

lorem ipsum dolor

https://github.com/javaee-samples/javaee7-samples/tree/master/websocket/google-docs

38

Page 108: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Programmatic Endpointpublic class ChatServer extends Endpoint { @Override public void onOpen(Session session) { session.addMessageHandler(new MessageHandler.Text() { public void onMessage(String message) { try { session .getBasicRemote() .sendText(message); } catch (IOException ex) { } } }); }}

39

Page 109: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Programmatic Endpoint Configpublic class MyEndpointConfig implements ServerApplicationConfig {

@Override public Set<ServerEndpointConfig> getEndpointConfigs( Set<Class<? extends Endpoint>> set) { return new HashSet<ServerEndpointConfig>() { { add(ServerEndpointConfig.Builder .create(ChatServer.class, "/chat") .build()); } }; }

@Override public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> set) { return Collections.emptySet(); }}

40

Page 110: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

https://github.com/javaee-samples/javaee7-samples/tree/master/websocket/endpoint-singleton

41

Page 111: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Securing WebSockets

42

Page 112: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Securing WebSockets

• Origin-based security model

42

Page 113: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Securing WebSockets

• Origin-based security model

• Sec-xxx keys can not be set using XMLHttpRequest

• Sec-WebSocket-Key, Sec-WebSocket-Version

42

Page 114: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Securing WebSockets

• Origin-based security model

• Sec-xxx keys can not be set using XMLHttpRequest

• Sec-WebSocket-Key, Sec-WebSocket-Version

• User-based security using Servlet security mechanism• Endpoint mapped by ws:// is protected using security model defined using the corresponding http://

URI

• Authorization defined using <security-constraint>

42

Page 115: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Securing WebSockets

• Origin-based security model

• Sec-xxx keys can not be set using XMLHttpRequest

• Sec-WebSocket-Key, Sec-WebSocket-Version

• User-based security using Servlet security mechanism• Endpoint mapped by ws:// is protected using security model defined using the corresponding http://

URI

• Authorization defined using <security-constraint>

• Transport Confidentiality using wss://• Access allowed over encrypted connection only

42

Page 116: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Cross-Origin Resource Sharing

43

Page 117: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Cross-Origin Resource Sharing

• Relaxes same-origin restrictions to network requests

43

Page 118: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Cross-Origin Resource Sharing

• Relaxes same-origin restrictions to network requests

• Servers include Access-Control-Allow-Origin HTTP header

43

Page 119: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Cross-Origin Resource Sharing

• Relaxes same-origin restrictions to network requests

• Servers include Access-Control-Allow-Origin HTTP header

• Access-Control-* headers

• Max-Age

• Allow-Methods

• Allow-Headers

• …

43

Page 120: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Cross-Origin Resource Sharing

• Relaxes same-origin restrictions to network requests

• Servers include Access-Control-Allow-Origin HTTP header

• Access-Control-* headers

• Max-Age

• Allow-Methods

• Allow-Headers

• …

• www.w3.org/TR/cors/

43

Page 121: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

User-based Security

https://github.com/javaee-samples/javaee7-samples/tree/master/websocket/endpoint-security

44

Page 122: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

TLS-based Security

https://github.com/javaee-samples/javaee7-samples/tree/master/websocket/endpoint-wss

45

Page 123: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Embedded WebSocket

46

Page 124: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Embedded WebSocket

• Undertow New web server in WildFly 8

46

Page 125: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Embedded WebSocket

• Undertow New web server in WildFly 8

• Blocking and non-blocking based on NIO

46

Page 126: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Embedded WebSocket

• Undertow New web server in WildFly 8

• Blocking and non-blocking based on NIO

• Composition/handler-based architecture

46

Page 127: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Embedded WebSocket

• Undertow New web server in WildFly 8

• Blocking and non-blocking based on NIO

• Composition/handler-based architecture

• Lightweight and fully embeddable

46

Page 128: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Embedded WebSocket

• Undertow New web server in WildFly 8

• Blocking and non-blocking based on NIO

• Composition/handler-based architecture

• Lightweight and fully embeddable

• Supports Servlet 3.1 and HTTP Upgrade

46

Page 129: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Embedded WebSocket

• Undertow New web server in WildFly 8

• Blocking and non-blocking based on NIO

• Composition/handler-based architecture

• Lightweight and fully embeddable

• Supports Servlet 3.1 and HTTP Upgrade

• mod_cluster supported

46

Page 130: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Undertow is awesome!

http://www.techempower.com/blog/2014/03/04/one-million-http-rps-without-load-balancing-is-easy/

47

Page 131: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

[email protected]:undertow-io/undertow.git

48

Page 132: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Load Balance WebSocket

49

Page 133: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Load Balance WebSocket

• Reverse proxy

49

Page 134: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Load Balance WebSocket

• Reverse proxy

• Apache module: mod_proxy_wstunnel

49

Page 135: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Load Balance WebSocket

• Reverse proxy

• Apache module: mod_proxy_wstunnel

• Only vertical scaling

49

Page 136: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Load Balance WebSocket

• Reverse proxy

• Apache module: mod_proxy_wstunnel

• Only vertical scaling

• No session replication

49

Page 137: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

http://blog.arungupta.me/2014/08/load-balance-websockets-apache-httpd-techtip48/

50

Page 138: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Pub/Sub over WebSocket

https://github.com/arun-gupta/kaazing-openshift-cartridge

51

Page 139: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

STOMP over WebSocket

52

Page 140: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

STOMP over WebSocket

• STOMP: Simple Text Oriented Messaging Protocol

52

Page 141: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

STOMP over WebSocket

• STOMP: Simple Text Oriented Messaging Protocol

• Interoperable wire format: any client, any broker

52

Page 142: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

STOMP over WebSocket

• STOMP: Simple Text Oriented Messaging Protocol

• Interoperable wire format: any client, any broker

• Messaging interoperability among languages and platforms

52

Page 143: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

STOMP over WebSocket

• STOMP: Simple Text Oriented Messaging Protocol

• Interoperable wire format: any client, any broker

• Messaging interoperability among languages and platforms• Unlike JMS

52

Page 144: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

STOMP over WebSocket

• STOMP: Simple Text Oriented Messaging Protocol

• Interoperable wire format: any client, any broker

• Messaging interoperability among languages and platforms• Unlike JMS

• REST for messaging: CONNECT, SEND, SUBSCRIBE, . . .

52

Page 145: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

STOMP over WebSocket

• STOMP: Simple Text Oriented Messaging Protocol

• Interoperable wire format: any client, any broker

• Messaging interoperability among languages and platforms• Unlike JMS

• REST for messaging: CONNECT, SEND, SUBSCRIBE, . . .

• Map STOMP frames to WebSocket frames

52

Page 146: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

https://github.com/arun-gupta/wildfly-samples/tree/master/websocket-stomp

53

Page 147: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

MQTT over WebSocket

54

Page 148: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

MQTT over WebSocket

• Light-weight pub/sub messaging over TCP

54

Page 149: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

MQTT over WebSocket

• Light-weight pub/sub messaging over TCP

• Designed for “small foot print” or limited bandwidth

54

Page 150: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

MQTT over WebSocket

• Light-weight pub/sub messaging over TCP

• Designed for “small foot print” or limited bandwidth

• MQTT 3.1.1 just became an OASIS standard

54

Page 151: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

MQTT over WebSocket

• Light-weight pub/sub messaging over TCP

• Designed for “small foot print” or limited bandwidth

• MQTT 3.1.1 just became an OASIS standard

• Plain byte array message payload

54

Page 152: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

MQTT over WebSocket

• Light-weight pub/sub messaging over TCP

• Designed for “small foot print” or limited bandwidth

• MQTT 3.1.1 just became an OASIS standard

• Plain byte array message payload

• Quality-of-Service: 0 (TCP), 1 (at least once), 2 (missed messages)

54

Page 153: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

MQTT over WebSocket

• Light-weight pub/sub messaging over TCP

• Designed for “small foot print” or limited bandwidth

• MQTT 3.1.1 just became an OASIS standard

• Plain byte array message payload

• Quality-of-Service: 0 (TCP), 1 (at least once), 2 (missed messages)

• “Last will and testament” publish a message after client goes offline

54

Page 154: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSockethttp://blog.kaazing.com/2013/10/01/controlling-physical-devices-on-the-real-time-web-kaazing-iot-talk-at-javaone-2013/

55

Page 155: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Compare with REST

https://github.com/javaee-samples/javaee7-samples/tree/master/websocket/websocket-vs-rest-payload

https://github.com/javaee-samples/javaee7-samples/tree/master/websocket/websocket-vs-rest

56

Page 156: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Server-Sent Events

57

Page 157: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Server-Sent Events

• Part of HTML5 Specification

57

Page 158: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Server-Sent Events

• Part of HTML5 Specification

• Server-push notifications

57

Page 159: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Server-Sent Events

• Part of HTML5 Specification

• Server-push notifications

• Cross-browser JavaScript API: EventSource

57

Page 160: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Server-Sent Events

• Part of HTML5 Specification

• Server-push notifications

• Cross-browser JavaScript API: EventSource

• Message callbacks

57

Page 161: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Server-Sent Events

• Part of HTML5 Specification

• Server-push notifications

• Cross-browser JavaScript API: EventSource

• Message callbacks

• MIME type: text/eventstream

57

Page 162: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

EventSource API

58

Page 163: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSockets and SSE ?

59

Page 164: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSockets and SSE ?

WebSocket Server-Sent Event

59

Page 165: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSockets and SSE ?

WebSocket Server-Sent Event

Over a custom protocol Over simple HTTP

59

Page 166: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSockets and SSE ?

WebSocket Server-Sent Event

Over a custom protocol Over simple HTTP

Full-duplex, bi-directional Server-push only, client-server OOB

59

Page 167: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSockets and SSE ?

WebSocket Server-Sent Event

Over a custom protocol Over simple HTTP

Full-duplex, bi-directional Server-push only, client-server OOB

Native support in most browsers Can be poly-filled to backport

59

Page 168: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSockets and SSE ?

WebSocket Server-Sent Event

Over a custom protocol Over simple HTTP

Full-duplex, bi-directional Server-push only, client-server OOB

Native support in most browsers Can be poly-filled to backport

Not straight forward protocol Simpler protocol

59

Page 169: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSockets and SSE ?

60

Page 170: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSockets and SSE ?

WebSocket Server-Sent Event

60

Page 171: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSockets and SSE ?

WebSocket Server-Sent Event

Application-specific reconnection Built-in support for reconnection and event id

60

Page 172: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSockets and SSE ?

WebSocket Server-Sent Event

Application-specific reconnection Built-in support for reconnection and event id

Require server and/or proxy configurations No server or proxy change required

60

Page 173: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSockets and SSE ?

WebSocket Server-Sent Event

Application-specific reconnection Built-in support for reconnection and event id

Require server and/or proxy configurations No server or proxy change required

Text and Binary Text only

60

Page 174: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

WebSockets and SSE ?

WebSocket Server-Sent Event

Application-specific reconnection Built-in support for reconnection and event id

Require server and/or proxy configurations No server or proxy change required

Text and Binary Text only

Pre-defined message handlers Pre-defined and arbitrary

60

Page 175: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What makes them scalable ?

61

Page 176: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What makes them scalable ?

• No HTTP/TCP opening/closing connections

61

Page 177: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What makes them scalable ?

• No HTTP/TCP opening/closing connections• Handshake over a single TCP connection

61

Page 178: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What makes them scalable ?

• No HTTP/TCP opening/closing connections• Handshake over a single TCP connection

• HTTP connections have short connection timeout (5 secs for Apache)

61

Page 179: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What makes them scalable ?

• No HTTP/TCP opening/closing connections• Handshake over a single TCP connection

• HTTP connections have short connection timeout (5 secs for Apache)

• Elimination of HTTP headers (cookies, content-type, user-agent, …)

61

Page 180: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What makes them scalable ?

• No HTTP/TCP opening/closing connections• Handshake over a single TCP connection

• HTTP connections have short connection timeout (5 secs for Apache)

• Elimination of HTTP headers (cookies, content-type, user-agent, …)

• Reduces bandwidth dramatically

61

Page 181: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What makes them scalable ?

62

Page 182: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What makes them scalable ?

• Minimal data framing

62

Page 183: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What makes them scalable ?

• Minimal data framing• 2-14 bytes overhead after handshake

62

Page 184: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What makes them scalable ?

• Minimal data framing• 2-14 bytes overhead after handshake

• Maintaining a TCP connection on server is relatively inexpensive

62

Page 185: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What makes them scalable ?

• Minimal data framing• 2-14 bytes overhead after handshake

• Maintaining a TCP connection on server is relatively inexpensive

• Smaller data fragments can be sent without out a request/response

62

Page 186: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What makes them scalable ?

• Minimal data framing• 2-14 bytes overhead after handshake

• Maintaining a TCP connection on server is relatively inexpensive

• Smaller data fragments can be sent without out a request/response

• Live pushes, e.g. stock sticker

62

Page 187: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What makes them scalable ?

• Minimal data framing• 2-14 bytes overhead after handshake

• Maintaining a TCP connection on server is relatively inexpensive

• Smaller data fragments can be sent without out a request/response

• Live pushes, e.g. stock sticker

• Lower latency

62

Page 188: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What makes them scalable ?

63

Page 189: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What makes them scalable ?

• WebSockets are good at scaling vertically

63

Page 190: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What makes them scalable ?

• WebSockets are good at scaling vertically

• HTTP servers are typically configured to log start/completion of HTTP request, not so for WebSocket

63

Page 191: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What makes them scalable ?

• WebSockets are good at scaling vertically

• HTTP servers are typically configured to log start/completion of HTTP request, not so for WebSocket

• Polling and Long-polling is a waste of bandwidth, WebSockets are more elegant

63

Page 192: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

What makes them scalable ?

• WebSockets are good at scaling vertically

• HTTP servers are typically configured to log start/completion of HTTP request, not so for WebSocket

• Polling and Long-polling is a waste of bandwidth, WebSockets are more elegant

• Number of concurrent clients depend upon FD settings

63

Page 193: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Debugging WebSockets

64

Page 194: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket 65

Page 195: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket 66

Page 196: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Debugging WebSockets

67

Page 197: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Production Tips

68

Page 198: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Production Tips

• Proxy can be evil and make WebSockets unusable

68

Page 199: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Production Tips

• Proxy can be evil and make WebSockets unusable• Issue: Remove “Upgrade” header

68

Page 200: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Production Tips

• Proxy can be evil and make WebSockets unusable• Issue: Remove “Upgrade” header

• Fix: Set timeout, remove after onOpen called

68

Page 201: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Production Tips

• Proxy can be evil and make WebSockets unusable• Issue: Remove “Upgrade” header

• Fix: Set timeout, remove after onOpen called

• Issue: Close connection after X idle time

68

Page 202: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Production Tips

• Proxy can be evil and make WebSockets unusable• Issue: Remove “Upgrade” header

• Fix: Set timeout, remove after onOpen called

• Issue: Close connection after X idle time

• Fix: Application-level heartbeat

68

Page 203: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Production Tips

• Proxy can be evil and make WebSockets unusable• Issue: Remove “Upgrade” header

• Fix: Set timeout, remove after onOpen called

• Issue: Close connection after X idle time

• Fix: Application-level heartbeat

• Issue: Not allow to pass through at all

68

Page 204: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Production Tips

• Proxy can be evil and make WebSockets unusable• Issue: Remove “Upgrade” header

• Fix: Set timeout, remove after onOpen called

• Issue: Close connection after X idle time

• Fix: Application-level heartbeat

• Issue: Not allow to pass through at all

• Fix: Fall back on long-polling

68

Page 205: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Production Tips

69

Page 206: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Production Tips

• Load Balancer

69

Page 207: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Production Tips

• Load Balancer• Issue: Don’t work with WebSocket, e.g. Amazon ELB

69

Page 208: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Production Tips

• Load Balancer• Issue: Don’t work with WebSocket, e.g. Amazon ELB

• Fix: ELB configured to use TCP, but no session affinity

69

Page 209: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Production Tips

• Load Balancer• Issue: Don’t work with WebSocket, e.g. Amazon ELB

• Fix: ELB configured to use TCP, but no session affinity

• Browsers

69

Page 210: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Production Tips

• Load Balancer• Issue: Don’t work with WebSocket, e.g. Amazon ELB

• Fix: ELB configured to use TCP, but no session affinity

• Browsers• Issue: IE 6, 7, 8, 9 and Safari 5 do not support WebSocket

69

Page 211: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Production Tips

• Load Balancer• Issue: Don’t work with WebSocket, e.g. Amazon ELB

• Fix: ELB configured to use TCP, but no session affinity

• Browsers• Issue: IE 6, 7, 8, 9 and Safari 5 do not support WebSocket

• Fix: Fallback using Atmosphere, Socket.IO, SockJS

69

Page 212: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Production Tips

• Load Balancer• Issue: Don’t work with WebSocket, e.g. Amazon ELB

• Fix: ELB configured to use TCP, but no session affinity

• Browsers• Issue: IE 6, 7, 8, 9 and Safari 5 do not support WebSocket

• Fix: Fallback using Atmosphere, Socket.IO, SockJS

• Inconsistencies in JSR 356

69

Page 213: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Atmosphere

70

Page 214: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Atmosphere

• Java/JavaScript framework

70

Page 215: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Atmosphere

• Java/JavaScript framework

• Portable asynchronous applications

70

Page 216: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Atmosphere

• Java/JavaScript framework

• Portable asynchronous applications

• Fallback to long-polling in absence of WebSocket

70

Page 217: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Atmosphere

• Java/JavaScript framework

• Portable asynchronous applications

• Fallback to long-polling in absence of WebSocket

• Containers: Netty, Jetty, GlassFish, Tomcat, JBoss, WildFly, WebLogic, Resin, WebSphere

70

Page 218: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Atmosphere

• Java/JavaScript framework

• Portable asynchronous applications

• Fallback to long-polling in absence of WebSocket

• Containers: Netty, Jetty, GlassFish, Tomcat, JBoss, WildFly, WebLogic, Resin, WebSphere

• Browsers: Firefox, Chrome, IE (6x+), Opera, Safari, Android

70

Page 219: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Atmosphere

• Java/JavaScript framework

• Portable asynchronous applications

• Fallback to long-polling in absence of WebSocket

• Containers: Netty, Jetty, GlassFish, Tomcat, JBoss, WildFly, WebLogic, Resin, WebSphere

• Browsers: Firefox, Chrome, IE (6x+), Opera, Safari, Android

https://github.com/javaee-samples/javaee7-samples/tree/master/websocket/atmosphere-chat

70

Page 220: Nuts and Bolts of WebSocket Devoxx 2014

@arungupta @peterm_kaazing#Devoxx #WebSocket

Resources

• Material: github.com/arun-gupta/nuts-and-bolts-of-websocket

71