2. Web SOCKETS
CURRENT STATE
FOR JAVA DEVELOPERS
PRESENTED
BY
VIKTOR
GAMOV
3. WEB SOCKETS
AGENDA
BIT OF MEET WEBSOCKETS Q&A
HISTORY THE WEBSOCKETS IN ACTION SESSION
?
4. OPTIONS FOR “REALTIME” WEB
“LEGACY” WEB
POLLING LONG-POLLING STEAMING
Browser sends HTTP requests at Browser sends a request to the
regular intervals and server and the server keeps the Browser sends a complete
immediately receives a request open for a set period request, but the server sends
response. However, real- time of time. If a notification is and maintains an open
data is often not that received within that period, a response that is continuously
predictable, making response containing the updated and kept open
unnecessary requests message is sent to the client. If indefinitely (or for a set period
inevitable and as a result, a notification is not received of time)
many connections are opened within the set time period, the
and closed needlessly in low- server sends a response to
message-rate situations terminate the open request.
5. WHAT IS WEBSOCKET
“Reducing kilobytes of data to 2 bytes... and reducing latency from 150ms to 50
ms is far more than marginal. In fact, these two factors alone are enough to make
WebSocket seriously interesting...”
www.ietf.org/mail-archive/web/hybi/current/msg00784.html
6. WHAT IS WEBSOCKETS?
WEB SOCKET
STANDARD PROTOCOL CLIENT-SIDE API SERVER-SIDE API
Websocket is a HTML5 specification True real-time server
standardized technology introduces WebSocket updates. Expected large
(described in RFC6455) client side object. No penetration in Java
to support low-overhead plugin required. world with upcoming
bidirectional traffic from JavaEE 7 spec and
your Web browser. JSR-356
7. WEBSOCKET HANDSHAKE
To Start full-duplex communication client should send
UPGRADE request
1 2 3 4
SEND RECEIVE CHANGE LISTEN
UPGRADE UPGRADE READYSTATE MESSAGE
REQUEST RESPONSE TO OPEN EVENT
11. CLIENT-SIDE WEBSOCKET API
var
ws;
if
(window.WebSocket)
{
output("WebSocket
supported
in
your
browser");
ws
=
new
WebSocket("ws://www.websockets.org");
//
Set
event
handlers.
ws.onopen
=
function
()
{
output("onopen");
};
ws.onmessage
=
function
(e)
{
//
e.data
contains
received
string.
output("echo
from
server
:
"
+
e.data);
};
ws.onclose
=
function
()
{
output("onclose");
};
ws.onerror
=
function
()
{
output("onerror");
};
}
else
{output("WebSocket
not
supported
in
your
browser");}
12. JAVA EE 7 SERVER-SIDE API
package
org.javaee.glassfishwebsocket;
import
org.glassfish.websocket.api.annotations.WebSocket;
import
org.glassfish.websocket.api.annotations.WebSocketMessage;
@WebSocket(path
=
"/echo")
public
class
EchoBean
{
@WebSocketMessage
public
String
echo(String
message)
{
System.out.println("#####################
Message
received");
return
message
+
"
(from
your
server)";
}
}
15. WEBSOCKET SUPPORT
JAVA-based Web servers with native support.
The WebServer has API for WebSocket
✦Netty 3.3.x
✦Jetty 7.x, 8.x
✦Glassfish 3.1.2
✦Tomcat 7.0.27
19. USE CASE
OUR CLIENTS
WebSockets really shine with following applications:
✦Live trading/sports ticker
✦Controlling medical equipment over the web
✦Chat applications
✦Multiplayer online games
✦Realtime updating social streams