writing portable websockets in java

106
Writing Portable WebSocket in Java Jeanfrancois Arcand twitter.com/jfarcand

Upload: jfarcand

Post on 24-Jun-2015

2.399 views

Category:

Technology


2 download

DESCRIPTION

Write WebSockets Applications using the Atmosphere Framework, deploy them anywhere, with any browsers.

TRANSCRIPT

Page 1: Writing Portable WebSockets in Java

Writing Portable WebSocket in Java

Jeanfrancois Arcand

twitter.com/jfarcand

Page 2: Writing Portable WebSockets in Java

Pardon?

•Worked 10 years for Sun Microsystems

• Active Commiter on Tomcat 4/5

• Active Commiter GlassFish 1/2/3

• Glassfish v3 extensible container kernel

• Performance/Scalability

• Creator of the Grizzly NIO Framework

•Creator of the AHC (AsyncHttpClient) library

•Creator of Atmosphere

Page 3: Writing Portable WebSockets in Java

Atmosphere

Apache 2 Github ~470 « followers »

20 000 downloads per months

Client + serverSupported

~25 framework

s

Scala, Groovy,

JRuby, Java

1.0.0.beta3

Since 2008

Page 4: Writing Portable WebSockets in Java

Atmosphere

Apache 2 Github ~450 « followers »

20 000 downloads per months

Client + serverSupported

~25 framework

s

Scala, Groovy,

JRuby, Java

1.0.0.beta2

Since2008

EXTREMELY

ACTIVE !!!

Page 5: Writing Portable WebSockets in Java

Today’s Presentation

WebSocket

Page 6: Writing Portable WebSockets in Java

Today’s Presentation

WebSocket

Definition

Page 7: Writing Portable WebSockets in Java

Today’s Presentation

WebSocket

Free for all!!!

Definition

Page 8: Writing Portable WebSockets in Java

Today’s Presentation

WebSocket

Free for all!

C’est quoi

Atmosphere

Free for all!!!

Definition

Page 9: Writing Portable WebSockets in Java

Today’s Presentation

WebSocket

Free for all!

Definition

Atmosphere

Concepts

Page 10: Writing Portable WebSockets in Java

Today’s Presentation

WebSocket

Free for all!

C’est quoi

Atmosphere

DemoConcepts

Free for all!!!

Definition

Page 11: Writing Portable WebSockets in Java

is a web technology providing for bi-directional, full-duplex

communications channels over a single TCP connection. The WebSocket API is being

standardized by the W3C, and the WebSocket protocol has been

standardized by the IETF as RFC 6455

WebSockets

Page 12: Writing Portable WebSockets in Java

Before (Long-Polling)

Browser

Server

Request

Page 13: Writing Portable WebSockets in Java

Before (Long-Polling)

Browser

Request

Server

Page 14: Writing Portable WebSockets in Java

Before (Long-Polling)

Browser

Request

Response

Server

Page 15: Writing Portable WebSockets in Java

Oups!!

Browser

ServerNone

Request

Page 16: Writing Portable WebSockets in Java

Oups!

Browser

Request

Server

Zzzz

Page 17: Writing Portable WebSockets in Java

Better!

Browser

Server

Cache

Request

Page 18: Writing Portable WebSockets in Java

Better!

Request

Server

Cache

Response

Browser

Page 19: Writing Portable WebSockets in Java

Pushing the Protocol (HTTP Streaming)

Browser

Server

Request

Page 20: Writing Portable WebSockets in Java

Pushing the Protocol (HTTP Streaming)

Browser

Request

Server

Page 21: Writing Portable WebSockets in Java

Pushing the Protocol (HTTP Streaming)

Browser

Request

Response

Server

Page 22: Writing Portable WebSockets in Java

Pushing the Protocol (HTTP Streaming)

Browser

Request

Response

Response

Server

Page 23: Writing Portable WebSockets in Java

Oups!!

Browser

Request

Response

Response

Server

Hell!!!

Page 24: Writing Portable WebSockets in Java

Pushing the Protocol (HTTP Streaming)

Browser

Request

Response

Response

Server

Hell!!!

Hack

Page 25: Writing Portable WebSockets in Java

Oups!!!

Browser

Request

Response

Response

Server

Proxy

Page 26: Writing Portable WebSockets in Java

Better

Browser

Request

Response

Response

Server

Cache

Page 27: Writing Portable WebSockets in Java

Better

Browser

Request

Response

Response

Server

Cache« HeartBea

t »

Page 28: Writing Portable WebSockets in Java

Better: Server Side Events (SSE)

Browser

Request

Response

Response

Server

Page 29: Writing Portable WebSockets in Java

Oups!!!

