SlideShare ist ein Scribd-Unternehmen logo
1 von 27
Downloaden Sie, um offline zu lesen
GWT Web Socket and 
data serialization 
Michele Ficarra 
Software Engineer at Thales Italy S.p.A
Case study 
With GWT this! 
is easy 
Let's focus on the Browser <-> Server communication
“GWT provides an RPC mechanism based on 
Java Servlets to provide access to server-side 
resources. This mechanism includes generation 
of efficient client-side and server-side code to 
serialize objects across the network using 
deferred binding.” 
From gwtproject.org
GWT RPC
GWT RPC 
• Easy to write, hide all AJAX and serialization 
complexity. ! 
• You can use the same POJO in client and server, 
you need only to implement the serializable 
interface.! 
• But …
Push data from the 
server to browser! 
isn’t so easy…
Possible solutions 
• Polling! 
• HTTP Long Polling (COMET)! 
• Web Socket 
Two RFC can help us to choose
“On today's Internet, the Hypertext Transfer 
Protocol (HTTP) is often used (some would say 
abused) to enable asynchronous, "server-initiated" 
communication from a server to a 
client as well as communication from a client to 
a server.” 
From RFC 6202 - Bidirectional HTTP - April 2011
“The WebSocket protocol consists of an 
opening handshake followed by basic message 
framing, layered over TCP. The goal of this 
technology is to provide a mechanism for 
browser-based applications that need two-way 
communication with servers” 
From RFC 6455 - The WebSocket Protocol - December 2011
• WebSocket seems to be the right choice, but GWT 
doesn’t provide natively a way to use it! 
• On Internet we can find a lots of library that can 
help:! 
• Errai - http://erraiframework.org/! 
• Gwt-ws - https://code.google.com/p/gwt-ws/! 
• …! 
• But this time we want to make our hands “dirty” and 
try to do it by ourself!
Web Socket - Client Side 
var websocket = new WebSocket("ws://locahost:8025/echo"); 
! 
websocket.onopen = function(evt) { 
console.log("WebSocket open"); 
}; 
websocket.onclose = function(evt) { 
console.log("WebSocket close"); 
}; 
websocket.onmessage = function(evt) { 
alart(evt.data); 
}; 
websocket.onerror = function(evt) { 
console.log("WebSocket error"); 
}; 
! 
websocket.send("Hello World!"); 
JAVASCRIPT
Web Socket - Client Side 
For use the previous code with GWT we need to write a JSNI wrapper 
package ws; 
import com.google.gwt.core.client.JavaScriptObject; 
! 
public abstract class WebSocket { 
! 
private JavaScriptObject ws; 
! 
public WebSocket(String url) { 
ws = init(url); 
} 
! 
abstract void onClose(); 
abstract void onError(); 
abstract void onMessage(String msg); 
abstract void onOpen(); 
! 
private native JavaScriptObject init(String url) /*-{ 
. . . 
}-*/; 
! 
native void send(String message) /*-{ 
this.@ws.WebSocket::ws.send(message); 
}-*/; 
} 
JAVA
Web Socket - Client Side 
private native JavaScriptObject init(String url) /*-{ 
var websocket = new WebSocket(url); 
var wrapper = this; 
websocket.onopen = function(evt) { 
wrapper.@ws.WebSocket::onOpen()(); 
}; 
websocket.onclose = function(evt) { 
wrapper.@ws.WebSocket::onClose()(); 
}; 
websocket.onmessage = function(evt) { 
wrapper.@ws.WebSocket::onMessage(Ljava/lang/String;)(evt.data); 
}; 
websocket.onerror = function(evt) { 
wrapper.@ws.WebSocket::onError()(); 
}; 
return websocket; 
}-*/; 
JAVA
Web Socket - Server Side 
• For develop the server side we can use the Java 
API for WebSocket (JSR 356), that is part of the 
Java EE 7 standard! 
• If we want use the JSR 356 without a full JEE 7 
container it’s possibile to embed a WS server like 
Tyrus - https://tyrus.java.net/ 
<dependency> 
<groupId>org.glassfish.tyrus</groupId> 
<artifactId>tyrus-server</artifactId> 
<version>1.8.3</version> 
</dependency> 
<dependency> 
<groupId>org.glassfish.tyrus</groupId> 
<artifactId>tyrus-container-grizzly-server</artifactId> 
<version>1.8.3</version> 
</dependency> 
MAVEN
Web Socket - Server Side 
Example of an Echo Web Socket service with JSR 356 
package ws; 
! 
import javax.websocket.OnMessage; 
import javax.websocket.server.ServerEndpoint; 
! 
@ServerEndpoint("/echo") 
public class EchoEndpoint { 
! 
@OnMessage 
public String echo(String message) { 
return message + " (from your server)"; 
} 
! 
} 
JAVA
Web Socket - Server Side 
Start Tyrus Server in Jetty 6 (DevMode) 
package ws; 
! 
import javax.servlet.ServletContextEvent; 
import javax.servlet.ServletContextListener; 
import javax.websocket.DeploymentException; 
! 
import org.glassfish.tyrus.server.Server; 
! 
public class InitListener implements ServletContextListener { 
! 
private Server server = null; 
! 
@Override 
public void contextDestroyed(ServletContextEvent servletContextEvent) { 
server.stop(); 
} 
! 
@Override 
public void contextInitialized(ServletContextEvent servletContextEvent) { 
try { 
server = new Server("localhost", 8025, "/", null, EchoEndpoint.class); 
server.start(); 
} catch (final DeploymentException e1) { 
e1.printStackTrace(); 
} 
} 
} 
JAVA 
WEB.XML 
<listener> 
<listener-class>ws.InitListener</listener-class> 
</listener>
Data Serialization 
• Now we are capable of send string back and 
forward from the browser to the server and 
viceversa! 
• But if we want use this technology in a complex 
environment this result is a little poor. We want 
send Object not String!! 
• The first idea to fix the problem can be to use JSON 
serialization…
• … and after a little search we found a lots of way to 
do that:! 
• http://www.gwtproject.org/doc/latest/tutorial/JSON.html! 
• https://github.com/nmorel/gwt-jackson! 
• …! 
• But every solution require a lot of boilerplate code 
or adding annotation to the data model! 
• We would like something more similar to the GWT 
RPC, that require only the Serializable interface. 
May be it can be reused?
• After searching in the web and in the GWT source 
code the solution come out! 
• We need to define the interface of an RPC and reuse 
the serialization engine already present in GWT
Data Serialization 
public class Message implements Serializable { 
! 
private String data; 
private String username; 
private Date time; 
! 
/* ... */ 
} 
JAVA 
JAVA 
@RemoteServiceRelativePath("MessageService") 
public interface MessageService extends RemoteService { 
Message getMessage(Message message); 
! 
} 
The RPC Serialization is designed for function call. The 
client serialize the function argument and the server the 
return value. So, if we want exchange the same object, it’s 
important that in the service definition the args and the 
return value are the same class
public String serializeMessae(Message message) { 
try { 
SerializationStreamFactory factory = 
(SerializationStreamFactory) GWT.create(MessageService.class); 
SerializationStreamWriter writer = factory.createStreamWriter(); 
writer.writeObject(message); 
final String data = writer.toString(); 
return data; 
} catch (final SerializationException e) { 
e.printStackTrace(); 
} 
return null; 
} 
JAVA 
Data Serialization - Client Side 
Here there is the trick, the Async Service that is usual 
return by the deferred binding is also an instance of a 
SerializationStreamFactory. That can be used for serialize 
and deserialize objects
public Message deserializeMessae(String data) { 
try { 
SerializationStreamFactory factory = 
(SerializationStreamFactory) GWT.create(MessageService.class); 
final SerializationStreamReader streamReader = factory 
.createStreamReader(data); 
final Message message = (Message) streamReader.readObject(); 
return message; 
} catch (final SerializationException e) { 
e.printStackTrace(); 
} 
return null; 
} 
JAVA 
Data Serialization - Client Side
Data Serialization - Server Side 
private Message deserializeMessage(String data) 
throws SerializationException { 
ServerSerializationStreamReader streamReader = 
new ServerSerializationStreamReader( 
Thread.currentThread().getContextClassLoader(), 
new CustomSerializationPolicyProvider()); 
// Filling stream reader with data 
streamReader.prepareToRead(data); 
// Reading deserialized object from the stream 
final Message message = (Message) streamReader.readObject(); 
return message; 
} 
JAVA 
On server side is more or less the same of the client. We 
need an instance of a ServerSerializationStreamReader for 
read the object. 
The only hack is how create a 
CustomSerializationPolicyProvider
Data Serialization - Serialization Policy 
public class CustomSerializationPolicyProvider 
implements SerializationPolicyProvider { 
! 
@Override 
public SerializationPolicy getSerializationPolicy(String moduleBaseURL, String serializationPolicyStrongName) { 
return new SimpleSerializationPolicy(); 
} 
! 
} 
JAVA 
public class SimpleSerializationPolicy extends SerializationPolicy { 
! 
@Override 
public boolean shouldDeserializeFields(Class<?> clazz) { 
return isSerializable(clazz); 
} 
! 
@Override 
public boolean shouldSerializeFields(Class<?> clazz) { 
return isSerializable(clazz); 
} 
! 
/* ... */ 
! 
private boolean isSerializable(Class<?> clazz) { 
if (clazz != null) { 
if (clazz.isPrimitive() 
|| Serializable.class.isAssignableFrom(clazz) 
|| IsSerializable.class.isAssignableFrom(clazz)) { 
return true; 
} 
} 
return false; 
} 
} 
JAVA 
RPC generates a serialization 
policy file during GWT 
compilation. The serialization 
policy file contains a whitelist 
of allowed types which may be 
serialized.! 
In this simple implementation 
there is only the check of the 
Serializable interface.! 
Watch out of what are you 
serializing or you can perform 
problem on client side.
Data Serialization - Server Side 
private String serializeMessage(final Message messageDto) 
throws SerializationException { 
! 
ServerSerializationStreamWriter serverSerializationStreamWriter = 
new ServerSerializationStreamWriter(new SimpleSerializationPolicy()); 
! 
serverSerializationStreamWriter.writeObject(messageDto); 
String result = serverSerializationStreamWriter.toString(); 
return result; 
} 
JAVA
Data Serialization - OnMessage 
Now we have all pieces for finish the OnMessage method 
@OnMessage 
public void onMessage(String message, Session session) { 
try { 
! 
Message messageDto = deserializeMessage(message); 
! 
String result = serializeMessage(messageDto); 
! 
for (Session s : session.getOpenSessions()) { 
if (s.isOpen()) { 
s.getBasicRemote().sendText(result); 
} 
} 
} catch (final SerializationException | IOException e) { 
logger.log(Level.WARNING, "Error", e); 
} 
} 
JAVA 
Deserialization of an 
incoming message 
from a client 
Serialization for the 
clients. The clients can 
deserialize only object 
encoded by the sever due 
the request - response 
nature of the RPC 
Web Socket 
broadcast
That’s all, happy hacking and thanks for the attention. 
Full working example available on: ! 
https://github.com/michelefi/gwtcon-websocket 
Questions?

