Introduction to HTML5 WebSockets

WebSocket is a TCP-based protocol like HTTP which is part of HTML 5 specification. Unlike HTTP which is a request response synchronous protocol, WebSocket provides a two way (full-duplex) communications channel over a single TCP connection and messages (textual and binary) can be sent between client and server asynchronously without the overhead of HTTP request/response life cycle. Note that when using WebSockets, both parties can send and receive messages and hence this is closer to a peer-to-peer architecture than a client server architecture like HTTP. An endpoint can be considered as the address and entry point to a websocket. Both parties will have a send/receive endpoint as both parties can send/receive messages asynchronously. HTML5-based web applications can now transport JSON data using WebSockets, in addition to Ajax and other transport technologies. Though WebSocket was designed to be implemented in web browsers and web servers, it can be used by any client or server application.


WebSocket and HTTP

The WebSocket Protocol is an independent TCP-based protocol. Its only relationship to HTTP is that its handshake is interpreted by HTTP servers. HTTP/1.1 introduced support for the upgrade header field that allows clients to request to change protocol to a newer http protocol version or switch to a different protocol. WebSocket uses this mechanism to set up a connection with a HTTP server. The WebSocket Protocol has two parts: a handshake to establish the upgraded connection, then the actual data transfer. First, a client requests a websocket connection by using the "Upgrade: websocket" and "Connection: Upgrade" headers, along with a few protocol-specific headers to establish the version being used and set-up a handshake. The server, if it supports the protocol, replies with the same "Upgrade: websocket" and "Connection: Upgrade" headers and completes the handshake. Once the handshake is completed successfully, data transfer begins.

Example HTTP request and response from the Wikipedia page:

Client request:

GET /chat HTTP/1.1


Upgrade: websocket

Connection: Upgrade

Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==

Sec-WebSocket-Protocol: chat, superchat

Sec-WebSocket-Version: 13



Server response:

HTTP/1.1 101 Switching Protocols

Upgrade: websocket

Connection: Upgrade

Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=

Sec-WebSocket-Protocol: chat


WebSocket and Java EE

The Java EE platform’s Java API for WebSocket enables you to create, configure, and deploy WebSocket endpoints in Java EE web applications. To accept messages from clients, a server-side class must be configured as a WebSocket endpoint. The WebSocket client API enables you to access remote WebSocket endpoints from any Java application. The javax.websocket.server package is specific to server endpoints whereas javax.websocket package is common to client and server endpoints.

With Java API for WebSocket, you can create two kinds of endpoints: programmatic endpoints and annotated endpoints. To create a programmatic endpoint, you extend the Endpoint class and override its lifecycle methods. To create an annotated endpoint, you decorate a Java class and some of its methods with the annotations. Java EE 7 provides @ServerEndpoint annotation for marking a class as endpoint and also provides @OnMessage annotation to annotate a message that will be invoked to process the message that is sent to the WebSocket endpoint.


WebSocket Java EE Example with Eclipse and Tomcat

You can find many examples on WebSocket Java EE Example that work with Glassfish, but since our direction is on eclipse+tomcat, we are working to make those examples work in an eclipse+tomcat environment. By the time you read this we might have already found out a solution. Please go to Java EE notes book here or use a search, and see if any examples are added. If you have got it working yourself, please share it with us and help us as well as our readers.



Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) ArrayLists (1) Arrays (2) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (53) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (4) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (7) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (11) Java Programmer 2 (8) Javascript Frameworks (1) Java SE Professional (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) resources (1) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)