SlideShare a Scribd company logo
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?

More Related Content

What's hot

Support programmation orientée objet c# .net version f8
Support programmation orientée objet c#  .net version f8Support programmation orientée objet c#  .net version f8
Support programmation orientée objet c# .net version f8
ENSET, Université Hassan II Casablanca
 
Relation between classes in arabic
Relation between classes in arabicRelation between classes in arabic
Relation between classes in arabic
Mahmoud Ouf
 
Servlet Filter
Servlet FilterServlet Filter
Servlet Filter
AshishSingh Bhatia
 
spring-api-rest.pdf
spring-api-rest.pdfspring-api-rest.pdf
spring-api-rest.pdf
Jaouad Assabbour
 
Performance Metrics in a Day with Selenium
Performance Metrics in a Day with SeleniumPerformance Metrics in a Day with Selenium
Performance Metrics in a Day with Selenium
Mark Watson
 
Cours java avance avancé thread arraylist
Cours java avance avancé thread arraylistCours java avance avancé thread arraylist
Cours java avance avancé thread arraylist
Houssem Hamrouni
 
Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-framework
Arulalan T
 
Blended learning et révolution des pratiques pédagogiques
Blended learning et révolution des pratiques pédagogiquesBlended learning et révolution des pratiques pédagogiques
Blended learning et révolution des pratiques pédagogiques
NGNAOUSSI ELONGUE Cedric Christian
 
Presentation swagger
Presentation swaggerPresentation swagger
Presentation swagger
François Robert
 
How to build a chat application with react js, nodejs, and socket.io
How to build a chat application with react js, nodejs, and socket.ioHow to build a chat application with react js, nodejs, and socket.io
How to build a chat application with react js, nodejs, and socket.io
Katy Slemon
 
The Death of Final Tagless
The Death of Final TaglessThe Death of Final Tagless
The Death of Final Tagless
John De Goes
 
Vert.x
Vert.xVert.x
Vert.x
Matt Stine
 
Support de cours Spring M.youssfi
Support de cours Spring  M.youssfiSupport de cours Spring  M.youssfi
Support de cours Spring M.youssfi
ENSET, Université Hassan II Casablanca
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
Harry Potter
 
JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event Loop
Derek Willian Stavis
 
Cours SGBD - L3 Bio-informatique - Mohamed Skander DAAS.pdf
Cours SGBD  - L3 Bio-informatique - Mohamed Skander DAAS.pdfCours SGBD  - L3 Bio-informatique - Mohamed Skander DAAS.pdf
Cours SGBD - L3 Bio-informatique - Mohamed Skander DAAS.pdf
JordaniMike
 
AST Rewriting Using recast and esprima
AST Rewriting Using recast and esprimaAST Rewriting Using recast and esprima
AST Rewriting Using recast and esprima
Stephen Vance
 
[OKKY 세미나] 정진욱 - 테스트하기 쉬운 코드로 개발하기
[OKKY 세미나] 정진욱 - 테스트하기 쉬운 코드로 개발하기[OKKY 세미나] 정진욱 - 테스트하기 쉬운 코드로 개발하기
[OKKY 세미나] 정진욱 - 테스트하기 쉬운 코드로 개발하기
OKKY
 
How to add a yellow circle around mouse cursor? - Thiyagu
How to add a yellow circle around mouse cursor? - ThiyaguHow to add a yellow circle around mouse cursor? - Thiyagu
How to add a yellow circle around mouse cursor? - Thiyagu
Thiyagu K
 
Manual de utilização ydm 3212
Manual de utilização ydm 3212Manual de utilização ydm 3212
Manual de utilização ydm 3212
Metalferco Comércio de Ferragens
 

What's hot (20)

Support programmation orientée objet c# .net version f8
Support programmation orientée objet c#  .net version f8Support programmation orientée objet c#  .net version f8
Support programmation orientée objet c# .net version f8
 
Relation between classes in arabic
Relation between classes in arabicRelation between classes in arabic
Relation between classes in arabic
 
Servlet Filter
Servlet FilterServlet Filter
Servlet Filter
 
spring-api-rest.pdf
spring-api-rest.pdfspring-api-rest.pdf
spring-api-rest.pdf
 
Performance Metrics in a Day with Selenium
Performance Metrics in a Day with SeleniumPerformance Metrics in a Day with Selenium
Performance Metrics in a Day with Selenium
 
Cours java avance avancé thread arraylist
Cours java avance avancé thread arraylistCours java avance avancé thread arraylist
Cours java avance avancé thread arraylist
 
Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-framework
 
Blended learning et révolution des pratiques pédagogiques
Blended learning et révolution des pratiques pédagogiquesBlended learning et révolution des pratiques pédagogiques
Blended learning et révolution des pratiques pédagogiques
 