Más contenido relacionado

Ähnlich wie GWT Web Socket and data serialization

Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)slire
 
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java DevelopersWebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java DevelopersViktor Gamov
 
Getting started with Websocket and Server-sent Events using Java - Arun Gupta
Getting started with Websocket and Server-sent Events using Java - Arun Gupta Getting started with Websocket and Server-sent Events using Java - Arun Gupta
Getting started with Websocket and Server-sent Events using Java - Arun Gupta jaxconf
 
Getting Started with WebSocket and Server-Sent Events using Java by Arun Gupta
Getting Started with WebSocket and Server-Sent Events using Java by Arun GuptaGetting Started with WebSocket and Server-Sent Events using Java by Arun Gupta
Getting Started with WebSocket and Server-Sent Events using Java by Arun GuptaCodemotion
 
Nodejs and WebSockets
Nodejs and WebSocketsNodejs and WebSockets
Nodejs and WebSocketsGonzalo Ayuso
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineRicardo Silva
 
Groovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentationGroovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentationStuart (Pid) Williams
 
Hazelcast and MongoDB at Cloud CMS
Hazelcast and MongoDB at Cloud CMSHazelcast and MongoDB at Cloud CMS
Hazelcast and MongoDB at Cloud CMSuzquiano
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applicationsTom Croucher
 
