writing highly scalable websocket using the atmosphere framework and scala
DESCRIPTION
Devoxx 2011 Atmosphere Framework talk on WebSocket, Redis and ScalaTRANSCRIPT
Writing highly scalable WebSocket using the Atmosphere Framework
Jeanfrancois ArcandWordnik.com
Who is Wordnik
2
• Well Funded Silicon Valley based Startup–Creators of Meaning Discovery Engine
• Wordnik extracts Meaning from Text–Lexicographic and Computational Linguistic algorithms
–Data & Algorithms exposed via Swagger API
• Self-updating English Language Dictionary–Based on in-context usage
–Proprietary Text Corpus• Word Graph relates all words in English
• Micro Service Architecture Infrastructure–Java/Scala/MongoDB 2
Who I amWho I am
3
• Twitter: @jfarcand
3
Creator of Grizzly
Framework
Creator of Grizzly
Framework
Who I amWho I am
44
Creator of Grizzly
Framework
Creator of Grizzly
Framework
Significant contributor of
GlassFish
Significant contributor of
GlassFish
• Twitter: @jfarcand
Who I amWho I am
55
Creator of Grizzly
Framework
Creator of Grizzly
Framework
Significant contributor of
GlassFish
Significant contributor of
GlassFish
Creator of the AsyncHttpClie
nt
Creator of the AsyncHttpClie
nt
• Twitter: @jfarcand
Who I amWho I am
66
Creator of Grizzly
Framework
Creator of Grizzly
Framework
Significant contributor of
GlassFish
Significant contributor of
GlassFish
Creator of AtmosphereCreator of
Atmosphere
Creator of the AsyncHttpClie
nt
Creator of the AsyncHttpClie
nt
• Twitter: @jfarcand
Who I amWho I am
77
Creator of Grizzly
Framework
Creator of Grizzly
Framework
Significant contributor of
GlassFish
Significant contributor of
GlassFish
Creator of AtmosphereCreator of
Atmosphere
Creator of the AsyncHttpClie
nt
Creator of the AsyncHttpClie
nt
• Twitter: @jfarcand
Tomcat, Apache Digester, Sitebricks,
Jersey, Aether/Maven, etc.
Tomcat, Apache Digester, Sitebricks,
Jersey, Aether/Maven, etc.
The Atmosphere FrameworkThe Atmosphere Framework
88
A Portable Comet
Framework
A Portable Comet
Framework
The Atmosphere FrameworkThe Atmosphere Framework
99
A Portable Comet
Framework
A Portable Comet
Framework
Support All native Comet
API and Servlet 3.0
Support All native Comet
API and Servlet 3.0
The Atmosphere FrameworkThe Atmosphere Framework
1010
A Portable Comet
Framework
A Portable Comet
Framework
Support All native Comet
API and Servlet 3.0
Support All native Comet
API and Servlet 3.0
Atmosphere Client JQuery
Plugin
Atmosphere Client JQuery
Plugin
The Atmosphere FrameworkThe Atmosphere Framework
1111
A Portable Comet
Framework
A Portable Comet
Framework
Support All native Comet
API and Servlet 3.0
Support All native Comet
API and Servlet 3.0
Atmosphere Client JQuery
Plugin
Atmosphere Client JQuery
Plugin
Support available
WebSocket Native API
Support available
WebSocket Native API
The Atmosphere FrameworkThe Atmosphere Framework
1212
A Portable Comet
Framework
A Portable Comet
Framework
Support All native Comet
API and Servlet 3.0
Support All native Comet
API and Servlet 3.0
Atmosphere Client JQuery
Plugin
Atmosphere Client JQuery
Plugin
Support available
WebSocket Native API
Support available
WebSocket Native API
Plugs in for Redis, Active
MQ (JMS), JGroups, XMPP,
etc.
Plugs in for Redis, Active
MQ (JMS), JGroups, XMPP,
etc.
The Atmosphere FrameworkThe Atmosphere Framework
1313
A Portable Comet
Framework
A Portable Comet
Framework
Support All native Comet
API and Servlet 3.0
Support All native Comet
API and Servlet 3.0
Atmosphere Client JQuery
Plugin
Atmosphere Client JQuery
Plugin
Support available
WebSocket Native API
Support available
WebSocket Native API
Plugs in for Redis, Active
MQ (JMS), JGroups, XMPP,
etc.
Plugs in for Redis, Active
MQ (JMS), JGroups, XMPP,
etc.
Supported/Integrated by almost
all major frameworks and javascript library
Supported/Integrated by almost
all major frameworks and javascript library
The Atmosphere FrameworkThe Atmosphere Framework
1414
WebSocket Spec is evolving
fast on both Browser and Server Side
WebSocket Spec is evolving
fast on both Browser and Server Side
The Atmosphere FrameworkThe Atmosphere Framework
1515
WebSocket Spec is evolving
fast on both Browser and Server Side
WebSocket Spec is evolving
fast on both Browser and Server Side
Chrome/Safari/Opera: WebSocket
FF: MozWebSocketIE: (crikets …
~10)
Chrome/Safari/Opera: WebSocket
FF: MozWebSocketIE: (crikets …
~10)
The Atmosphere FrameworkThe Atmosphere Framework
1616
WebSocket Spec is evolving
fast on both Browser and Server Side
WebSocket Spec is evolving
fast on both Browser and Server Side
Chrome/Safari/Opera: WebSocket
FF: MozWebSocketIE: (crikets …
~10)
Chrome/Safari/Opera: WebSocket
FF: MozWebSocketIE: (crikets …
~10)
Atmosphere JQuery Plug In hide that and
propose a uniform API
Atmosphere JQuery Plug In hide that and
propose a uniform API
The Atmosphere FrameworkThe Atmosphere Framework
1717
WebSocket Spec is evolving
fast on both Browser and Server Side
WebSocket Spec is evolving
fast on both Browser and Server Side
Chrome/Safari/Opera: WebSocket
FF: MozWebSocketIE: (crikets …
~10)
Chrome/Safari/Opera: WebSocket
FF: MozWebSocketIE: (crikets …
~10)
Atmosphere JQuery Plug In hide that and
propose a uniform API
Atmosphere JQuery Plug In hide that and
propose a uniform API
Decide the best transport,
negociate it with the server
Decide the best transport,
negociate it with the server
Java LandscapeJava Landscape
1818
Jetty 7.0: First to Support WebSocket
Jetty 7.0: First to Support WebSocket
Java LandscapeJava Landscape
1919
Jetty 7.0: First to Support WebSocket
Jetty 7.0: First to Support WebSocket
Jetty 7.2.x ->
API Changes, break
compatibility
Jetty 7.2.x ->
API Changes, break
compatibility
Java LandscapeJava Landscape
2020
Jetty 7.0: First to Support WebSocket
Jetty 7.0: First to Support WebSocket
Jetty 7.2.x ->
API Changes, break
compatibility
Jetty 7.2.x ->
API Changes, break
compatibility
Jetty 7.4.5 and
8.0.2 and up:
Spec change
Jetty 7.4.5 and
8.0.2 and up:
Spec change
Java LandscapeJava Landscape
2121
Jetty 7.0: First to Support WebSocket
Jetty 7.0: First to Support WebSocket
Jetty 7.2.x ->
API Changes, break
compatibility
Jetty 7.2.x ->
API Changes, break
compatibility
Jetty 7.4.5 and
8.0.2 and up:
Spec change
Jetty 7.4.5 and
8.0.2 and up:
Spec change
GlassF
ish 3
.0 -
> som
e su
pport
GlassF
ish 3
.0 -
> som
e su
pport
Java LandscapeJava Landscape
2222
Jetty 7.0: First to Support WebSocket
Jetty 7.0: First to Support WebSocket
Jetty 7.2.x ->
API Changes, break
compatibility
Jetty 7.2.x ->
API Changes, break
compatibility
Jetty 7.4.5 and
8.0.2 and up:
Spec change
Jetty 7.4.5 and
8.0.2 and up:
Spec change
GlassF
ish 3
.0 -
> som
e su
pport
GlassF
ish 3
.0 -
> som
e su
pport
Gla
ssFi
sh 3
.1.2
-> A
PI C
hang
es
Gla
ssFi
sh 3
.1.2
-> A
PI C
hang
es
Java LandscapeJava Landscape
2323
Jetty 7.0: First to Support WebSocket
Jetty 7.0: First to Support WebSocket
Jetty 7.2.x ->
API Changes, break
compatibility
Jetty 7.2.x ->
API Changes, break
compatibility
Jetty 7.4.5 and
8.0.2 and up:
Spec change
Jetty 7.4.5 and
8.0.2 and up:
Spec change
GlassF
ish 3
.0 -
> som
e su
pport
GlassF
ish 3
.0 -
> som
e su
pport
Gla
ssFi
sh 3
.1.2
-> A
PI C
hang
es
Gla
ssFi
sh 3
.1.2
-> A
PI C
hang
es
Tomcat 7
zzzzzzzzzzzz
Tomcat 7
zzzzzzzzzzzz
Java LandscapeJava Landscape
2424
Jetty 7.0: First to Support WebSocket
Jetty 7.0: First to Support WebSocket
Jetty 7.2.x ->
API Changes, break
compatibility
Jetty 7.2.x ->
API Changes, break
compatibility
Jetty 7.4.5 and
8.0.2 and up:
Spec change
Jetty 7.4.5 and
8.0.2 and up:
Spec change
GlassF
ish 3
.0 -
> som
e su
pport
GlassF
ish 3
.0 -
> som
e su
pport
Gla
ssFi
sh 3
.1.2
-> A
PI C
hang
es
Gla
ssFi
sh 3
.1.2
-> A
PI C
hang
es
Tomcat 7
zzzzzzzzzzzz
Tomcat 7
zzzzzzzzzzzz
WOUAAAAAAAAAAAAAAAAtmosphere to the
rescue!!
WOUAAAAAAAAAAAAAAAAtmosphere to the
rescue!!
Java LandscapeJava Landscape
2525
Jetty 7.0: First to Support WebSocket
Jetty 7.0: First to Support WebSocket
Jetty 7.2.x ->
API Changes, break
compatibility
Jetty 7.2.x ->
API Changes, break
compatibility
Jetty 7.4.5 and
8.0.2 and up:
Spec change
Jetty 7.4.5 and
8.0.2 and up:
Spec change
GlassF
ish 3
.0 -
> som
e su
pport
GlassF
ish 3
.0 -
> som
e su
pport
Gla
ssFi
sh 3
.1.2
-> A
PI C
hang
es
Gla
ssFi
sh 3
.1.2
-> A
PI C
hang
es
Tomcat 7
zzzzzzzzzzzz
Tomcat 7
zzzzzzzzzzzz
WOUAAAAAAAAAAAAAAAAtmosphere to the
rescue!!
WOUAAAAAAAAAAAAAAAAtmosphere to the
rescue!!
Are you crazy enough to
rewrite your entire app?
Are you crazy enough to
rewrite your entire app?
Java LandscapeJava Landscape
2626
Jetty 7.0: First to Support WebSocket
Jetty 7.0: First to Support WebSocket
Jetty 7.2.x ->
API Changes, break
compatibility
Jetty 7.2.x ->
API Changes, break
compatibility
Jetty 7.4.5 and
8.0.2 and up:
Spec change
Jetty 7.4.5 and
8.0.2 and up:
Spec change
GlassF
ish 3
.0 -
> som
e su
pport
GlassF
ish 3
.0 -
> som
e su
pport
Gla
ssFi
sh 3
.1.2
-> A
PI C
hang
es
Gla
ssFi
sh 3
.1.2
-> A
PI C
hang
es
Tomcat 7
zzzzzzzzzzzz
Tomcat 7
zzzzzzzzzzzz
WOUAAAAAAAAAAAAAAAAtmosphere to the
rescue!!
WOUAAAAAAAAAAAAAAAAtmosphere to the
rescue!!
Are you crazy enough to
rewrite your entire app?
Are you crazy enough to
rewrite your entire app?Really want to
use proprietary
solution like ?
Really want to use
proprietary solution like ?
How?How?
2727
Integrate with all
framework? Check!
Integrate with all
framework? Check!
How?How?
2828
Integrate with all
framework? Check!
Integrate with all
framework? Check!
Want Servlet API? Check!Want Servlet API? Check!
How?How?
2929
Integrate with all
framework? Check!
Integrate with all
framework? Check!
Want Servlet API? Check!Want Servlet API? Check!
Want REST like API? Check!
Want REST like API? Check!
How?How?
3030
Integrate with all
framework? Check!
Integrate with all
framework? Check!
Want Servlet API? Check!Want Servlet API? Check!
Want REST like API? Check!
Want REST like API? Check!
Fallback to Comet? Check!
Fallback to Comet? Check!
How?How?
3131
Integrate with all
framework? Check!
Integrate with all
framework? Check!
Want Servlet API? Check!Want Servlet API? Check!
Want REST like API? Check!
Want REST like API? Check!
Fallback to Comet? Check!
Fallback to Comet? Check!
Pure WebSocket?
Check!
Pure WebSocket?
Check!
How?How?
3232
Integrate with all
framework? Check!
Integrate with all
framework? Check!
Want Servlet API? Check!Want Servlet API? Check!
Want REST like API? Check!
Want REST like API? Check!
Fallback to Comet? Check!
Fallback to Comet? Check!
Pure WebSocket?
Check!
Pure WebSocket?
Check!
WebSocket Sub Protocol?
Check
WebSocket Sub Protocol?
Check
How?How?
3333
Integrate with all
framework? Check!
Integrate with all
framework? Check!
Want Servlet API? Check!Want Servlet API? Check!
Want REST like API? Check!
Want REST like API? Check!
Fallback to Comet? Check!
Fallback to Comet? Check!
Pure WebSocket?
Check!
Pure WebSocket?
Check!
WebSocket Sub Protocol?
Check
WebSocket Sub Protocol?
Check
Atmosphere provides a uniform API and hides
the underlying transport details
(async I/O included)
Atmosphere provides a uniform API and hides
the underlying transport details
(async I/O included)
ConceptsConcepts
3434
Upgrade/SuspendUpgrade/Suspend
ConceptsConcepts
3535
Upgrade/SuspendUpgrade/Suspend
BroadcastBroadcast
ConceptsConcepts
3636
Upgrade/SuspendUpgrade/Suspend
BroadcastBroadcast
ResumeResume
ConceptsConcepts
3737
Upgrade/SuspendUpgrade/Suspend
BroadcastBroadcast
ResumeResume
Client choose the best
technique (ws or comet)
Client choose the best
technique (ws or comet)
Async I/O(Input/Output
Stream)
Async I/O(Input/Output
Stream)
ConceptsConcepts
3838
Upgrade/SuspendUpgrade/Suspend
BroadcastBroadcast
ResumeResume
Cloud enabled (zzzzzz)
Cloud enabled (zzzzzz)
Async I/O(Input/Output
Stream)
Async I/O(Input/Output
Stream)Survive ProxyCloud EnabledSurvive ProxyCloud Enabled
ZzzzzzzzzEnough Bla Bla Bla,
let’s do Scala!
ZzzzzzzzzEnough Bla Bla Bla,
let’s do Scala!
APIAPI
4040
AtmosphereHandler
AtmosphereHandler
APIAPI
4141
Jersey to the rescue!
(@ and Prog API)
Jersey to the rescue!
(@ and Prog API)
AtmosphereHandler
AtmosphereHandler
APIAPI
4242
AtmosphereHandler
AtmosphereHandler
WebSocketProtocol
WebSocketProtocolJersey to the
rescue!(@ and Prog
API)
Jersey to the rescue!
(@ and Prog API)
APIAPI
4343
AtmosphereHandler
AtmosphereHandler
WebSocketProtocol
WebSocketProtocol
Servlet APIServlet API
Jersey to the rescue!
(@ and Prog API)
Jersey to the rescue!
(@ and Prog API)
The DemoThe Demo
4444
Jetty 8Jetty 8 Tomcat 6
Tomcat 6
RedisRedis
ChromeChrome Redis CLIRedis CLI SafariSafari
WebSocketWebSocketWebSocket FAIL ->
StreamingWebSocket FAIL ->
Streaming
Broadcaster
Broadcaster
Broadcaster
Broadcaster
2005- AtmosphereHandler
45
def onRequest(r: AtmosphereResource[HttpServletRequest, HttpServletResponse]): Unit = {
var req: HttpServletRequest = r.getRequest
var res: HttpServletResponse = r.getResponse
var method: String = req.getMethod
if ("GET".equalsIgnoreCase(method)) {
var b: Broadcaster = lookupBroadcaster(req.getPathInfo)
r.setBroadcaster(b)
if (req.getHeader(HeaderConfig.X_ATMOSPHERE_TRANSPORT).equalsIgnoreCase(HeaderConfig.LONG_POLLING_TRANSPORT)) {
req.setAttribute(ApplicationConfig.RESUME_ON_BROADCAST, true)
r.suspend(-1, false)
} else {
r.suspend(-1)
}
} else if ("POST".equalsIgnoreCase(method)) {
var b: Broadcaster = lookupBroadcaster(req.getPathInfo)
var message: String = req.getReader.readLine
if (message != null && message.indexOf("message") != -1) {
b.broadcast(message.substring("message=".length))
}
}
}
45
http://is.gd/hH3AaHhttp://is.gd/hH3AaH
1999 – Servlet API
46
override def doGet(req: HttpServletRequest, res: HttpServletResponse): Unit = {
var m: org.atmosphere.cpr.Meteor = org.atmosphere.cpr.Meteor.build(req)
m.addListener(new Console)
res.setContentType("text/html;charset=ISO-8859-1")
var b: Broadcaster = lookupBroadcaster(req.getPathInfo)
m.setBroadcaster(b)
if (req.getHeader(HeaderConfig.X_ATMOSPHERE_TRANSPORT).equalsIgnoreCase(HeaderConfig.LONG_POLLING_TRANSPORT)) {
req.setAttribute(ApplicationConfig.RESUME_ON_BROADCAST, true)
m.suspend(-1, false)
}
else {
m.suspend(-1)
}
}
override def doPost(req: HttpServletRequest, res: HttpServletResponse): Unit = {
var b: Broadcaster = lookupBroadcaster(req.getPathInfo)
var message: String = req.getReader.readLine
if (message != null && message.indexOf("message") != -1) {
b.broadcast(message.substring("message=".length))
}
}
46
http://is.gd/GcnG0Ahttp://is.gd/GcnG0A
2007 – REST API
47
@Path("/resource/{topic}")
@Produces(Array("text/html;charset=ISO-8859-1"))
class Resource {
@PathParam("topic") private var topic: Broadcaster = null
@GET
def subscribe: SuspendResponse[String] = {
return new SuspendResponse.SuspendResponseBuilder[String]()
.broadcaster(topic)
.outputComments(true)
.addListener(new Console)
.build
}
@POST
@Broadcast
def publish(@FormParam("message") message: String): Broadcastable = {
return new Broadcastable(message, "", topic)
}
47
http://is.gd/Df9Uvmhttp://is.gd/Df9Uvm
2011 – WebSocket API
4848
http://is.gd/SEa4AKhttp://is.gd/SEa4AK
2011 – Client API
4949
http://is.gd/9jumKthttp://is.gd/9jumKt
SwaggerSocket ProtocolSwaggerSocket Protocol
5050
HTTP/REST Over
WebSocket
HTTP/REST Over
WebSocket
SwaggerSocket ProtocolSwaggerSocket Protocol
5151
HTTP/REST Over
WebSocket
HTTP/REST Over
WebSocket
JSON based protocol
JSON based protocol
SwaggerSocket ProtocolSwaggerSocket Protocol
5252
HTTP/REST Over
WebSocket
HTTP/REST Over
WebSocket
JSON based protocol
JSON based protocol
Support Pipelined Requests
Support Pipelined Requests
SwaggerSocket ProtocolSwaggerSocket Protocol
5353
HTTP/REST Over
WebSocket
HTTP/REST Over
WebSocket
JSON based protocol
JSON based protocol
Support Pipelined Requests
Support Pipelined Requests
Fully ASYNCHRONOUS
Fully ASYNCHRONOUS
SwaggerSocket ProtocolSwaggerSocket Protocol
5454
HTTP/REST Over
WebSocket
HTTP/REST Over
WebSocket
JSON based protocol
JSON based protocol
Support Pipelined Request
Support Pipelined Request
Fully ASYNCHRONOUS
Fully ASYNCHRONOUS
•Work with ANY existing technology, without any
changes
• No need to use Atmosphere API directly
• Protocol easily extensible
•Work with ANY existing technology, without any
changes
• No need to use Atmosphere API directly
• Protocol easily extensible
SwaggerSocket ProtocolSwaggerSocket Protocol
5555
WebServer
WebServer
Atmosphere
Atmosphere
WebSocket
WebSocket
WebSocket
Protocol
WebSocket
Protocol
JerseyJerseyREST Resource
REST Resource
Benchmark
56
Comparing HTTP/keep-alive vs WebSocket
56
Conclusion
57
• Writing WebSocket doesn’t have to be complex
• Use technology/API that you know, don’t re-invent the wheel again!
• Twitter: @atmo_framework• Github: http://github.com/Atmosphere/atmosphere
• Wordnik: http://wordnik.com57