Warm up
This chapter provides a gentle introduction to WebSocket in order to get you warmed up for things to come
WebSocket primer
What
Simply put, WebSocket is an IETF standard recognized by RFC 6455. To be specific, it's a protocol (just like HTTP) which works on top of TCP. You can think of it as a mid-way between long-polling and Server Sent Events (SSE - a W3C standard). A WebSocket connection piggybacks on top of HTTP for the initial handshake (HTTP Upgrade mechanism).
Important: Once established, the underlying TCP connection remains open
Why
It's key characteristics are as follows
Bi-directional: both server and client can initiate a communication
Full duplex: once the WebSocket session is established, both server and client can communicate independent of each other
The aforementioned characteristics make WebSocket a great fit for applications which require have low latency and high frequency messaging requirements e.g. chat, monitoring, multiplayer online games, broadcasting real time finance data etc. Some of it's benefits (as compared to other solutions) include
Less verbosity (as compared to HTTP)
More efficient (as compared to long-polling)
Richer semantics (as compared to Server Sent Events)
WebSocket as a Java standard
The API (specification) and its implementations
The Java equivalent for this technology is defined by JSR 356 - a standard API which was first released in May 2013 along with a 1.1 release (minor additions) in August 2014. Just like any other JSR (Java Specification Request) defined API, the Java API for WebSocket is backed by a specification which makes it possible to have multiple implementations of the same. Some of these are
Tyrus, which is an open source project and also happens to be the Reference Implementation included in Weblogic and GlassFish
Undertow (included in JBoss EAP and Wildfly)
Tomcat 7 and above (it provides an internal implementation)
Java EE Platform
JSR 356 is also included as a part of the Java Enterprise Edition 7 (Java EE 7) Platform. Any Java EE 7 compliant application server would include a pre-packaged (default) implementation of this API as well as integration with other Java EE technologies like EJB, CDI, Security etc.
JSR 356 support is also provided by other containers/frameworks such as Spring, Jetty etc.
Time to dive in
That's it for the warm up! Let's forge ahead..
Last updated