Bt0083 server side programing
Bt0083 server side programing Bt0083 server side programing
Bt0083 server side programing Techglyphs
 
SCWCD : Thread safe servlets : CHAP : 8
SCWCD : Thread safe servlets : CHAP : 8SCWCD : Thread safe servlets : CHAP : 8
SCWCD : Thread safe servlets : CHAP : 8Ben Abdallah Helmi
 
Play Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaPlay Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaYevgeniy Brikman
 
Java web application development
Java web application developmentJava web application development
Java web application developmentRitikRathaur
 
Developing RESTful WebServices using Jersey
Developing RESTful WebServices using JerseyDeveloping RESTful WebServices using Jersey
Developing RESTful WebServices using Jerseyb_kathir
 

Ähnlich wie GWT Web Socket and data serialization (20)

AJppt.pptx
AJppt.pptxAJppt.pptx
AJppt.pptx
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
 
Servlets
ServletsServlets
Servlets
 
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java DevelopersWebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
WebSockets: The Current State of the Most Valuable HTML5 API for Java Developers
 
Web-Socket
Web-SocketWeb-Socket
Web-Socket
 
Getting started with Websocket and Server-sent Events using Java - Arun Gupta
Getting started with Websocket and Server-sent Events using Java - Arun Gupta Getting started with Websocket and Server-sent Events using Java - Arun Gupta
Getting started with Websocket and Server-sent Events using Java - Arun Gupta
 
Getting Started with WebSocket and Server-Sent Events using Java by Arun Gupta
Getting Started with WebSocket and Server-Sent Events using Java by Arun GuptaGetting Started with WebSocket and Server-Sent Events using Java by Arun Gupta
Getting Started with WebSocket and Server-Sent Events using Java by Arun Gupta
 
The HTML5 WebSocket API
The HTML5 WebSocket APIThe HTML5 WebSocket API
The HTML5 WebSocket API
 
Nodejs and WebSockets
Nodejs and WebSocketsNodejs and WebSockets
Nodejs and WebSockets
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 Engine
 
