Date post: | 10-May-2015 |
Category: |
Technology |
Upload: | osintegrators |
View: | 1,122 times |
Download: | 1 times |
WebSockets in JavaJonathan Freeman
@freethejazz
{CJUG}
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
Open Software Integrators● Founded January 2008 by Andrew C. Oliver
○ Durham, NCRevenue 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
WebSockets in Java
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
Jonathan Freeman @freethejazz
Overview
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan Freeman
@freethejazz
● Gradients of client server communication● Intro to WebSockets● Implementing WebSockets
Client/Server Communication
{WebSockets in Java}
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan Freeman
@freethejazz
Basic Web Applications
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan Freeman
@freethejazz
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan 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}
WebSockets in JavaJonathan Freeman
@freethejazz
How do we add more interactionbetween the client and the server?
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan Freeman
@freethejazz
Variations on the basic model● AJAX● Long-Polling
New models● SSE● WebSockets
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan Freeman
@freethejazz
AJAX
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan Freeman
@freethejazz
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan 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}
WebSockets in JavaJonathan Freeman
@freethejazz
Long Polling
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan Freeman
@freethejazz
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan 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}
WebSockets in JavaJonathan Freeman
@freethejazz
Server-Sent Events
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan Freeman
@freethejazz
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan Freeman
@freethejazz
Pros● A real-time communication model from server to client
Cons● Communication is only one way
Intro to WebSockets
{WebSockets in Java}
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan Freeman
@freethejazz
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan 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}
WebSockets in JavaJonathan Freeman
@freethejazz
Why use WebSockets?
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan Freeman
@freethejazz
● Chat applications● Instagram-like applications(social streams)● Financial tickers● Multiplayer games
Implementing WebSockets
{WebSockets in Java}
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan 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 logic4. Deploy!
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan 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}
WebSockets in JavaJonathan Freeman
@freethejazz
MessageHandler methods to override:
onMessage
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan Freeman
@freethejazz
ServerEndpointConfig.Builder.create(MySocketEndpoint.class,
"/mywebsocket").build();
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan 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}
WebSockets in JavaJonathan Freeman
@freethejazz
Possible Annotations:
@OnOpen@OnMessage@OnError@OnClose
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan 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}
WebSockets in JavaJonathan 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}
WebSockets in JavaJonathan Freeman
@freethejazz
In Spring 4...
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan Freeman
@freethejazz
@Controllerpublic 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-0http://assets.spring.io/wp/WebSocketBlogPost.html
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
WebSockets in JavaJonathan 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}
WebSockets in JavaJonathan Freeman
@freethejazz
Image Attribution:
client-server communication diagrams: http://stackoverflow.com/questions/11077857/what-are-long-polling-websockets-server-sent-events-sse-and-comet
Thank You
{WebSockets in Java}
{Open Software Integrators} { www.osintegrators.com} {@osintegrators}