This sample demonstrates how to use Websockets on Google App Engine Flexible Environment using Java. The sample uses the native Jetty WebSocket Server API to create a server-side socket and the native Jetty WebSocket Client API.
- The sample application creates a server socket using the endpoint
/echo. - The homepage (
/) provides a form to submit a text message to the server socket. This creates a client-side socket and sends the message to the server. - The server on receiving the message, echoes the message back to the client.
- The message received by the client is stored in an in-memory cache and is viewable on the homepage.
The sample also provides a Javascript
client(/js_client.jsp) that you can use to
test against the Websocket server.
- Install and initialize GCloud SDK. This will
gcloud init-
If this is your first time creating an app engine application
gcloud appengine create
The sample application is packaged as a jar and runs on the Java 25 runtime.
mvn clean package appengine:deployYou can then direct your browser to https://YOUR_PROJECT_ID.appspot.com/
To test the Javascript client, access
https://YOUR_PROJECT_ID.appspot.com/js_client.jsp
Note: This application constructs a Web Socket URL using getWebSocketAddress
in the SendServlet Class. The application assumes the latest version of the service.