Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
102 lines (73 sloc) 4.44 KB

websocket-client: WebSocket Java Client Example

Demonstrates use of a Javascript WebSocket client, WebSocket configuration, programmatic binding, and secure WebSocket.

What is it?

The websocket-client quickstart demonstrates how to use the Java API for WebSockets to create Java client endpoint connected to a remote WebSocket server.

The example is modeled as a relay between a front-end WebSocket server endpoint and a back-end WebSocket client endpoint.

Message Flow:
Browser Javascript WebSocket Client{productName} WebSocket Server Endpoint`{productName} Websocket Client EndpointRemote WebSocket Echo Server

CDI events are used to pass messages between the front-end and back-end servers. A single back-end WebSocket client endpoint is shared for all front-end clients.

The remote WebSocket server must be an Echo server; a simple WebSocket server that echos back messages the client sends for the purpose of testing. Such a server is publicly available at ws://, but any echo server will work.

Frontend does not use WebSocket annotations because it demonstrates how to use ServerEndpointConfig to modify the default Configurator to use an application scoped Endpoint, and how to deploy the Endpoint programatically.

Backend does not use WebSocket annotations because it demonstrates how to use ClientEndpointConfig to configure the WebSocket client to connect to a secure (wss) WebSocket.

Access the Application

Access the running application in a browser at the following URL: http://localhost:8080/{artifactId}/

You are presented with the WebSocket Echo Replay page confirming the connection to the remote WebSocket Echo server.

Connecting to ws://localhost:8080/{artifactId}/relay
RECV: Opened frontend session FRONTEND_SESSION_ID

Type a message in the text input field at the bottom of the page and click Send. The message is processed and the form displays the relayed results. The message This is a test was used in the following example.

SEND: This is a test
RECV: BROADCAST: Connecting to backend wss://
RECV: Sending message from frontend session FRONTEND_SESSION_ID
RECV: Received message from backend session BACKEND_SESSION_ID
RECV: This is a test
You can’t perform that action at this time.