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 IETFarrow-up-right standard recognized by RFC 6455arrow-up-right. 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 standardarrow-up-right). A WebSocket connection piggybacks on top of HTTP for the initial handshake (HTTP Upgrade mechanismarrow-up-right).

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 356arrow-up-right - 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 specificationarrow-up-right which makes it possible to have multiple implementations of the same. Some of these are

Java EE Platform

JSR 356 is also included as a part of the Java Enterprise Edition 7arrow-up-right (Java EE 7) Platform. Any Java EE 7 compliantarrow-up-right 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 Springarrow-up-right, Jettyarrow-up-right etc.

Time to dive in

That's it for the warm up! Let's forge ahead..

Last updated