Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring...

19
Java and WebSocket Petr Kˇ remen [email protected] Winter Term 2018 Petr Kˇ remen ([email protected]) Java and WebSocket Winter Term 2018 1 / 19

Transcript of Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring...

Page 1: Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring Open Chrome Developer Tools Navigate to the web site using Google Chrome Open tab \Network"

Java and WebSocket

Petr Kremen

[email protected]

Winter Term 2018

Petr Kremen ([email protected]) Java and WebSocket Winter Term 2018 1 / 19

Page 2: Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring Open Chrome Developer Tools Navigate to the web site using Google Chrome Open tab \Network"

Contents

1 Basics

2 Web Sockets in Java

Petr Kremen ([email protected]) Java and WebSocket Winter Term 2018 2 / 19

Page 3: Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring Open Chrome Developer Tools Navigate to the web site using Google Chrome Open tab \Network"

Basics

Basics

Petr Kremen ([email protected]) Java and WebSocket Winter Term 2018 3 / 19

Page 4: Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring Open Chrome Developer Tools Navigate to the web site using Google Chrome Open tab \Network"

Basics

XMLHttpRequest

taken from https://devcentral.f5.com/articles/social-media-abcs-x-is-for-xmlhttprequest

Petr Kremen ([email protected]) Java and WebSocket Winter Term 2018 4 / 19

Page 5: Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring Open Chrome Developer Tools Navigate to the web site using Google Chrome Open tab \Network"

Basics

The Story so Far

we have learned technologies to create an application on server-side,

to communicate with a client we use exclusively the HTTP(S)protocol, typically throught REST.

Problem

What to do when new data on server appear and the client doesnot know ?

Petr Kremen ([email protected]) Java and WebSocket Winter Term 2018 5 / 19

Page 6: Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring Open Chrome Developer Tools Navigate to the web site using Google Chrome Open tab \Network"

Basics

Simple Solution Using HTTP

Petr Kremen ([email protected]) Java and WebSocket Winter Term 2018 6 / 19

Page 7: Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring Open Chrome Developer Tools Navigate to the web site using Google Chrome Open tab \Network"

Basics

Better Solution Using HTTP – AJAX push (Long Poll)

Petr Kremen ([email protected]) Java and WebSocket Winter Term 2018 7 / 19

Page 8: Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring Open Chrome Developer Tools Navigate to the web site using Google Chrome Open tab \Network"

Basics

Better Solution Using HTTP – AJAX push (Streaming)

Petr Kremen ([email protected]) Java and WebSocket Winter Term 2018 8 / 19

Page 9: Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring Open Chrome Developer Tools Navigate to the web site using Google Chrome Open tab \Network"

Basics

... but Still Not Perfect

the client has to create a new HTTP connection for eachcommunication type (getReports, getUsers, chat)

HTTP headers have to be sent forth and back for each client side

the client has to understand/parse low-level HTTP chunks

WebSockets

represent a systematic solution to HTTP client-server pecularities andprovides a symmetric model for client-server communication.

Petr Kremen ([email protected]) Java and WebSocket Winter Term 2018 9 / 19

Page 10: Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring Open Chrome Developer Tools Navigate to the web site using Google Chrome Open tab \Network"

Basics

Web Socket vs. HTTP

HTTP

designed for “web pages” not “interactive webapplications”traditional request-response modelintensive client-server communication – significantoverhead (HTTP headers)

Web Sockets

bi-directional, full-duplex, real-time,low-latency client/server communications on top ofTCP/IP∈ Java EE 7

Petr Kremen ([email protected]) Java and WebSocket Winter Term 2018 10 / 19

Page 11: Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring Open Chrome Developer Tools Navigate to the web site using Google Chrome Open tab \Network"

Basics

Web Socket Handshake

GET ws://server.org/wsendpointHTTP/1.1

Host: server.orgConnection: UpgradeUpgrade: websocketOrigin: http://server.orgSec-WebSocket-Version: 13Sec-WebSocket-Key:GhkZiCk+0/91FXIbUuRlVQ==