Servlet
ServletServlet
Servlet
 
Groovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentationGroovy & Grails eXchange 2012 vert.x presentation
Groovy & Grails eXchange 2012 vert.x presentation
 
Hazelcast and MongoDB at Cloud CMS
Hazelcast and MongoDB at Cloud CMSHazelcast and MongoDB at Cloud CMS
Hazelcast and MongoDB at Cloud CMS
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applications
 
Servlet
Servlet Servlet
Servlet
 
Bt0083 server side programing
Bt0083 server side programing Bt0083 server side programing
Bt0083 server side programing
 
SCWCD : Thread safe servlets : CHAP : 8
SCWCD : Thread safe servlets : CHAP : 8SCWCD : Thread safe servlets : CHAP : 8
SCWCD : Thread safe servlets : CHAP : 8
 
Play Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaPlay Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and Scala
 
Java web application development
Java web application developmentJava web application development
Java web application development
 
Developing RESTful WebServices using Jersey
Developing RESTful WebServices using JerseyDeveloping RESTful WebServices using Jersey
Developing RESTful WebServices using Jersey
 

Mehr von GWTcon

"Jclays, A global solution for application design and automatic GWT code gene...
"Jclays, A global solution for application design and automatic GWT code gene..."Jclays, A global solution for application design and automatic GWT code gene...
"Jclays, A global solution for application design and automatic GWT code gene...GWTcon
 
"Xapi-lang For declarative code generation" By James Nelson
"Xapi-lang For declarative code generation" By James Nelson"Xapi-lang For declarative code generation" By James Nelson
"Xapi-lang For declarative code generation" By James NelsonGWTcon
 
In defense of GWT-RPC By Colin Alworth
In defense of GWT-RPC By Colin AlworthIn defense of GWT-RPC By Colin Alworth
In defense of GWT-RPC By Colin AlworthGWTcon
 
DIY: Split GWT Applications using TURDUCKEN approach By Alberto Mancini
DIY: Split GWT Applications using TURDUCKEN approach By Alberto ManciniDIY: Split GWT Applications using TURDUCKEN approach By Alberto Mancini
DIY: Split GWT Applications using TURDUCKEN approach By Alberto ManciniGWTcon
 
Unirex Lean tools By Dario Carotenuto
Unirex Lean tools By Dario CarotenutoUnirex Lean tools By Dario Carotenuto
Unirex Lean tools By Dario CarotenutoGWTcon
 
The future of GWT 2.x - By Colin Alworth
The future of GWT 2.x - By Colin AlworthThe future of GWT 2.x - By Colin Alworth
The future of GWT 2.x - By Colin AlworthGWTcon
 
Web components with java by Haijian Wang
Web components with java by Haijian WangWeb components with java by Haijian Wang
Web components with java by Haijian WangGWTcon
 
UI Framework Development using GWT and HTML Canvas - By Iarosla Kobyliukh
UI Framework Development using GWT and HTML Canvas - By Iarosla KobyliukhUI Framework Development using GWT and HTML Canvas - By Iarosla Kobyliukh
UI Framework Development using GWT and HTML Canvas - By Iarosla KobyliukhGWTcon
 
Best Practices - By Lofi Dewanto
Best Practices - By Lofi DewantoBest Practices - By Lofi Dewanto
Best Practices - By Lofi DewantoGWTcon
 
"Migrate large gwt applications - Lessons Learned" By Harald Pehl
"Migrate large gwt applications - Lessons Learned" By Harald Pehl"Migrate large gwt applications - Lessons Learned" By Harald Pehl
"Migrate large gwt applications - Lessons Learned" By Harald PehlGWTcon
 
GWT Development for Handheld Devices
GWT Development for Handheld DevicesGWT Development for Handheld Devices
GWT Development for Handheld DevicesGWTcon
 
GWT vs CSS3
GWT vs CSS3GWT vs CSS3
GWT vs CSS3GWTcon
 
WebTram: una WebApp GWT per l'editing di dati cartografici e topologici di un...
WebTram: una WebApp GWT per l'editing di dati cartografici e topologici di un...WebTram: una WebApp GWT per l'editing di dati cartografici e topologici di un...
WebTram: una WebApp GWT per l'editing di dati cartografici e topologici di un...GWTcon
 
GWTcon 2014 - Apertura
GWTcon 2014 - AperturaGWTcon 2014 - Apertura
GWTcon 2014 - AperturaGWTcon
 
GWT videocall: power-up your mobile & web app with WebRTC
GWT videocall:  power-up your mobile & web app with WebRTCGWT videocall:  power-up your mobile & web app with WebRTC
GWT videocall: power-up your mobile & web app with WebRTCGWTcon
 

Mehr von GWTcon (15)

"Jclays, A global solution for application design and automatic GWT code gene...
"Jclays, A global solution for application design and automatic GWT code gene..."Jclays, A global solution for application design and automatic GWT code gene...
"Jclays, A global solution for application design and automatic GWT code gene...
 
