0
{CJUG}

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

Open Software Integrators
●

Jonathan Freeman
@freethejazz

Founded January 2008 by Andrew C. Oliver
○...
WebSockets in Java

Overview

Jonathan Freeman
@freethejazz

● Gradients of client server communication
● Intro to WebSock...
{WebSockets in Java}

Client/Server
Communication

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

Basic Web Applications

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

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

Pros
●
●
●

Simple
Client gets what it wants(and when)
Minimal interacti...
WebSockets in Java
Jonathan Freeman
@freethejazz

How do we add more interaction
between the client and the server?

{Open...
WebSockets in Java
Jonathan Freeman
@freethejazz

Variations on the basic model
● AJAX
● Long-Polling
New models
● SSE
● W...
WebSockets in Java
Jonathan Freeman
@freethejazz

AJAX

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

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

Pros
●
●
●
●
●

Simple
Client gets what it wants(and when)
Minimal inter...
WebSockets in Java
Jonathan Freeman
@freethejazz

Long Polling

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

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

Pros
●

Emulating a more real-time communication model from server to cl...
WebSockets in Java
Jonathan Freeman
@freethejazz

Server-Sent Events

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

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

Pros
●

A real-time communication model from server to client

Cons
●

C...
{WebSockets in Java}

Intro to
WebSockets

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

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

Pros
●

A real-time communication model

Cons
●
●
●

May be overkill for...
WebSockets in Java
Jonathan Freeman
@freethejazz

Why use WebSockets?

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

●
●
●
●

Chat applications
Instagram-like applications(social streams)
F...
{WebSockets in Java}

Implementing
WebSockets

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

1. Create an endpoint class
(programmatic or annotated)
2. Override/anno...
WebSockets in Java
Jonathan Freeman
@freethejazz
Creating the endpoint class
public class MyWebSocketEndoint extends Endpo...
WebSockets in Java
Jonathan Freeman
@freethejazz

MessageHandler methods to override:
onMessage

{Open Software Integrator...
WebSockets in Java
Jonathan Freeman
@freethejazz

ServerEndpointConfig.Builder.create(MySocketEndpoint.class,
"/mywebsocke...
WebSockets in Java
Jonathan Freeman
@freethejazz
Creating the endpoint class
@ServerEndpoint("/mywebsocket")
public class ...
WebSockets in Java
Jonathan Freeman
@freethejazz

Possible Annotations:
@OnOpen
@OnMessage
@OnError
@OnClose

{Open Softwa...
WebSockets in Java
Jonathan Freeman
@freethejazz
Sending a response to a single client
@ServerEndpoint("/mywebsocket")
pub...
WebSockets in Java
Jonathan Freeman
@freethejazz
Sending a response to all connected clients
@ServerEndpoint("/mywebsocket...
WebSockets in Java
Jonathan Freeman
@freethejazz

In Spring 4...

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

@Controller
public class MyWebSocketController {
@RequestMapping(value=”...
WebSockets in Java
Jonathan Freeman
@freethejazz

References:
https://www.openshift.com/blogs/how-to-build-java-websocket-...
WebSockets in Java
Jonathan Freeman
@freethejazz
Image Attribution:
client-server communication diagrams: http://stackover...
{WebSockets in Java}

Thank You

{Open Software Integrators} { www.osintegrators.com} {@osintegrators}
Intro to WebSockets (in Java)
Intro to WebSockets (in Java)
Upcoming SlideShare
Loading in...5
×

Intro to WebSockets (in Java)

1,211

Published on

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.

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,211
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
235
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Intro to WebSockets (in Java)"

  1. 1. {CJUG} WebSockets in Java Jonathan Freeman @freethejazz {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  2. 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. 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}
  4. 4. {WebSockets in Java} Client/Server Communication {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  5. 5. WebSockets in Java Jonathan Freeman @freethejazz Basic Web Applications {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  6. 6. WebSockets in Java Jonathan Freeman @freethejazz {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  7. 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. 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. 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. 10. WebSockets in Java Jonathan Freeman @freethejazz AJAX {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  11. 11. WebSockets in Java Jonathan Freeman @freethejazz {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  12. 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. 13. WebSockets in Java Jonathan Freeman @freethejazz Long Polling {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  14. 14. WebSockets in Java Jonathan Freeman @freethejazz {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  15. 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. 16. WebSockets in Java Jonathan Freeman @freethejazz Server-Sent Events {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  17. 17. WebSockets in Java Jonathan Freeman @freethejazz {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  18. 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. 19. {WebSockets in Java} Intro to WebSockets {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  20. 20. WebSockets in Java Jonathan Freeman @freethejazz {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  21. 21. 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}
  22. 22. WebSockets in Java Jonathan Freeman @freethejazz Why use WebSockets? {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  23. 23. WebSockets in Java Jonathan Freeman @freethejazz ● ● ● ● Chat applications Instagram-like applications(social streams) Financial tickers Multiplayer games {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  24. 24. {WebSockets in Java} Implementing WebSockets {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  25. 25. 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}
  26. 26. 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}
  27. 27. WebSockets in Java Jonathan Freeman @freethejazz MessageHandler methods to override: onMessage {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  28. 28. WebSockets in Java Jonathan Freeman @freethejazz ServerEndpointConfig.Builder.create(MySocketEndpoint.class, "/mywebsocket").build(); {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  29. 29. 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}
  30. 30. WebSockets in Java Jonathan Freeman @freethejazz Possible Annotations: @OnOpen @OnMessage @OnError @OnClose {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  31. 31. 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}
  32. 32. 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}
  33. 33. WebSockets in Java Jonathan Freeman @freethejazz In Spring 4... {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  34. 34. 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}
  35. 35. 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}
  36. 36. 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}
  37. 37. {WebSockets in Java} Thank You {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×