Sec-WebSocket-Extensions:permessage-deflate;client_max_window_bits

HTTP/1.1 101 Switching ProtocolsUpgrade: websocketConnection: upgradeSec-WebSocket-Accept:jpwu9a/SXDrsoRR26Oa3JUEFchY=

Sec-WebSocket-Extensions:permessage-deflate;client_max_window_bits=15

...

Petr Kremen ([email protected]) Java and WebSocket Winter Term 2018 11 / 19

Page 12: Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring Open Chrome Developer Tools Navigate to the web site using Google Chrome Open tab \Network"

Web Sockets in Java

Web Sockets in Java

Petr Kremen ([email protected]) Java and WebSocket Winter Term 2018 12 / 19

Page 13: Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring Open Chrome Developer Tools Navigate to the web site using Google Chrome Open tab \Network"

Web Sockets in Java

Java API for WebSocket (JSR-356)

annotations on POJOs to interact with WebSocket lifecycle events

interfaces to implement to interact with WebSocket lifecycle events

integration with other Java EE technologies – EJB, CDI

Petr Kremen ([email protected]) Java and WebSocket Winter Term 2018 13 / 19

Page 14: Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring Open Chrome Developer Tools Navigate to the web site using Google Chrome Open tab \Network"

Web Sockets in Java

JSR-356 Example

@ServerEndpoint("/actions")public class WebSocketServer {

@OnOpenpublic void open(Session session) { ... }

@OnClosepublic void close(Session session) { ... }

@OnErrorpublic void onError(Throwable error) { ... }

@OnMessagepublic void handleMessage(String message, Session session) {

// actual message processing}

}

Petr Kremen ([email protected]) Java and WebSocket Winter Term 2018 14 / 19

Page 15: Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring Open Chrome Developer Tools Navigate to the web site using Google Chrome Open tab \Network"

Web Sockets in Java

JavaScript Side Example

var socket = new WebSocket("ws://server.org/wsendpoint");

socket.onmessage = onMessage;

function onMessage(event) {var data = JSON.parse(event.data);if (data.action === "addMessage") {

...// actual message processing

}if (data.action === "removeMessage") {

...// actual message processing

}}

Petr Kremen ([email protected]) Java and WebSocket Winter Term 2018 15 / 19

Page 16: Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring Open Chrome Developer Tools Navigate to the web site using Google Chrome Open tab \Network"

Web Sockets in Java

Other Options

Spring has wide support through custom annotations -spring-websocket module

ReactJS has react-websocket module (listener to WebSocketEvents)

Petr Kremen ([email protected]) Java and WebSocket Winter Term 2018 16 / 19

Page 17: Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring Open Chrome Developer Tools Navigate to the web site using Google Chrome Open tab \Network"

Web Sockets in Java

Sample Application – Chat

https://goo.gl/MQMWBf

Petr Kremen ([email protected]) Java and WebSocket Winter Term 2018 17 / 19

Page 18: Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring Open Chrome Developer Tools Navigate to the web site using Google Chrome Open tab \Network"

Web Sockets in Java

Sample Application – Chat Monitoring

Open Chrome Developer Tools

Navigate to the web site using Google Chrome

Open tab “Network” and select the request “actions” (chat)

Select the subtab “Frames” and you can track the WebSocketcommunication

Petr Kremen ([email protected]) Java and WebSocket Winter Term 2018 18 / 19

Page 19: Java and WebSocket - cw.fel.cvut.cz · Web Sockets in Java Sample Application { Chat Monitoring Open Chrome Developer Tools Navigate to the web site using Google Chrome Open tab \Network"

Web Sockets in Java

References

RFC 6455 - The WebSocket Protocolhttps://tools.ietf.org/html/rfc6455

JSR 356: Java API for WebSockethttps://jcp.org/en/jsr/detail?id=356

Java EE 7: Building Web Applications with WebSocket, JavaScript and HTML5http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/HomeWebsocket/WebsocketHome.html

Spring Support for WebSocket http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html

Petr Kremen ([email protected]) Java and WebSocket Winter Term 2018 19 / 19