"Xapi-lang For declarative code generation" By James Nelson
"Xapi-lang For declarative code generation" By James Nelson"Xapi-lang For declarative code generation" By James Nelson
"Xapi-lang For declarative code generation" By James Nelson
 
In defense of GWT-RPC By Colin Alworth
In defense of GWT-RPC By Colin AlworthIn defense of GWT-RPC By Colin Alworth
In defense of GWT-RPC By Colin Alworth
 
DIY: Split GWT Applications using TURDUCKEN approach By Alberto Mancini
DIY: Split GWT Applications using TURDUCKEN approach By Alberto ManciniDIY: Split GWT Applications using TURDUCKEN approach By Alberto Mancini
DIY: Split GWT Applications using TURDUCKEN approach By Alberto Mancini
 
Unirex Lean tools By Dario Carotenuto
Unirex Lean tools By Dario CarotenutoUnirex Lean tools By Dario Carotenuto
Unirex Lean tools By Dario Carotenuto
 
The future of GWT 2.x - By Colin Alworth
The future of GWT 2.x - By Colin AlworthThe future of GWT 2.x - By Colin Alworth
The future of GWT 2.x - By Colin Alworth
 
Web components with java by Haijian Wang
Web components with java by Haijian WangWeb components with java by Haijian Wang
Web components with java by Haijian Wang
 
UI Framework Development using GWT and HTML Canvas - By Iarosla Kobyliukh
UI Framework Development using GWT and HTML Canvas - By Iarosla KobyliukhUI Framework Development using GWT and HTML Canvas - By Iarosla Kobyliukh
UI Framework Development using GWT and HTML Canvas - By Iarosla Kobyliukh
 
Best Practices - By Lofi Dewanto
Best Practices - By Lofi DewantoBest Practices - By Lofi Dewanto
Best Practices - By Lofi Dewanto
 
"Migrate large gwt applications - Lessons Learned" By Harald Pehl
"Migrate large gwt applications - Lessons Learned" By Harald Pehl"Migrate large gwt applications - Lessons Learned" By Harald Pehl
"Migrate large gwt applications - Lessons Learned" By Harald Pehl
 
GWT Development for Handheld Devices
GWT Development for Handheld DevicesGWT Development for Handheld Devices
GWT Development for Handheld Devices
 
GWT vs CSS3
GWT vs CSS3GWT vs CSS3
GWT vs CSS3
 
WebTram: una WebApp GWT per l'editing di dati cartografici e topologici di un...
WebTram: una WebApp GWT per l'editing di dati cartografici e topologici di un...WebTram: una WebApp GWT per l'editing di dati cartografici e topologici di un...
WebTram: una WebApp GWT per l'editing di dati cartografici e topologici di un...
 
GWTcon 2014 - Apertura
GWTcon 2014 - AperturaGWTcon 2014 - Apertura
GWTcon 2014 - Apertura
 
GWT videocall: power-up your mobile & web app with WebRTC
GWT videocall:  power-up your mobile & web app with WebRTCGWT videocall:  power-up your mobile & web app with WebRTC
GWT videocall: power-up your mobile & web app with WebRTC
 

Último

The New Cloud World Order Is FinOps (Slideshow)
The New Cloud World Order Is FinOps (Slideshow)The New Cloud World Order Is FinOps (Slideshow)
The New Cloud World Order Is FinOps (Slideshow)codyslingerland1
 
Extra-120324-Visite-Entreprise-icare.pdf
Extra-120324-Visite-Entreprise-icare.pdfExtra-120324-Visite-Entreprise-icare.pdf
Extra-120324-Visite-Entreprise-icare.pdfInfopole1
 
Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)Muhammad Tiham Siddiqui
 
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024Alkin Tezuysal
 
.NET 8 ChatBot with Azure OpenAI Services.pptx
.NET 8 ChatBot with Azure OpenAI Services.pptx.NET 8 ChatBot with Azure OpenAI Services.pptx
.NET 8 ChatBot with Azure OpenAI Services.pptxHansamali Gamage
 
Graphene Quantum Dots-Based Composites for Biomedical Applications
Graphene Quantum Dots-Based Composites for  Biomedical ApplicationsGraphene Quantum Dots-Based Composites for  Biomedical Applications
Graphene Quantum Dots-Based Composites for Biomedical Applicationsnooralam814309
 
Where developers are challenged, what developers want and where DevEx is going
Where developers are challenged, what developers want and where DevEx is goingWhere developers are challenged, what developers want and where DevEx is going
Where developers are challenged, what developers want and where DevEx is goingFrancesco Corti
 
2024.03.12 Cost drivers of cultivated meat production.pdf
2024.03.12 Cost drivers of cultivated meat production.pdf2024.03.12 Cost drivers of cultivated meat production.pdf
2024.03.12 Cost drivers of cultivated meat production.pdfThe Good Food Institute
 
Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.IPLOOK Networks
 
