intro to websockets (in java)

39
WebSockets in Java Jonathan Freeman @freethejazz {CJUG} {Open Software Integrators} { www.osintegrators.com} {@osintegrators}

Upload: osintegrators

Post on 10-May-2015

1.126 views

Category:

Technology


1 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: Intro to WebSockets (in Java)

WebSockets in JavaJonathan Freeman

@freethejazz

{CJUG}

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

Page 2: Intro to WebSockets (in Java)

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

Page 3: Intro to WebSockets (in Java)

Overview

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

WebSockets in JavaJonathan Freeman

@freethejazz

● Gradients of client server communication● Intro to WebSockets● Implementing WebSockets

Page 4: Intro to WebSockets (in Java)

Client/Server Communication

{WebSockets in Java}

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

Page 5: Intro to WebSockets (in Java)

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

WebSockets in JavaJonathan Freeman

@freethejazz

Basic Web Applications

Page 6: Intro to WebSockets (in Java)

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

WebSockets in JavaJonathan Freeman

@freethejazz

Page 7: Intro to WebSockets (in Java)

{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

Page 8: Intro to WebSockets (in Java)

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

WebSockets in JavaJonathan Freeman

@freethejazz

How do we add more interactionbetween the client and the server?

Page 9: Intro to WebSockets (in Java)

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

WebSockets in JavaJonathan Freeman

@freethejazz

Variations on the basic model● AJAX● Long-Polling

New models● SSE● WebSockets

Page 10: Intro to WebSockets (in Java)

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

WebSockets in JavaJonathan Freeman

@freethejazz

AJAX

Page 11: Intro to WebSockets (in Java)

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

WebSockets in JavaJonathan Freeman

@freethejazz

Page 12: Intro to WebSockets (in Java)

{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

Page 13: Intro to WebSockets (in Java)

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

WebSockets in JavaJonathan Freeman

@freethejazz

Long Polling

Page 14: Intro to WebSockets (in Java)

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

WebSockets in JavaJonathan Freeman

@freethejazz

Page 15: Intro to WebSockets (in Java)

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

Page 16: Intro to WebSockets (in Java)

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

WebSockets in JavaJonathan Freeman

@freethejazz

Server-Sent Events

Page 17: Intro to WebSockets (in Java)

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

WebSockets in JavaJonathan Freeman

@freethejazz

Page 18: Intro to WebSockets (in Java)

{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

Page 19: Intro to WebSockets (in Java)
Page 20: Intro to WebSockets (in Java)

Intro to WebSockets

{WebSockets in Java}

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

Page 21: Intro to WebSockets (in Java)

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

WebSockets in JavaJonathan Freeman

@freethejazz

Page 22: Intro to WebSockets (in Java)

{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

Page 23: Intro to WebSockets (in Java)
Page 24: Intro to WebSockets (in Java)

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

WebSockets in JavaJonathan Freeman

@freethejazz

Why use WebSockets?

Page 25: Intro to WebSockets (in Java)

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

WebSockets in JavaJonathan Freeman

@freethejazz

● Chat applications● Instagram-like applications(social streams)● Financial tickers● Multiplayer games

Page 26: Intro to WebSockets (in Java)

Implementing WebSockets

{WebSockets in Java}

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

Page 27: Intro to WebSockets (in Java)

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

Page 28: Intro to WebSockets (in Java)

{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) { ... }

}

});

}

}

Page 29: Intro to WebSockets (in Java)

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

WebSockets in JavaJonathan Freeman

@freethejazz

MessageHandler methods to override:

onMessage

Page 30: Intro to WebSockets (in Java)

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

WebSockets in JavaJonathan Freeman

@freethejazz

ServerEndpointConfig.Builder.create(MySocketEndpoint.class,

"/mywebsocket").build();

Page 31: Intro to WebSockets (in Java)

{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) { ... }

}

}

Page 32: Intro to WebSockets (in Java)

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

WebSockets in JavaJonathan Freeman

@freethejazz

Possible Annotations:

@OnOpen@OnMessage@OnError@OnClose

Page 33: Intro to WebSockets (in Java)

{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) { ... }

}

}

Page 34: Intro to WebSockets (in Java)

{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) { ... }

}

}

Page 35: Intro to WebSockets (in Java)

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}

WebSockets in JavaJonathan Freeman

@freethejazz

In Spring 4...

Page 36: Intro to WebSockets (in Java)

{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

Page 37: Intro to WebSockets (in Java)

{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

Page 38: Intro to WebSockets (in Java)

{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

Page 39: Intro to WebSockets (in Java)

Thank You

{WebSockets in Java}

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}