96
Building WebSocket and Server Side Events application using Atmosphere Jeanfrancois Arcand twitter.com/jfarcand facebook.com/jeanfrancois.arcand.3

Building WebSocket and Server Side Events Applications using Atmosphere

Embed Size (px)

DESCRIPTION

An introduction to WebSocket and Server Side Events using the Atmosphee Framework

Citation preview

Page 1: Building WebSocket and Server Side Events Applications using Atmosphere

Building WebSocket and Server Side Events application using Atmosphere

Jeanfrancois Arcand

twitter.com/jfarcand facebook.com/jeanfrancois.arcand.3

Page 2: Building WebSocket and Server Side Events Applications using Atmosphere

Who I am?

Author of Atmosphere

Author of AHC HTTP/WebSocket

library

Author of Grizzly Framework

10 years @ Sun

Microsystems

Author of GlassFish Micro

Kernel

Page 3: Building WebSocket and Server Side Events Applications using Atmosphere

Atmosphere

Apache 2 Github ~900 « followers »

60 000 unique download per

months

Client + serverSupported

by25 frameworks Scala,

Groovy, JRuby, Java

1.0.9 / 1.1.beta2

Depuis 2008

Page 4: Building WebSocket and Server Side Events Applications using Atmosphere

Atmosphere

Apache 2 Github ~900 « followers »

60 000 unique download per

months

Client + serverSupported

by25 frameworks Scala,

Groovy, JRuby, Java

1.0.9 / 1.1.beta2

Depuis 2008

~1000 followers on Twitter

~400 users

mailing list

Page 5: Building WebSocket and Server Side Events Applications using Atmosphere

WebSocket: A Socket, that’s it!

WebSockets

Page 6: Building WebSocket and Server Side Events Applications using Atmosphere

Conclusion

Page 7: Building WebSocket and Server Side Events Applications using Atmosphere

WebSocket is a web technology providing 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 8: Building WebSocket and Server Side Events Applications using Atmosphere

WARNING!!Nobody is/will/was fired

because of long-polling!!!

Page 9: Building WebSocket and Server Side Events Applications using Atmosphere

Browser Serverrequest

empty response

request

response

event

request

request

responseevent

request

response part

event

response part

event

Polling Long Polling Streaming

Browser Server

Browser Server

request

responseevent

event

request

Response

event

response

event

Browser Server

WebSocket =>

Page 10: Building WebSocket and Server Side Events Applications using Atmosphere

WebSockets

HTML5

Bi-directional

Page 11: Building WebSocket and Server Side Events Applications using Atmosphere

WebSockets

HTML5

Proxy/Firewall

Bi-directional

Page 12: Building WebSocket and Server Side Events Applications using Atmosphere

WebSockets

HTML5

Proxy/Firewall

Bi-directional

« Less network overhead »

Page 13: Building WebSocket and Server Side Events Applications using Atmosphere

WebSockets

HTML5

Proxy/Firewall

Bi-directional

« Less network overhead »

Faster

Page 14: Building WebSocket and Server Side Events Applications using Atmosphere

WebSockets

HTML5

Proxy/Firewall

Bi-directional

« Less network overhead »

Faster than Ajax

SubProtocol

Page 15: Building WebSocket and Server Side Events Applications using Atmosphere

WebSockets

Real time apps

Collaboration

Presence

Notification

Anything!

Page 16: Building WebSocket and Server Side Events Applications using Atmosphere

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 17: Building WebSocket and Server Side Events Applications using Atmosphere

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=.

Second Request

Page 18: Building WebSocket and Server Side Events Applications using Atmosphere

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 if good!

Page 19: Building WebSocket and Server Side Events Applications using Atmosphere

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 20: Building WebSocket and Server Side Events Applications using Atmosphere

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!!!

Atmosphere to the rescue

Page 21: Building WebSocket and Server Side Events Applications using Atmosphere

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 22: Building WebSocket and Server Side Events Applications using Atmosphere

Server – No Standard yet

•Node.js

•Pusher

•Jetty

•GlassFish

•Tomcat

•Apache

•NIO Framework like Netty and Grizzly

Page 23: Building WebSocket and Server Side Events Applications using Atmosphere

WebSocket API – Java

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

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

AHC (Client -> De facto)

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

Page 24: Building WebSocket and Server Side Events Applications using Atmosphere

WebSocket API – Java & Scala

wAsync:

https://github.com/Atmosphere/wasync

SwaggerSocket (REST over WebSocket)

https://github.com/wordnik/swaggersocket

Page 25: Building WebSocket and Server Side Events Applications using Atmosphere

WARNING! Y’en aura pas de facile!!!

Page 26: Building WebSocket and Server Side Events Applications using Atmosphere

Before (Long-Polling)

Browser

Server

Request