Browser

Request

Response

Response

Server

Proxy

Page 30: Writing Portable WebSockets in Java

Better

Browser

Request

Response

Response

Server

Cache« HeartBea

t »

Page 31: Writing Portable WebSockets in Java

WebSockets

Browser

Server

Hanshake

Page 32: Writing Portable WebSockets in Java

WebSockets

Browser

Server

Hanshake

OK

Page 33: Writing Portable WebSockets in Java

WebSockets

Browser

Server

Request

Page 34: Writing Portable WebSockets in Java

WebSockets

Browser

Request

Server

Page 35: Writing Portable WebSockets in Java

WebSockets

Browser

Server

Response

Page 36: Writing Portable WebSockets in Java

WebSockets

Browser

Server

Request

Page 37: Writing Portable WebSockets in Java

WebSockets

Browser

Server

Request

Request

Page 38: Writing Portable WebSockets in Java

WebSockets

Browser

Request

Request

Server

Page 39: Writing Portable WebSockets in Java

WebSockets

Browser

Server

Response

Response

Page 40: Writing Portable WebSockets in Java

Anytime

Browser

Response

Server

Page 41: Writing Portable WebSockets in Java

Life is good, lalalalalala

Browser

Response

Server

Page 42: Writing Portable WebSockets in Java

Oups!!

Browser

Response

Server

Proxy

Page 43: Writing Portable WebSockets in Java

Better!

Browser

Response

Server

Cache

Page 44: Writing Portable WebSockets in Java

T 127.0.0.1:65062 -> 127.0.0.1:8080 [AP]

GET / HTTP/1.1.

Upgrade: websocket.

Connection: Upgrade.

Host: 127.0.0.1:8080.

Origin: http://127.0.0.1:8080.

Sec-WebSocket-Key: Tz9qdt3lmte6Slf+GvpRqQ==.

Sec-WebSocket-Version: 13.

Sec-WebSocket-Extensions: x-webkit-deflate-frame.

First Request

Page 45: Writing Portable WebSockets in Java

T 127.0.0.1:8080 -> 127.0.0.1:51292 [AP]

HTTP/1.1 101 Switching Protocols.

Upgrade: WebSocket.

Connection: Upgrade.

Sec-WebSocket-Accept: HVXA7SqH5uYeN6aD9tZ0JQbfTJA=.

Response

Page 46: Writing Portable WebSockets in Java

T 127.0.0.1:8080 -> 127.0.0.1:51292 [AP]

HTTP/1.1 101 Switching Protocols.

Upgrade: WebSocket.

Connection: Upgrade.

Sec-WebSocket-Accept: HVXA7SqH5uYeN6aD9tZ0JQbfTJA=.

Life is good, lalalalalala

Page 47: Writing Portable WebSockets in Java

T 127.0.0.1:8080 -> 127.0.0.1:65064 [AP]

HTTP/1.1 501 Not Implemented.

Server: Apache-Coyote/1.1.

X-Atmosphere-error: Websocket protocol not supported.

Transfer-Encoding: chunked.

Date: Fri, 15 Jun 2012 10:06:30 GMT.

Connection: close.

.

OUPS!!!

Page 48: Writing Portable WebSockets in Java

WebSocket API – Standard JavaScript

websocket = new WebSocket(wsUri);

websocket.onopen = function(evt) { …};

websocket.onclose = function(evt) { …};

websocket.onmessage = function(evt) { …};

websocket.onerror = function(evt) { …};

webSocket.send(…)

Page 49: Writing Portable WebSockets in Java

WebSocket API – Java

Jetty 7, GlassFish 3.1, Netty 3, Tomcat 7.0.27, Resin 4, JBoss Plugin

JSR 356: http://jcp.org/en/jsr/detail?id=356

AHC (Client -> De facto)

http://github.com/sonatype/async-http-client

Page 50: Writing Portable WebSockets in Java

Life is good, lalalalalala

Page 51: Writing Portable WebSockets in Java

OUPS!!!

Page 52: Writing Portable WebSockets in Java

Ready?

Tomcat7

Jetty7

Jetty8

GlassFish3.

GlassFish

312

Firefox

Safari

Opera

Chrome

IE

Page 53: Writing Portable WebSockets in Java

Free for all

Tomcat7

Jetty7

Jetty8

GlassFish3.

GlassFish

312

Firefox

Safari

Opera

Chrome

IE

Page 54: Writing Portable WebSockets in Java

Free for all

Tomcat7

Jetty7

Jetty8

GlassFish3.

GlassFish

312

Firefox

Safari

Opera

Chrome

IE

Imdat!!

Page 55: Writing Portable WebSockets in Java

Free for all

