This repository is no longer being maintained and has been archived. It is left for reference and available for use as-is with the advisory that it depends old dependencies which may have security vulnerabilties.
For a newer sample, see the Bidirectional communication between services using Jakarta WebSocket Open Liberty guide.
The WebSockets standard defines a full-duplex communication protocol to simplify and streamline long-running communications between a client and a server. The protocol has a well-defined wire format that allows for text or binary messages to be interleaved at will: either side of the connection can send messages at any time, in any order (which is a significant difference from the requirements of Comet or other long-polling mechanisms which require management of several connections to emulate bidirectional communication). The wire format is compact and efficient, making it ideal for small messages.
A WebSocket connection is established by upgrading an existing HTTP connection via an Upgrade handshake. The connection continues to use the original HTTP connection after upgrade, which allows it to work with firewalls and other infrastructure optimized for HTTP traffic; however, HTTP proxy servers may need to be upgraded to understand the WebSocket protocol, especially those that do SSL termination.
This sample contains a few variations to illustrate how to use WebSockets in Java EE7 applications. It goes hand-in-hand with the Introduction to WebSockets session from IBM InterConnect 2015.
- Simple Annotated WebSocket: SimpleEndpoint provides a simple example of an annotated endpoint
- Simple Programmatic WebSocket: ProgrammaticEndpoint defines an endpoint that is configured via the EndpointApplicationConfig
- Echo sample: EchoEndpoint also uses annotations, but provides an example of simple message broadcasting via WebSockets.
- Echo async sample: EchoAsyncEndpoint adds an asynchronous element to the simple broadcast example, using a CDI-injected executor to provide a delayed rebroadcast.
- Echo sample with Encoder/Decoder: EchoEncoderEndpoint extends the basic broadcast example with a simple Decoder and Encoder.
This sample also includes a WebSocket client, AnnotatedClientEndpoint, which can be brought into the conversation with either the Echo sample or the Echo async sample by providing client
as input.
Browse the code to see what it does, or build and run it yourself:
- Building and running on the command line using Maven or Gradle
- Building and running using Eclipse and WebSphere Development Tools (WDT)
Once the server has been started, go to http://localhost:9082/websocket/ to interact with the sample. Cross-reference the source to understand what the client side (Java or JavaScript) and server side (Java) are doing.
- Wikipedia summary
- WebSockets: Proxy Traversal
- w3 JavaScript WebSockets API
- WebSocket protocol: RFC 6455
- JSR 356: Java API for WebSocket
© Copyright IBM Corporation 2015, 2017.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.