Page 27: Building WebSocket and Server Side Events Applications using Atmosphere

Before (Long-Polling)

Browser

Request

Server

Page 28: Building WebSocket and Server Side Events Applications using Atmosphere

Before (Long-Polling)

Browser

Request

Response

Server

Page 29: Building WebSocket and Server Side Events Applications using Atmosphere

Oups!!

Browser

Serverzzzz

Request

Page 30: Building WebSocket and Server Side Events Applications using Atmosphere

Oups!

Browser

Request

Server

Zzzz

Page 31: Building WebSocket and Server Side Events Applications using Atmosphere

Better!

Browser

Server

Cache

Request

Page 32: Building WebSocket and Server Side Events Applications using Atmosphere

Better!

Request

Server

Cache

Response

Browser

Page 33: Building WebSocket and Server Side Events Applications using Atmosphere

Pushing the limits (HTTP Streaming)

Browser

Server

Request

Page 34: Building WebSocket and Server Side Events Applications using Atmosphere

Pushing the limits (HTTP Streaming)

Browser

Request

Server

Page 35: Building WebSocket and Server Side Events Applications using Atmosphere

Pushing the limits (HTTP Streaming)

Browser

Request

Response

Server

Page 36: Building WebSocket and Server Side Events Applications using Atmosphere

Pushing the limits (HTTP Streaming)

Browser

Request

Response

Response

Server

Page 37: Building WebSocket and Server Side Events Applications using Atmosphere

Oups!!

Browser

Request

Response

Response

Server

JS HELL

Page 38: Building WebSocket and Server Side Events Applications using Atmosphere

Pushing the limits (HTTP Streaming)

Browser

Request

Response

Response

Server

L’enfer

Hack

Page 39: Building WebSocket and Server Side Events Applications using Atmosphere

Oups!!!

Browser

Request

Response

Response

Server

Proxy

Page 40: Building WebSocket and Server Side Events Applications using Atmosphere

Better

Browser

Request

Response

Response

Server

Cache

Page 41: Building WebSocket and Server Side Events Applications using Atmosphere

Better

Browser

Request

Response

Response

Server

Cache« HeartBea

t »

Page 42: Building WebSocket and Server Side Events Applications using Atmosphere

Better: Server Side Events (SSE)

Browser

Request

Response

Response

Server

Page 43: Building WebSocket and Server Side Events Applications using Atmosphere

Oups!!!

Browser

Request

Response

Response

Server

Proxy

Page 44: Building WebSocket and Server Side Events Applications using Atmosphere

Better

Browser

Request

Response

Response

Server

Cache« HeartBea

t »

Page 45: Building WebSocket and Server Side Events Applications using Atmosphere

Better

Browser

Request

Response

Response

Server

Cache« HeartBea

t »

Not Supported by

Internet Explorer

Page 46: Building WebSocket and Server Side Events Applications using Atmosphere

WebSockets

Browser

Server

Hanshake

Page 47: Building WebSocket and Server Side Events Applications using Atmosphere

WebSockets

Browser

Server

Hanshake

OK

Page 48: Building WebSocket and Server Side Events Applications using Atmosphere

WebSockets

Browser

Server

Request

Page 49: Building WebSocket and Server Side Events Applications using Atmosphere

WebSockets

Browser

Request

Server

Page 50: Building WebSocket and Server Side Events Applications using Atmosphere

WebSockets

Browser

Server

Response

Page 51: Building WebSocket and Server Side Events Applications using Atmosphere

WebSockets

Browser

Server

Request

Page 52: Building WebSocket and Server Side Events Applications using Atmosphere

WebSockets

Browser

Server

Request

Request

Page 53: Building WebSocket and Server Side Events Applications using Atmosphere

WebSockets

Browser

Request

Request

Server

Page 54: Building WebSocket and Server Side Events Applications using Atmosphere

WebSockets

Browser

Server

Response

Response

Page 55: Building WebSocket and Server Side Events Applications using Atmosphere

Anytime

Browser

Response

Server

Page 56: Building WebSocket and Server Side Events Applications using Atmosphere

Ah la belle vie, la vie, la vie ahah!

Browser

Response

Server

Page 57: Building WebSocket and Server Side Events Applications using Atmosphere

Oups!!

Browser

Response

ServerProx

y

Page 58: Building WebSocket and Server Side Events Applications using Atmosphere

Better!

Browser

Response

Server

Cache

Page 59: Building WebSocket and Server Side Events Applications using Atmosphere

Better!

Browser

Response

Server

Cache« HeartBea

t »

Page 60: Building WebSocket and Server Side Events Applications using Atmosphere

Ah la belle vie, la vie, la vie, ahah

Page 61: Building WebSocket and Server Side Events Applications using Atmosphere

OUPS!!!