Tomcat7

Jetty7

Jetty8

GlassFish3.

GlassFish

312

Firefox

Safari

Opera

Chrome

IE

Imdat!!

Streaming

Page 56: Writing Portable WebSockets in Java

Free for all

Tomcat7

Jetty7

Jetty8

GlassFish3.

GlassFish

312

Firefox

Safari

Opera

Chrome

IE

Imdat!!

Streaming

SSE

Page 57: Writing Portable WebSockets in Java

Free for all

Tomcat7

Jetty7

Jetty8

GlassFish3.

GlassFish

312

Firefox

Safari

Opera

Chrome

IE

Imdat!!

Streaming

SSE

JSONP

Page 58: Writing Portable WebSockets in Java

Free for all

Tomcat7

Jetty7

Jetty8

GlassFish3.

GlassFish

312

Firefox

Safari

Opera

Chrome

IE

Imdat!!

Streaming

SSE

Long Polling

JSONP

Page 59: Writing Portable WebSockets in Java

Tomcat7

Jetty7

Jetty8

GlassFish3.

GlassFish

312

Firefox

Safari

Opera

Chrome

IE

Atmosphere to the

rescue!!!

Page 60: Writing Portable WebSockets in Java

Atmosphere -WebSockets

Tomcat7

Jetty7

Jetty8

GlassFish3.

GlassFish

312

Firefox

Safari

Opera

Chrome

IE

atm

osp

here

.js

Atm

osp

here

API

Page 61: Writing Portable WebSockets in Java

Atmosphere - HTML5 Server Side Events

Tomcat7

Jetty7

Servlet3

WebLogic

GlassFish

312

Firefox

Safari

Opera

Chrome

IE

atm

osp

here

.js

Atm

osp

here

API

Page 62: Writing Portable WebSockets in Java

Atmosphere Long-Polling/HTTP Streaming

JBoss

Jetty7

Servlet3

WebLogic.

GlassFish

Firefox

Safari

Opera

Chrome

IE

atm

osp

here

.js

Atm

osp

here

API

Page 63: Writing Portable WebSockets in Java

Atmosphere

JBoss

Jetty7

Servlet3

WebLogic.

GlassFish

Firefox

Safari

Opera

Chrome

IE

atm

osp

here

.js

Atm

osp

here

API

Same API, transport

independent!!

Page 64: Writing Portable WebSockets in Java

Socket.IO, GWT, Wicket, JSF, etc.

JBoss

Jetty7

Servlet3

WebLogic.

GlassFish

Firefox

Safari

Opera

Chrome

IE

Sock

et.

IO

Atm

osp

here

API

Page 65: Writing Portable WebSockets in Java

PORTABLE!!!!!

JBoss

Jetty7

Servlet3

WebLogic.

GlassFish

Firefox

Safari

Opera

Chrome

IE

Sock

et.

IO

Atm

osp

here

API

PORTABLE

Page 66: Writing Portable WebSockets in Java

•Framework that support ONLY WebSockets

Going into Production?IMPOSSIBLE

Big Big Big Mistake

Page 67: Writing Portable WebSockets in Java

Atmosphere

Page 68: Writing Portable WebSockets in Java

•Suspend: open a channel

•Resume: close a channel

•Broadcast: push message to one or more channel

Definition

Page 69: Writing Portable WebSockets in Java

Components

Handler(s)

Atmosphere.js

Interceptor(s)

Page 70: Writing Portable WebSockets in Java

Components

Handler(s)

Atmosphere.js

Interceptor(s)

Javascript client

Page 71: Writing Portable WebSockets in Java

Components

Handler(s)

Atmosphere.js

Interceptor(s)

Intercepts/Filters

Page 72: Writing Portable WebSockets in Java

Components

Handler(s)

Atmosphere.js

Interceptor(s)

Applications Logic

Page 73: Writing Portable WebSockets in Java

Components

ServletHandler

Atmosphere.js

Interceptor(s)

Your Servlet

Page 74: Writing Portable WebSockets in Java

Components

ServletHandler

Atmosphere.js

Interceptor(s)

Your Servlet Based

Framework

Page 75: Writing Portable WebSockets in Java

Components

ServletHandler

Atmosphere.js

Interceptor(s)

Your Servletws

Page 76: Writing Portable WebSockets in Java

Components

ServletHandler

Atmosphere.js

Interceptor(s)

Your Servletwssse

Page 77: Writing Portable WebSockets in Java

Components

ServletHandler

Atmosphere.js

Interceptor(s)

Your Servletwsssehtt

p

Page 78: Writing Portable WebSockets in Java

Request

Browser

Browser

Browser

Server

Page 79: Writing Portable WebSockets in Java