Planetek Italia Srl - Corporate Profile Brochure
Planetek Italia Srl - Corporate Profile BrochurePlanetek Italia Srl - Corporate Profile Brochure
Planetek Italia Srl - Corporate Profile BrochurePlanetek Italia Srl
 
Automation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projectsAutomation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projectsDianaGray10
 
Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...DianaGray10
 
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENTSIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENTxtailishbaloch
 
How to release an Open Source Dataweave Library
How to release an Open Source Dataweave LibraryHow to release an Open Source Dataweave Library
How to release an Open Source Dataweave Libraryshyamraj55
 
Novo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4jNovo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4jNeo4j
 
TrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie WorldTrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie WorldTrustArc
 
EMEA What is ThousandEyes? Webinar
EMEA What is ThousandEyes? WebinarEMEA What is ThousandEyes? Webinar
EMEA What is ThousandEyes? WebinarThousandEyes
 
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInOutage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInThousandEyes
 
Stobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Stobox 4: Revolutionizing Investment in Real-World Assets Through TokenizationStobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Stobox 4: Revolutionizing Investment in Real-World Assets Through TokenizationStobox
 

Último (20)

The New Cloud World Order Is FinOps (Slideshow)
The New Cloud World Order Is FinOps (Slideshow)The New Cloud World Order Is FinOps (Slideshow)
The New Cloud World Order Is FinOps (Slideshow)
 
Extra-120324-Visite-Entreprise-icare.pdf
Extra-120324-Visite-Entreprise-icare.pdfExtra-120324-Visite-Entreprise-icare.pdf
Extra-120324-Visite-Entreprise-icare.pdf
 
Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)Trailblazer Community - Flows Workshop (Session 2)
Trailblazer Community - Flows Workshop (Session 2)
 
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
Design and Modeling for MySQL SCALE 21X Pasadena, CA Mar 2024
 
.NET 8 ChatBot with Azure OpenAI Services.pptx
.NET 8 ChatBot with Azure OpenAI Services.pptx.NET 8 ChatBot with Azure OpenAI Services.pptx
.NET 8 ChatBot with Azure OpenAI Services.pptx
 
Graphene Quantum Dots-Based Composites for Biomedical Applications
Graphene Quantum Dots-Based Composites for  Biomedical ApplicationsGraphene Quantum Dots-Based Composites for  Biomedical Applications
Graphene Quantum Dots-Based Composites for Biomedical Applications
 
Where developers are challenged, what developers want and where DevEx is going
Where developers are challenged, what developers want and where DevEx is goingWhere developers are challenged, what developers want and where DevEx is going
Where developers are challenged, what developers want and where DevEx is going
 
2024.03.12 Cost drivers of cultivated meat production.pdf
2024.03.12 Cost drivers of cultivated meat production.pdf2024.03.12 Cost drivers of cultivated meat production.pdf
2024.03.12 Cost drivers of cultivated meat production.pdf
 
Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.Introduction - IPLOOK NETWORKS CO., LTD.
Introduction - IPLOOK NETWORKS CO., LTD.
 
Planetek Italia Srl - Corporate Profile Brochure
Planetek Italia Srl - Corporate Profile BrochurePlanetek Italia Srl - Corporate Profile Brochure
Planetek Italia Srl - Corporate Profile Brochure
 
Automation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projectsAutomation Ops Series: Session 2 - Governance for UiPath projects
Automation Ops Series: Session 2 - Governance for UiPath projects
 
Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...Explore the UiPath Community and ways you can benefit on your journey to auto...
Explore the UiPath Community and ways you can benefit on your journey to auto...
 
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENTSIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
SIM INFORMATION SYSTEM: REVOLUTIONIZING DATA MANAGEMENT
 
How to release an Open Source Dataweave Library
How to release an Open Source Dataweave LibraryHow to release an Open Source Dataweave Library
How to release an Open Source Dataweave Library
 
Novo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4jNovo Nordisk's journey in developing an open-source application on Neo4j
Novo Nordisk's journey in developing an open-source application on Neo4j
 
TrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie WorldTrustArc Webinar - How to Live in a Post Third-Party Cookie World
TrustArc Webinar - How to Live in a Post Third-Party Cookie World
 
EMEA What is ThousandEyes? Webinar
EMEA What is ThousandEyes? WebinarEMEA What is ThousandEyes? Webinar
EMEA What is ThousandEyes? Webinar
 
SheDev 2024
SheDev 2024SheDev 2024
SheDev 2024
 
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedInOutage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
Outage Analysis: March 5th/6th 2024 Meta, Comcast, and LinkedIn
 
Stobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Stobox 4: Revolutionizing Investment in Real-World Assets Through TokenizationStobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
Stobox 4: Revolutionizing Investment in Real-World Assets Through Tokenization
 