Page 62: Building WebSocket and Server Side Events Applications using Atmosphere

Go!!

Tomcat7

Jetty7

Jetty8

GlassFish3.

GlassFish

312

Firefox

Safari

Opera

Chrome

IE

Page 63: Building WebSocket and Server Side Events Applications using Atmosphere

Free for all!

Tomcat7

Jetty7

Jetty8

GlassFish3.

GlassFish

312

Firefox

Safari

Opera

Chrome

IE

Page 64: Building WebSocket and Server Side Events Applications using Atmosphere

Free for all!

Tomcat7

Jetty7

Jetty8

GlassFish3.

GlassFish

312

Firefox

Safari

Opera

Chrome

IE

HELP

Page 65: Building WebSocket and Server Side Events Applications using Atmosphere

Free for all!

Tomcat7

Jetty7

Jetty8

GlassFish3.

GlassFish

312

Firefox

Safari

Opera

Chrome

IE

Au Secour

s!!

Streaming

Page 66: Building WebSocket and Server Side Events Applications using Atmosphere

Free for all!

Tomcat7

Jetty7

Jetty8

GlassFish3.

GlassFish

312

Firefox

Safari

Opera

Chrome

IE

Au Secour

s!!

Streaming

SSE

Page 67: Building WebSocket and Server Side Events Applications using Atmosphere

Free for all!

Tomcat7

Jetty7

Jetty8

GlassFish3.

GlassFish

312

Firefox

Safari

Opera

Chrome

IE

Au Secour

s!!

Streaming

SSE

JSONP

Page 68: Building WebSocket and Server Side Events Applications using Atmosphere

Free for all!

Tomcat7

Jetty7

Jetty8

GlassFish3.

GlassFish

312

Firefox

Safari

Opera

Chrome

IE

Au Secour

s!!

Streaming

SSE

Long Polling

JSONP

Page 69: Building WebSocket and Server Side Events Applications using Atmosphere

Tomcat7

Jetty7

Jetty8

GlassFish3.

GlassFish

312

Firefox

Safari

Opera

Chrome

IE

Atmosphere to the

rescue!!!

Page 70: Building WebSocket and Server Side Events Applications using Atmosphere

Atmosphere -WebSockets

Tomcat7

Jetty7

Jetty8

GlassFish3.

GlassFish

312

Firefox

Safari

Opera

Chrome

IE

atm

osp

here

.js

Atm

osp

here

API

Page 71: Building WebSocket and Server Side Events Applications using Atmosphere

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 72: Building WebSocket and Server Side Events Applications using Atmosphere

Atmosphere Long-Polling/HTTP Streaming

JBoss

Jetty7

Servlet3

WebLogic.

GlassFish

Firefox

Safari

Opera

Chrome

IE

atm

osp

here

.js

Atm

osp

here

API

Page 73: Building WebSocket and Server Side Events Applications using Atmosphere

Atmosphere

JBoss

Jetty7

Servlet3

WebLogic.

GlassFish

Firefox

Safari

Opera

Chrome

IE

atm

osp

here

.js

Atm

osp

here

API

One API to rule them

all

Page 74: Building WebSocket and Server Side Events Applications using Atmosphere

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

JBoss

Jetty7

Servlet3

WebLogic.

GlassFish

Firefox

Safari

Opera

Chrome

IE

Sock

et.

IO

Atm

osp

here

API

Page 75: Building WebSocket and Server Side Events Applications using Atmosphere

PORTABLE!!!!!

JBoss

Jetty7

Servlet3

WebLogic.

GlassFish

Firefox

Safari

Opera

Chrome

IE

Sock

et.

IO

Atm

osp

here

API

PORTABLE

Page 76: Building WebSocket and Server Side Events Applications using Atmosphere

•There are still a lot of dinosaur in the wilderness!

Deploy in Production

impossible

Big Mistake!!!!!

Page 77: Building WebSocket and Server Side Events Applications using Atmosphere

Demo

Page 78: Building WebSocket and Server Side Events Applications using Atmosphere
Page 79: Building WebSocket and Server Side Events Applications using Atmosphere

public interface AtmosphereHandler {

void onRequest(AtmosphereResource resource)

throws IOException;

void onStateChange(AtmosphereResourceEvent

event) throws IOException;

void destroy();

}

AtmosphereHandler

Page 80: Building WebSocket and Server Side Events Applications using Atmosphere

