• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Intro to WebSockets (in Java)
 

Intro to WebSockets (in Java)

on

  • 711 views

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

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.

Statistics

Views

Total Views
711
Views on SlideShare
709
Embed Views
2

Actions

Likes
0
Downloads
149
Comments
0

1 Embed 2

http://www.linkedin.com 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Intro to WebSockets (in Java) Intro to WebSockets (in Java) Presentation Transcript

    • {CJUG} WebSockets in Java Jonathan Freeman @freethejazz {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
    • 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}
    • WebSockets in Java Overview Jonathan Freeman @freethejazz ● Gradients of client server communication ● Intro to WebSockets ● Implementing WebSockets {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
    • {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.com} {@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 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 Java Jonathan Freeman @freethejazz How do we add more interaction between the client and the server? {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
    • 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}
    • WebSockets in Java Jonathan Freeman @freethejazz AJAX {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 ● ● ● ● ● 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 Java Jonathan Freeman @freethejazz Long Polling {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 ● 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 Java Jonathan Freeman @freethejazz Server-Sent Events {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 from server to client Cons ● Communication is only one way {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
    • {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 your particular application Spec still being defined Security vulnerabilities in previous implementation {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
    • WebSockets in Java Jonathan Freeman @freethejazz Why use WebSockets? {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
    • WebSockets in Java Jonathan Freeman @freethejazz ● ● ● ● Chat applications Instagram-like applications(social streams) Financial tickers Multiplayer games {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
    • {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/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}
    • 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}
    • WebSockets in Java Jonathan Freeman @freethejazz MessageHandler methods to override: onMessage {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
    • WebSockets in Java Jonathan Freeman @freethejazz ServerEndpointConfig.Builder.create(MySocketEndpoint.class, "/mywebsocket").build(); {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
    • 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}
    • WebSockets in Java Jonathan Freeman @freethejazz Possible Annotations: @OnOpen @OnMessage @OnError @OnClose {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
    • 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}
    • 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}
    • WebSockets in Java Jonathan Freeman @freethejazz In Spring 4... {Open Software Integrators} { www.osintegrators.com} {@osintegrators}
    • 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}
    • 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}
    • 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}
    • {WebSockets in Java} Thank You {Open Software Integrators} { www.osintegrators.com} {@osintegrators}