include::../shared-doc/attributes.adoc[]

= websocket-client: WebSocket Java Client Example
:author: Paul Cowan
:level: Intermediate
:technologies: Web Socket, CDI Events, JSON, SSL

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

:standalone-server-type: default
:archiveType: war

//*************************************************
// Shared CD and Product Release content
//*************************************************

== 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 Endpoint` -> `Remote 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://echo.websocket.org, 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.

//*************************************************
// Product Release content only
//*************************************************

ifndef::EAPCDRelease[]

// System Requirements
include::../shared-doc/system-requirements.adoc[leveloffset=+1]
// Use of {jbossHomeName}
include::../shared-doc/use-of-jboss-home-name.adoc[leveloffset=+1]
// Start the {productName} Standalone Server
include::../shared-doc/start-the-standalone-server.adoc[leveloffset=+1]
//  Build and Deploy the Quickstart
include::../shared-doc/build-and-deploy-the-quickstart.adoc[leveloffset=+1]

== 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.

[source,subs="+quotes,attributes+",options="nowrap"]
----
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.

[source,subs="+quotes,attributes+",options="nowrap"]
----
SEND: This is a test
RECV: BROADCAST: Connecting to backend wss://echo.websocket.org
RECV: BROADCAST: Opened backend session __BACKEND_SESSION_ID__
RECV: Sending message from frontend session __FRONTEND_SESSION_ID__
RECV: Received message from backend session __BACKEND_SESSION_ID__
RECV: This is a test
----

// Undeploy the Quickstart
include::../shared-doc/undeploy-the-quickstart.adoc[leveloffset=+1]
// Run the Quickstart in Red Hat JBoss Developer Studio or Eclipse
include::../shared-doc/run-the-quickstart-in-jboss-developer-studio.adoc[leveloffset=+1]
// Debug the Application
include::../shared-doc/debug-the-application.adoc[leveloffset=+1]


endif::[]

//*************************************************
// CD Release content only
//*************************************************

ifdef::EAPCDRelease[]

// Getting Started with OpenShift
include::../shared-doc/cd-openshift-getting-started.adoc[leveloffset=+1]
//Prepare OpenShift for Quickstart Deployment
include::../shared-doc/cd-create-project.adoc[leveloffset=+1]
// Import the Latest {xpaasproduct-shortname} Image Streams and Templates
include::../shared-doc/cd-import-imagestreams-templates.adoc[leveloffset=+1]
// Deploy the {ProductShortName} Source-to-Image (S2I) Quickstart to OpenShift
include::../shared-doc/cd-deploy-project.adoc[leveloffset=+1]
// OpenShift Post Deployment Tasks: This is not currently needed.
// include::../shared-doc/cd-post-deployment-tasks.adoc[leveloffset=+1]

endif::[]