public class ChatAtmosphereHandler implements AtmosphereHandler {

@Override

public void onRequest(AtmosphereResource r) throws IOException {

AtmosphereRequest req = r.getRequest();

if (req.getMethod().equalsIgnoreCase("GET")) {

r.suspend();

} else if (req.getMethod().equalsIgnoreCase("POST")) {

r.getBroadcaster().broadcast(req.getReader().readLine().trim());

}

}

AtmosphereHandler

Page 81: Building WebSocket and Server Side Events Applications using Atmosphere

public void onStateChange(AtmosphereResourceEvent event) throws IOException {

….

if (event.isSuspended()) {

res.getWriter().write(new Data(author, message).toString());

switch (r.transport()) {

case JSONP:

case AJAX:

case LONG_POLLING:

event.getResource().resume();

break;

case WEBSOCKET :

case STREAMING:

res.getWriter().flush();

break;

}

} else if (!event.isResuming()){

event.broadcaster().broadcast(new Data("Someone", "say bye bye!").toString());

}

}

AtmosphereHandler

Page 82: Building WebSocket and Server Side Events Applications using Atmosphere

@AtmosphereHandlerService(

path="/chat",

interceptors = {AtmosphereResourceLifecycleInterceptor.class,

BroadcastOnPostAtmosphereInterceptor.class})

public class ChatRoom extends OnMessage<String> {

private final ObjectMapper mapper = new ObjectMapper();

@Override

public void onMessage(AtmosphereResponse response, String message) {

response.getWriter()

.write(mapper.writeValueAsString(

mapper.readValue(message, Data.class)));

}

}

OnMessage

Page 83: Building WebSocket and Server Side Events Applications using Atmosphere

@ManagedAtmosphereHandlerService (path = "/chat")

public class ChatAtmosphereHandler {

private final ObjectMapper mapper =

new ObjectMapper();

@Message

public void onMessage(AtmosphereResponse response,

String message) throws IOException {

response.getWriter().write(

mapper.writeValueAsString(

mapper.readValue(message, Data.class)));

}

@Managed

Page 84: Building WebSocket and Server Side Events Applications using Atmosphere

@AtmosphereHandlerService(

path = "/chat",

broadcasterCache =

HeaderBroadcasterCache.class,

interceptors = {

AtmosphereResourceLifecycleInterceptor.class,

BroadcastOnPostAtmosphereInterceptor.class,

TrackMessageSizeInterceptor.class,

HeartbeatInterceptor.class})

@Managed are..

Page 85: Building WebSocket and Server Side Events Applications using Atmosphere

@Path("/")

public class ChatResource {

@Suspend(contentType = "application/json")

@GET

public String suspend() {

return "";

}

@Broadcast(writeEntity = false)

@POST

@Produces("application/json")

public Response broadcast(Message message) {

return new Response(message.author, message.message);

}

}

Jersey

Page 86: Building WebSocket and Server Side Events Applications using Atmosphere

var socket = $.atmosphere;

var subSocket;

var transport = 'websocket';

var request = { url: document.location.toString() + 'chat',

contentType : "application/json",

logLevel : 'debug',

shared : true,

transport : transport ,

trackMessageLength : true,

fallbackTransport: 'long-polling'};

Client

Page 87: Building WebSocket and Server Side Events Applications using Atmosphere

request.onOpen = function(response) {

};

request.onTransportFailure = function(errorMsg, request) {

};

request.onReconnect = function (request, response) {

};

request.onMessage = function (response) {

};

request.onClose = function(response) {

}

request.onError = function(response) {

};

subSocket = socket.subscribe(request);

Client

Page 88: Building WebSocket and Server Side Events Applications using Atmosphere

subSocket.push(….);

Client

Page 89: Building WebSocket and Server Side Events Applications using Atmosphere

•Default: in-memory

•Cloud

• RedisBroadcaster

• JMSBroadcaster

• XMPPBroadcaster

• HazelcastBroadcaster

• JGroupsBroascaster

•Multi-Threads, Async I/O par default

Broadcaster

Page 90: Building WebSocket and Server Side Events Applications using Atmosphere

Broadcaster

Page 91: Building WebSocket and Server Side Events Applications using Atmosphere
Page 92: Building WebSocket and Server Side Events Applications using Atmosphere

•WebSocketHandler

• Only WebSocket (WARNING)!

•AtmosphereHandler

• All transports

•Jersey Resource

• All transports

•Meteor

• All transports

Atmosphere Components

Page 93: Building WebSocket and Server Side Events Applications using Atmosphere

Démo

Page 94: Building WebSocket and Server Side Events Applications using Atmosphere

•WebSocketProtocolDefine your own protocol on top of WebSocket

•Default:WebSocket message => POST

•SwaggerSocket: REST over WebSockets -> More information

WebSocket Sub Protocol

Page 95: Building WebSocket and Server Side Events Applications using Atmosphere

PLEASE HELPhttp://github.com/Atmosphere/atmosp

here

Page 96: Building WebSocket and Server Side Events Applications using Atmosphere

twitter.com/jfarcandtwitter.com/atmo_frameworktwitter.com/swaggersocket

http://github.com/Atmosphere/

http://github.com/wordnik/swaggersocket