An example demonstrating how Server Sent Events (SSE) Jersey support can be used to notify clients about changes in server-side managed data (collection of items). The example also outlines how SSE events can be consumed using javascript browser-based clients.
The example consists of a web-based client application and a server-side application deployed on a Jetty servlet container.
The server side part of the application consists of a JAX-RS resource managing collection of string items and a simple HTML page that includes a browser SSE client written in Javascript as well as a basic CSS stylesheet. The SSE Javascript client connects to the JAX-RS resource and transforms the streamed messages into HTML code that is rendered by the browser. The javascript client also demonstrates how named and unnamed SSE events are handled by HTML5 EventSource
component. The mapping of the URI path space of the server-side part of the application is presented in the following table:
URI path | Resource class | HTTP methods |
---|---|---|
/resources/items | ItemStoreResource | GET, POST |
/resources/items/events | ItemStoreResource | GET (SSE) |
Application is configured to run using Jetty maven plugin under base path sse-item-store-webapp
.
mvn clean compile jetty:run
The command above deploys the current example. After successful deployment, you should be able to access the browser SSE client page at http://localhost:8080/sse-item-store-jersey-webapp/index.html. To see the raw SSE event stream, you may also point your browser directly at the ItemStoreResource
.
mvn clean package
The command above creates a Servlet 3.x compliant WAR located in the target directory. The WAR can be then deployed to your Servlet 3.x compliant container.
After deploying the application into a Servlet 3.x compliant container, you can run the attached test by executing:
mvn test -Djersey.config.test.container.factory=org.glassfish.jersey.test.external.ExternalTestContainerFactory -Djersey.config.test.container.port=<port>