GWT Web Socket and data serialization

  • 1. GWT Web Socket and data serialization Michele Ficarra Software Engineer at Thales Italy S.p.A
  • 2. Case study With GWT this! is easy Let's focus on the Browser <-> Server communication
  • 3. “GWT provides an RPC mechanism based on Java Servlets to provide access to server-side resources. This mechanism includes generation of efficient client-side and server-side code to serialize objects across the network using deferred binding.” From gwtproject.org
  • 5. GWT RPC • Easy to write, hide all AJAX and serialization complexity. ! • You can use the same POJO in client and server, you need only to implement the serializable interface.! • But …
  • 6. Push data from the server to browser! isn’t so easy…
  • 7. Possible solutions • Polling! • HTTP Long Polling (COMET)! • Web Socket Two RFC can help us to choose
  • 8. “On today's Internet, the Hypertext Transfer Protocol (HTTP) is often used (some would say abused) to enable asynchronous, "server-initiated" communication from a server to a client as well as communication from a client to a server.” From RFC 6202 - Bidirectional HTTP - April 2011
  • 9. “The WebSocket protocol consists of an opening handshake followed by basic message framing, layered over TCP. The goal of this technology is to provide a mechanism for browser-based applications that need two-way communication with servers” From RFC 6455 - The WebSocket Protocol - December 2011
  • 10. • WebSocket seems to be the right choice, but GWT doesn’t provide natively a way to use it! • On Internet we can find a lots of library that can help:! • Errai - http://erraiframework.org/! • Gwt-ws - https://code.google.com/p/gwt-ws/! • …! • But this time we want to make our hands “dirty” and try to do it by ourself!
  • 11. Web Socket - Client Side var websocket = new WebSocket("ws://locahost:8025/echo"); ! websocket.onopen = function(evt) { console.log("WebSocket open"); }; websocket.onclose = function(evt) { console.log("WebSocket close"); }; websocket.onmessage = function(evt) { alart(evt.data); }; websocket.onerror = function(evt) { console.log("WebSocket error"); }; ! websocket.send("Hello World!"); JAVASCRIPT
  • 12. Web Socket - Client Side For use the previous code with GWT we need to write a JSNI wrapper package ws; import com.google.gwt.core.client.JavaScriptObject; ! public abstract class WebSocket { ! private JavaScriptObject ws; ! public WebSocket(String url) { ws = init(url); } ! abstract void onClose(); abstract void onError(); abstract void onMessage(String msg); abstract void onOpen(); ! private native JavaScriptObject init(String url) /*-{ . . . }-*/; ! native void send(String message) /*-{ this.@ws.WebSocket::ws.send(message); }-*/; } JAVA
  • 13. Web Socket - Client Side private native JavaScriptObject init(String url) /*-{ var websocket = new WebSocket(url); var wrapper = this; websocket.onopen = function(evt) { wrapper.@ws.WebSocket::onOpen()(); }; websocket.onclose = function(evt) { wrapper.@ws.WebSocket::onClose()(); }; websocket.onmessage = function(evt) { wrapper.@ws.WebSocket::onMessage(Ljava/lang/String;)(evt.data); }; websocket.onerror = function(evt) { wrapper.@ws.WebSocket::onError()(); }; return websocket; }-*/; JAVA
  • 14. Web Socket - Server Side • For develop the server side we can use the Java API for WebSocket (JSR 356), that is part of the Java EE 7 standard! • If we want use the JSR 356 without a full JEE 7 container it’s possibile to embed a WS server like Tyrus - https://tyrus.java.net/ <dependency> <groupId>org.glassfish.tyrus</groupId> <artifactId>tyrus-server</artifactId> <version>1.8.3</version> </dependency> <dependency> <groupId>org.glassfish.tyrus</groupId> <artifactId>tyrus-container-grizzly-server</artifactId> <version>1.8.3</version> </dependency> MAVEN
  • 15. Web Socket - Server Side Example of an Echo Web Socket service with JSR 356 package ws; ! import javax.websocket.OnMessage; import javax.websocket.server.ServerEndpoint; ! @ServerEndpoint("/echo") public class EchoEndpoint { ! @OnMessage public String echo(String message) { return message + " (from your server)"; } ! } JAVA
  • 16. Web Socket - Server Side Start Tyrus Server in Jetty 6 (DevMode) package ws; ! import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.websocket.DeploymentException; ! import org.glassfish.tyrus.server.Server; ! public class InitListener implements ServletContextListener { ! private Server server = null; ! @Override public void contextDestroyed(ServletContextEvent servletContextEvent) { server.stop(); } ! @Override public void contextInitialized(ServletContextEvent servletContextEvent) { try { server = new Server("localhost", 8025, "/", null, EchoEndpoint.class); server.start(); } catch (final DeploymentException e1) { e1.printStackTrace(); } } } JAVA WEB.XML <listener> <listener-class>ws.InitListener</listener-class> </listener>
  • 17. Data Serialization • Now we are capable of send string back and forward from the browser to the server and viceversa! • But if we want use this technology in a complex environment this result is a little poor. We want send Object not String!! • The first idea to fix the problem can be to use JSON serialization…
  • 18. • … and after a little search we found a lots of way to do that:! • http://www.gwtproject.org/doc/latest/tutorial/JSON.html! • https://github.com/nmorel/gwt-jackson! • …! • But every solution require a lot of boilerplate code or adding annotation to the data model! • We would like something more similar to the GWT RPC, that require only the Serializable interface. May be it can be reused?
  • 19. • After searching in the web and in the GWT source code the solution come out! • We need to define the interface of an RPC and reuse the serialization engine already present in GWT
  • 20. Data Serialization public class Message implements Serializable { ! private String data; private String username; private Date time; ! /* ... */ } JAVA JAVA @RemoteServiceRelativePath("MessageService") public interface MessageService extends RemoteService { Message getMessage(Message message); ! } The RPC Serialization is designed for function call. The client serialize the function argument and the server the return value. So, if we want exchange the same object, it’s important that in the service definition the args and the return value are the same class
  • 21. public String serializeMessae(Message message) { try { SerializationStreamFactory factory = (SerializationStreamFactory) GWT.create(MessageService.class); SerializationStreamWriter writer = factory.createStreamWriter(); writer.writeObject(message); final String data = writer.toString(); return data; } catch (final SerializationException e) { e.printStackTrace(); } return null; } JAVA Data Serialization - Client Side Here there is the trick, the Async Service that is usual return by the deferred binding is also an instance of a SerializationStreamFactory. That can be used for serialize and deserialize objects
  • 22. public Message deserializeMessae(String data) { try { SerializationStreamFactory factory = (SerializationStreamFactory) GWT.create(MessageService.class); final SerializationStreamReader streamReader = factory .createStreamReader(data); final Message message = (Message) streamReader.readObject(); return message; } catch (final SerializationException e) { e.printStackTrace(); } return null; } JAVA Data Serialization - Client Side
  • 23. Data Serialization - Server Side private Message deserializeMessage(String data) throws SerializationException { ServerSerializationStreamReader streamReader = new ServerSerializationStreamReader( Thread.currentThread().getContextClassLoader(), new CustomSerializationPolicyProvider()); // Filling stream reader with data streamReader.prepareToRead(data); // Reading deserialized object from the stream final Message message = (Message) streamReader.readObject(); return message; } JAVA On server side is more or less the same of the client. We need an instance of a ServerSerializationStreamReader for read the object. The only hack is how create a CustomSerializationPolicyProvider
  • 24. Data Serialization - Serialization Policy public class CustomSerializationPolicyProvider implements SerializationPolicyProvider { ! @Override public SerializationPolicy getSerializationPolicy(String moduleBaseURL, String serializationPolicyStrongName) { return new SimpleSerializationPolicy(); } ! } JAVA public class SimpleSerializationPolicy extends SerializationPolicy { ! @Override public boolean shouldDeserializeFields(Class<?> clazz) { return isSerializable(clazz); } ! @Override public boolean shouldSerializeFields(Class<?> clazz) { return isSerializable(clazz); } ! /* ... */ ! private boolean isSerializable(Class<?> clazz) { if (clazz != null) { if (clazz.isPrimitive() || Serializable.class.isAssignableFrom(clazz) || IsSerializable.class.isAssignableFrom(clazz)) { return true; } } return false; } } JAVA RPC generates a serialization policy file during GWT compilation. The serialization policy file contains a whitelist of allowed types which may be serialized.! In this simple implementation there is only the check of the Serializable interface.! Watch out of what are you serializing or you can perform problem on client side.
  • 25. Data Serialization - Server Side private String serializeMessage(final Message messageDto) throws SerializationException { ! ServerSerializationStreamWriter serverSerializationStreamWriter = new ServerSerializationStreamWriter(new SimpleSerializationPolicy()); ! serverSerializationStreamWriter.writeObject(messageDto); String result = serverSerializationStreamWriter.toString(); return result; } JAVA
  • 26. Data Serialization - OnMessage Now we have all pieces for finish the OnMessage method @OnMessage public void onMessage(String message, Session session) { try { ! Message messageDto = deserializeMessage(message); ! String result = serializeMessage(messageDto); ! for (Session s : session.getOpenSessions()) { if (s.isOpen()) { s.getBasicRemote().sendText(result); } } } catch (final SerializationException | IOException e) { logger.log(Level.WARNING, "Error", e); } } JAVA Deserialization of an incoming message from a client Serialization for the clients. The clients can deserialize only object encoded by the sever due the request - response nature of the RPC Web Socket broadcast
  • 27. That’s all, happy hacking and thanks for the attention. Full working example available on: ! https://github.com/michelefi/gwtcon-websocket Questions?