Presentation swagger
Presentation swaggerPresentation swagger
Presentation swagger
 
How to build a chat application with react js, nodejs, and socket.io
How to build a chat application with react js, nodejs, and socket.ioHow to build a chat application with react js, nodejs, and socket.io
How to build a chat application with react js, nodejs, and socket.io
 
The Death of Final Tagless
The Death of Final TaglessThe Death of Final Tagless
The Death of Final Tagless
 
Vert.x
Vert.xVert.x
Vert.x
 
Support de cours Spring M.youssfi
Support de cours Spring  M.youssfiSupport de cours Spring  M.youssfi
Support de cours Spring M.youssfi
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event Loop
 
Cours SGBD - L3 Bio-informatique - Mohamed Skander DAAS.pdf
Cours SGBD  - L3 Bio-informatique - Mohamed Skander DAAS.pdfCours SGBD  - L3 Bio-informatique - Mohamed Skander DAAS.pdf
Cours SGBD - L3 Bio-informatique - Mohamed Skander DAAS.pdf
 
AST Rewriting Using recast and esprima
AST Rewriting Using recast and esprimaAST Rewriting Using recast and esprima
AST Rewriting Using recast and esprima
 
[OKKY 세미나] 정진욱 - 테스트하기 쉬운 코드로 개발하기
[OKKY 세미나] 정진욱 - 테스트하기 쉬운 코드로 개발하기[OKKY 세미나] 정진욱 - 테스트하기 쉬운 코드로 개발하기
[OKKY 세미나] 정진욱 - 테스트하기 쉬운 코드로 개발하기
 
How to add a yellow circle around mouse cursor? - Thiyagu
How to add a yellow circle around mouse cursor? - ThiyaguHow to add a yellow circle around mouse cursor? - Thiyagu
How to add a yellow circle around mouse cursor? - Thiyagu
 
Manual de utilização ydm 3212
Manual de utilização ydm 3212Manual de utilização ydm 3212
Manual de utilização ydm 3212
 

Similar to GWT Web Socket and data serialization

AJppt.pptx
AJppt.pptxAJppt.pptx
AJppt.pptx
SachinSingh217687
 
18CSC311J Web Design and Development UNIT-3
18CSC311J Web Design and Development UNIT-318CSC311J Web Design and Development UNIT-3
18CSC311J Web Design and Development UNIT-3
sivakumarmcs
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
slire
 
Servlets
ServletsServlets
Servlets
ZainabNoorGul
 
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
 
Web-Socket
Web-SocketWeb-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
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 Gupta
Codemotion
 
The HTML5 WebSocket API
The HTML5 WebSocket APIThe HTML5 WebSocket API
The HTML5 WebSocket API
David Lindkvist
 
Nodejs and WebSockets
Nodejs and WebSocketsNodejs and WebSockets
Nodejs and WebSockets
Gonzalo 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 Engine
Ricardo 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 presentation
Stuart (Pid) Williams
 
Hazelcast and MongoDB at Cloud CMS
Hazelcast and MongoDB at Cloud CMSHazelcast and MongoDB at Cloud CMS
Hazelcast and MongoDB at Cloud CMS
uzquiano
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applicationsTom Croucher
 
Servlet
Servlet Servlet
Servlet
Dhara Joshi
 
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 : 8
Ben 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 Scala
Yevgeniy Brikman
 
Java web application development
Java web application developmentJava web application development
Java web application development
RitikRathaur
 

Similar to GWT Web Socket and data serialization (20)

AJppt.pptx
AJppt.pptxAJppt.pptx
AJppt.pptx
 
18CSC311J Web Design and Development UNIT-3
18CSC311J Web Design and Development UNIT-318CSC311J Web Design and Development UNIT-3
18CSC311J Web Design and Development UNIT-3
 
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
 

More from 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 Nelson
GWTcon
 
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
GWTcon
 
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
GWTcon
 
Unirex Lean tools By Dario Carotenuto
Unirex Lean tools By Dario CarotenutoUnirex Lean tools By Dario Carotenuto
Unirex Lean tools By Dario Carotenuto
GWTcon
 
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
GWTcon
 
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
GWTcon
 
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
GWTcon
 
Best Practices - By Lofi Dewanto
Best Practices - By Lofi DewantoBest Practices - By Lofi Dewanto
Best Practices - By Lofi Dewanto
GWTcon
 
"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
GWTcon
 
GWT Development for Handheld Devices
GWT Development for Handheld DevicesGWT Development for Handheld Devices
GWT Development for Handheld Devices
GWTcon
 
GWT vs CSS3
GWT vs CSS3GWT vs CSS3
GWT vs CSS3
GWTcon
 
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 WebRTC
GWTcon
 

More from 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
 

Recently uploaded

Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
Abida Shariff
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 

Recently uploaded (20)

Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 

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?