Request

Browser

Browser

Browser

Server

Page 80: Writing Portable WebSockets in Java

Suspend

Browser

Browser

Browser

Server

Page 81: Writing Portable WebSockets in Java

Suspend

Browser

Browser

Browser

ServerS

Page 82: Writing Portable WebSockets in Java

Suspend

Browser

Browser

Browser

ServerS

Page 83: Writing Portable WebSockets in Java

Suspend

Browser

Browser

Browser

ServerS

Page 84: Writing Portable WebSockets in Java

Broadcast

Browser

Browser

Browser

SB

Server

Page 85: Writing Portable WebSockets in Java

Broadcast

Browser

Browser

Browser

SB

Server

Page 86: Writing Portable WebSockets in Java

Resume

Browser

Browser

Browser

B

Server

R

Page 87: Writing Portable WebSockets in Java

Resume

Browser

Browser

Browser

B

ServerS

Page 88: Writing Portable WebSockets in Java

•Default: in-memory

•Cloud

• RedisBroadcaster

• JMSBroadcaster

• XMPPBroadcaster

• HazelcastBroadcaster

• JGroupsBroascaster

•Multi-Threads, Async I/O par default

Broadcaster

Page 89: Writing Portable WebSockets in Java

Broadcaster Cloud

Browser

Browser

Browser S

Server

ServerS

Page 90: Writing Portable WebSockets in Java

Broadcaster Cloud

Browser

Browser

Browser S

Server

ServerS

Page 91: Writing Portable WebSockets in Java

Broadcaster Cloud

Browser

Browser

Browser S

B

Server

ServerS

Page 92: Writing Portable WebSockets in Java

Broadcaster Cloud

Browser

Browser

Browser

BServer

Server

Page 93: Writing Portable WebSockets in Java

Broadcaster Cloud

Browser

Browser

Browser Server

Server

B

Page 94: Writing Portable WebSockets in Java

Broadcaster Cloud

Browser

Browser

Browser Server

Server

Page 95: Writing Portable WebSockets in Java

•WebSocketHandler

• Only WebSocket (WARNING)!

•AtmosphereHandler

• All transport

•Jersey Resource

• All transport

•Meteor

• All transport

Atmosphere API

Page 96: Writing Portable WebSockets in Java

•WebSocketProtocolDefine your own websocket protocol

•SimpleHttpProtocolWebSocket’s message mapped to HTTP POST, avec read/write asynchrone.

•SwaggerSocket: REST over WebSockets -> Powerful Protocol

WebSocket Sub Protocol

Page 97: Writing Portable WebSockets in Java

Let’s have some fun

Page 98: Writing Portable WebSockets in Java

https://github.com/Atmosphere/atmosphere/blob/master/samples/

chat/src/main/webapp/jquery/application.js#L1

Single Client to Rule Them All

Page 99: Writing Portable WebSockets in Java

https://github.com/Atmosphere/atmosphere/blob/master/samples/websocket-chat/src/main/java/org/

atmosphere/samples/chat/WebSocketChat.java#L33

WebSocket

Page 100: Writing Portable WebSockets in Java

https://github.com/Atmosphere/atmosphere/blob/master/samples/

chat/src/main/java/org/atmosphere/samples/chat/

ChatAtmosphereHandler.java#L33

WebSocket, SSE, long-polling

Page 101: Writing Portable WebSockets in Java

https://github.com/Atmosphere/atmosphere/blob/master/samples/socketio-chat/src/main/java/org/

atmosphere/samples/chat/SocketIOChatAtmosphereHandler.jav

a#L34

Socket IO

Page 102: Writing Portable WebSockets in Java

https://github.com/Atmosphere/atmosphere/blob/master/samples/sse-

rest-chat/src/main/java/org/atmosphere/samples/chat/jersey/

ResourceChat.java#L32

Jersey

Page 103: Writing Portable WebSockets in Java

https://github.com/Atmosphere/atmosphere/blob/master/samples/

jaxrs2-chat/src/main/java/org/atmosphere/samples/chat/jaxrs2/

Jaxrs2Chat.java#L34

JAXRS 2

Page 104: Writing Portable WebSockets in Java

•REST over WebSocket

•JSON Based

•Simple

•Works with HTTP

•Multi-Request/Response

SwaggerSocket

Page 105: Writing Portable WebSockets in Java

Join the Revolution

It’s open source, it’s free to jumps in!!

Conclusion

Page 106: Writing Portable WebSockets in Java

twitter.com/jfarcand twitter.com/atmo_frameworktwitter.com/swaggersocket

http://github.com/Atmosphere/atmospherehttp://github.com/wordnik/

swaggersocket