A discussion of WebSockets in general, with a few examples of basic implementation. We'll address the gradients of client-server interaction and how/why to incorporate real time communication in web applications.
2. WebSockets in Java
Open Software Integrators
●
Jonathan Freeman
@freethejazz
Founded January 2008 by Andrew C. Oliver
○ Durham, NC
Revenue and staff has at least doubled every year since
2009.
●
New office (2012) in Chicago, IL
○ We're hiring associate to senior level as well as UI Developers
(JQuery, Javascript, HTML, CSS)
○ Up to 50% travel (probably less), salary + bonus, 401k, health,
etc etc
○ Preferred: Java, Tomcat, JBoss, Hibernate, Spring, RDBMS,
JQuery
○ Nice to have: Hadoop, Neo4j, MongoDB, Ruby a/o at least one
Cloud platform
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
3. WebSockets in Java
Overview
Jonathan Freeman
@freethejazz
● Gradients of client server communication
● Intro to WebSockets
● Implementing WebSockets
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
5. WebSockets in Java
Jonathan Freeman
@freethejazz
Basic Web Applications
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
6. WebSockets in Java
Jonathan Freeman
@freethejazz
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
7. WebSockets in Java
Jonathan Freeman
@freethejazz
Pros
●
●
●
Simple
Client gets what it wants(and when)
Minimal interaction between the server and the client
For the server, this is a highly reactive model. Sits around waiting until it gets a request, then
delivers back whatever is needed.
Cons
●
●
Server cannot initiate the communication, only the client
new client request == new page load
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
8. WebSockets in Java
Jonathan Freeman
@freethejazz
How do we add more interaction
between the client and the server?
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
9. WebSockets in Java
Jonathan Freeman
@freethejazz
Variations on the basic model
● AJAX
● Long-Polling
New models
● SSE
● WebSockets
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
10. WebSockets in Java
Jonathan Freeman
@freethejazz
AJAX
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
11. WebSockets in Java
Jonathan Freeman
@freethejazz
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
12. WebSockets in Java
Jonathan Freeman
@freethejazz
Pros
●
●
●
●
●
Simple
Client gets what it wants(and when)
Minimal interaction between the server and the client
Piecemeal approach adds value to the client
Client can update based on the response from the server without loading a new page.
For the server, this is a still highly reactive model. Sits around waiting until it gets a request, then
delivers back whatever is needed.
Cons
●
●
Server cannot initiate the communication
New client request != new page load
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
13. WebSockets in Java
Jonathan Freeman
@freethejazz
Long Polling
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
14. WebSockets in Java
Jonathan Freeman
@freethejazz
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
15. WebSockets in Java
Jonathan Freeman
@freethejazz
Pros
●
Emulating a more real-time communication model from server to client.
Cons
●
●
Dealing with more longer and repeated connections, so you’ll have to account for that
It’s kind of a hack.
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
16. WebSockets in Java
Jonathan Freeman
@freethejazz
Server-Sent Events
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
17. WebSockets in Java
Jonathan Freeman
@freethejazz
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
18. WebSockets in Java
Jonathan Freeman
@freethejazz
Pros
●
A real-time communication model from server to client
Cons
●
Communication is only one way
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
19.
20. {WebSockets in Java}
Intro to
WebSockets
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
21. WebSockets in Java
Jonathan Freeman
@freethejazz
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
22. WebSockets in Java
Jonathan Freeman
@freethejazz
Pros
●
A real-time communication model
Cons
●
●
●
May be overkill for your particular application
Spec still being defined
Security vulnerabilities in previous implementation
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
23.
24. WebSockets in Java
Jonathan Freeman
@freethejazz
Why use WebSockets?
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
25. WebSockets in Java
Jonathan Freeman
@freethejazz
●
●
●
●
Chat applications
Instagram-like applications(social streams)
Financial tickers
Multiplayer games
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
27. WebSockets in Java
Jonathan Freeman
@freethejazz
1. Create an endpoint class
(programmatic or annotated)
2. Override/annotate the lifecycle methods
(depending on how you create the endpoint)
3. Add your business logic
4. Deploy!
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
28. WebSockets in Java
Jonathan Freeman
@freethejazz
Creating the endpoint class
public class MyWebSocketEndoint extends Endpoint {
@Override
public void onOpen(final Session session, EndpointConfig config) {
session.addMessageHandler(new MessageHandler.Whole<String>() {
@Override
public void onMessage(String msg) {
try {
session.getBasicRemote().sendText(msg);
} catch (IOException e) { ... }
}
});
}
}
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
29. WebSockets in Java
Jonathan Freeman
@freethejazz
MessageHandler methods to override:
onMessage
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
30. WebSockets in Java
Jonathan Freeman
@freethejazz
ServerEndpointConfig.Builder.create(MySocketEndpoint.class,
"/mywebsocket").build();
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
31. WebSockets in Java
Jonathan Freeman
@freethejazz
Creating the endpoint class
@ServerEndpoint("/mywebsocket")
public class MyWebSocketEndpoint {
@OnMessage
public void onMessage(Session session, String message) {
try {
//to do some work
} catch (IOException e) { ... }
}
}
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
32. WebSockets in Java
Jonathan Freeman
@freethejazz
Possible Annotations:
@OnOpen
@OnMessage
@OnError
@OnClose
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
33. WebSockets in Java
Jonathan Freeman
@freethejazz
Sending a response to a single client
@ServerEndpoint("/mywebsocket")
public class MyWebSocketEndpoint {
@OnMessage
public void onMessage(Session session, String message) {
RemoteEndpoint remote = session.getBasicRemote();
try {
remote.sendText(message);
} catch (IOException e) { ... }
}
}
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
34. WebSockets in Java
Jonathan Freeman
@freethejazz
Sending a response to all connected clients
@ServerEndpoint("/mywebsocket")
public class MyWebSocketEndpoint {
@OnMessage
public void onMessage(Session session, String message) {
try {
for (Session sess : session.getOpenSessions()) {
if (sess.isOpen())
sess.getBasicRemote().sendText(message);
}
} catch (IOException e) { ... }
}
}
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
35. WebSockets in Java
Jonathan Freeman
@freethejazz
In Spring 4...
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
36. WebSockets in Java
Jonathan Freeman
@freethejazz
@Controller
public class MyWebSocketController {
@RequestMapping(value=”/mywebsocket”, method=POST)
public void overHttp(String text) {
// ...
}
@MessageMapping("/mywebsocket")
public void overStomp(String text) {
// ...
}
}
http://blog.gopivotal.com/products/websocket-architecture-in-spring-4-0
http://assets.spring.io/wp/WebSocketBlogPost.html
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
37. WebSockets in Java
Jonathan Freeman
@freethejazz
References:
https://www.openshift.com/blogs/how-to-build-java-websocket-applications-using-the-jsr-356-api
http://docs.oracle.com/javaee/7/tutorial/doc/websocket.htm
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
38. WebSockets in Java
Jonathan Freeman
@freethejazz
Image Attribution:
client-server communication diagrams: http://stackoverflow.com/questions/11077857/what-are-longpolling-websockets-server-sent-events-sse-and-comet
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}