Java User Group Louvain-La-Neuve 
9/10/2014 – EPHEC LLN 
Rudy Vissers 
Coding Java WebSocket Applications 
Using the JSR 356 API
Introduction 
● HTTP(Hypertext Transfer Protocol) is a 
stateless request-response protocol. 
● This simple design of the HTTP protocol 
makes it very scalable but inefficient and 
unsuitable for highly interactive real-time web 
applications : Chat applications, Multiplayer 
games, etc. 
● HTTP was designed for document sharing 
and not for building today’s highly 
interactive web applications. 
● HTTP is bit chatty in nature,
Introduction cont. 
● Before the HTTP 1.1 version, every request 
made to the server resulted in a new 
connection. 
● This was improved in HTTP 1.1 with the 
introduction of HTTP persistence connections. 
● Persistent connections allowed web browsers 
to reuse the same connection for fetching 
images, scripts, etc.
Introduction cont. 
● HTTP was designed to be half-duplex which 
means it allows transmission of data in just one 
direction at a time. 
● A Walkie-talkie is an example of a half 
duplex device because only one person can 
speak at a time. 
● Developers have created some workarounds or 
hacks to overcome this HTTP shortcoming : 
polling, long polling, and streaming.
Introduction cont. 
● With polling, the client makes synchronous 
calls to get information from the server. Do you 
have something for me ? 
● If the server has new information available it will 
send back data in the response. 
● Otherwise, no information will be sent to the 
client and the client will again make a new 
connection after sometime. 
● This is very inefficient but a very simple way 
to achieve real-time behavior.
What is a WebSocket? 
● A WebSocket is asynchronous, bidirectional, 
full-duplex messaging implementation over a 
single TCP connection. 
● WebSockets are not a HTTP connection , but 
use HTTP to bootstrap a WebSocket 
connection. 
● A full-duplex system allows communication in 
both directions simultaneously. 
● Telephones lines are an example of a full-duplex 
device, since they allow both callers to 
speak and be heard at the same time.
What is a WebSocket? cont. 
● RFC 6455 : 
The WebSocket Protocol : 
http://tools.ietf.org/html/rfc6455 
● JSR 356 : 
Java API for WebSocket : 
https://jcp.org/en/jsr/detail?id=356 
● WebSocket JavaScript API : 
http://www.w3.org/TR/2011/WD-websockets- 
20110419
Browsers : can I use ? 
● WebSockets are supported by all the latest 
browsers.
How Does a WebSocket Work? 
● Every WebSocket connection begins its life as 
an HTTP request. 
● The HTTP request is much like another 
request, except that it has an Upgrade header. 
● The Upgrade header indicates that a client 
would like to upgrade the connection to 
different protocol(s). 
● For WebSockets it will upgrade to the 
WebSocket protocol. 
● The WebSocket connection is established by 
upgrading from HTTP protocol to the
WebSocket is more complex than 
HTTP 
● You can establish an HTTP connection with a 
telnet client, telnet localhost 80 :-) 
but you probably cannot do the same with WS. 
● Even if you ignored the handshake 
requirements (which include the use of the 
SHA1 hash function), you would then be unable 
to properly mask and frame the data to be sent 
and the server would close the connection.
WebSocket is more complex than 
HTTP cont. 
● WebSocket connections are intended to be 
more persistent than HTTP connections. 
● If you only want to receive an update every 30 
minutes, you will want to go with HTTP. 
● If you want to receive updates every second, a 
WebSocket might be a better option, because 
establishing an HTTP connection takes a lot 
of time.
WebSockets simplify real-time 
application architectures 
● For these kind of applications WebSockets are 
more efficient and performant than other 
workarounds like polling. 
● They require less bandwidth and reduce 
latency. 
● WebSockets do not require headers to send 
messages between peers. 
● This considerably lowers the required 
bandwidth.
WebSocket Use Cases 
● Chat applications 
● Multiplayer games 
● Stock trading or financial applications 
● Collaborative document editing 
● Social networking applications
Java APIs 
● JSR 356, Java API for WebSocket, specifies 
Java API that developers can use to integrate 
WebSockets into their applications both on the 
server side as well as on the Java client side. 
● JSR 356 is part of the upcoming Java EE 7 
standard. 
● This means all Java EE 7 compliant application 
servers will have an implementation of the 
WebSocket protocol that adheres to the JSR 
356 standard. 
● Developers can also use JSR 356 outside Java
Java APIs cont. 
● A Java client can use JSR 356 compliant client 
implementation to connect to a WebSocket 
server (Tyrus). 
● For web clients, developers can use 
WebSocket JavaScript API to communicate 
with WebSocket server. 
● The difference between a WebSocket client 
and a WebSocket server lies only in the means 
by which the two are connected. 
● A WebSocket client is a WebSocket endpoint 
that initiates a connection to a peer.
● Time to code !

