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
Run using the Jetty Maven plugin.
mvn jetty:run
You can then direct your browser to http://localhost:8080/
To test the Javascript client, access http://localhost:8080/js_client.jsp
App Engine Flex deployment configuration is provided in app.yaml.
Set the environment variable JETTY_MODULES_ENABLE:websocket to enable the Jetty websocket module on the Jetty server.
Manual scaling is set to a single instance as we are using an in-memory cache of messages for this sample application.
For more details on configuring your app.yaml, please refer to this resource.
The sample application is packaged as a war, and hence will be automatically run using the Java 8/Jetty 9 with Servlet 3.1 Runtime.
mvn appengine:deploy
You 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.