JUG louvain websockets

  • 1.
    Java User GroupLouvain-La-Neuve 9/10/2014 – EPHEC LLN Rudy Vissers Coding Java WebSocket Applications Using the JSR 356 API
  • 2.
    Introduction ● HTTP(HypertextTransfer Protocol) is a stateless request-response protocol. ● This simple design of the HTTP protocol makes it very scalable but inefficient and unsuitable for highly interactive real-time web applications : Chat applications, Multiplayer games, etc. ● HTTP was designed for document sharing and not for building today’s highly interactive web applications. ● HTTP is bit chatty in nature,
  • 3.
    Introduction cont. ●Before the HTTP 1.1 version, every request made to the server resulted in a new connection. ● This was improved in HTTP 1.1 with the introduction of HTTP persistence connections. ● Persistent connections allowed web browsers to reuse the same connection for fetching images, scripts, etc.
  • 4.
    Introduction cont. ●HTTP was designed to be half-duplex which means it allows transmission of data in just one direction at a time. ● A Walkie-talkie is an example of a half duplex device because only one person can speak at a time. ● Developers have created some workarounds or hacks to overcome this HTTP shortcoming : polling, long polling, and streaming.
  • 5.
    Introduction cont. ●With polling, the client makes synchronous calls to get information from the server. Do you have something for me ? ● If the server has new information available it will send back data in the response. ● Otherwise, no information will be sent to the client and the client will again make a new connection after sometime. ● This is very inefficient but a very simple way to achieve real-time behavior.
  • 6.
    What is aWebSocket? ● A WebSocket is asynchronous, bidirectional, full-duplex messaging implementation over a single TCP connection. ● WebSockets are not a HTTP connection , but use HTTP to bootstrap a WebSocket connection. ● A full-duplex system allows communication in both directions simultaneously. ● Telephones lines are an example of a full-duplex device, since they allow both callers to speak and be heard at the same time.
  • 7.
    What is aWebSocket? cont. ● RFC 6455 : The WebSocket Protocol : http://tools.ietf.org/html/rfc6455 ● JSR 356 : Java API for WebSocket : https://jcp.org/en/jsr/detail?id=356 ● WebSocket JavaScript API : http://www.w3.org/TR/2011/WD-websockets- 20110419
  • 8.
    Browsers : canI use ? ● WebSockets are supported by all the latest browsers.
  • 9.
    How Does aWebSocket Work? ● Every WebSocket connection begins its life as an HTTP request. ● The HTTP request is much like another request, except that it has an Upgrade header. ● The Upgrade header indicates that a client would like to upgrade the connection to different protocol(s). ● For WebSockets it will upgrade to the WebSocket protocol. ● The WebSocket connection is established by upgrading from HTTP protocol to the
  • 10.
    WebSocket is morecomplex than HTTP ● You can establish an HTTP connection with a telnet client, telnet localhost 80 :-) but you probably cannot do the same with WS. ● Even if you ignored the handshake requirements (which include the use of the SHA1 hash function), you would then be unable to properly mask and frame the data to be sent and the server would close the connection.
  • 11.
    WebSocket is morecomplex than HTTP cont. ● WebSocket connections are intended to be more persistent than HTTP connections. ● If you only want to receive an update every 30 minutes, you will want to go with HTTP. ● If you want to receive updates every second, a WebSocket might be a better option, because establishing an HTTP connection takes a lot of time.
  • 12.
    WebSockets simplify real-time application architectures ● For these kind of applications WebSockets are more efficient and performant than other workarounds like polling. ● They require less bandwidth and reduce latency. ● WebSockets do not require headers to send messages between peers. ● This considerably lowers the required bandwidth.
  • 13.
    WebSocket Use Cases ● Chat applications ● Multiplayer games ● Stock trading or financial applications ● Collaborative document editing ● Social networking applications
  • 14.
    Java APIs ●JSR 356, Java API for WebSocket, specifies Java API that developers can use to integrate WebSockets into their applications both on the server side as well as on the Java client side. ● JSR 356 is part of the upcoming Java EE 7 standard. ● This means all Java EE 7 compliant application servers will have an implementation of the WebSocket protocol that adheres to the JSR 356 standard. ● Developers can also use JSR 356 outside Java
  • 15.
    Java APIs cont. ● A Java client can use JSR 356 compliant client implementation to connect to a WebSocket server (Tyrus). ● For web clients, developers can use WebSocket JavaScript API to communicate with WebSocket server. ● The difference between a WebSocket client and a WebSocket server lies only in the means by which the two are connected. ● A WebSocket client is a WebSocket endpoint that initiates a connection to a peer.
  • 16.