https://github.com/cassiomolin/jersey-websockets-undertow
Example of REST API along with WebSocket endpoint using Undertow, Weld, Jersey and JSR 356.
https://github.com/cassiomolin/jersey-websockets-undertow
cdi java jersey jsr356 undertow websockets weld
Last synced: about 2 months ago
JSON representation
Example of REST API along with WebSocket endpoint using Undertow, Weld, Jersey and JSR 356.
- Host: GitHub
- URL: https://github.com/cassiomolin/jersey-websockets-undertow
- Owner: cassiomolin
- License: mit
- Created: 2017-06-17T12:16:19.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-07-20T08:40:37.000Z (over 8 years ago)
- Last Synced: 2024-12-20T08:49:15.142Z (10 months ago)
- Topics: cdi, java, jersey, jsr356, undertow, websockets, weld
- Language: Java
- Homepage:
- Size: 136 KB
- Stars: 19
- Watchers: 3
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Example with Undertow, Weld, Jersey and WebSockets
[](https://travis-ci.org/cassiomolin/jersey-websockets-undertow)
[](https://raw.githubusercontent.com/cassiomolin/jersey-websockets-undertow/master/LICENSE.txt)Example of application using:
- **Undertow:** Servlet container that also provides WebSockets support (JSR 356 implementation).
- **Weld:** CDI reference implementation.
- **Jersey:** JAX-RS reference implementation for creating RESTful web services in Java.
- **WebSockets:** Using the JSR 356 implementation provided by Undertow.In this example, a message created from the REST API is broadcasted to all WebSocket clients. [CDI events][] are used to send data from the REST API to the WebSocket endpoint.
## Building and running this application
Follow these steps to build and run this application:
1. Open a command line window or terminal.
1. Navigate to the root directory of the project, where the `pom.xml` resides.
1. Compile the project: `mvn clean compile`.
1. Package the application: `mvn package`.
1. Change into the `target` directory: `cd target`
1. You should see a file with the following or a similar name: `jersey-websockets-undertow-1.0.jar`.
1. Execute the JAR: `java -jar jersey-websockets-undertow-1.0.jar`.
1. A page to test the application will be available at `http://localhost:8080/index.html`. The following endpoints will be available:
1. `http://localhost:8080/api/messages`: REST endpoint over HTTP to broadcast a message to the WebSocket clients
1. `ws://localhost:8080/push`: WebSocket endpoint to get messages pushed by the server### Quick words on Undertow and uber-jars
This application is packed as an [uber-jar](https://stackoverflow.com/q/11947037/1426227), making it easy to run, so you don't need to be bothered by installing a servlet container such as Tomcat and then deploy the application on it. Just execute `java -jar ` and the application will be up and running.
This application uses [Undertow](http://undertow.io/), a lighweight Servlet container designed to be fully embeddable. It's used as the default web server in the [WildFly Application Server](http://wildfly.org/).
The uber-jar is created with the [Apache Maven Shade Plugin](https://maven.apache.org/plugins/maven-shade-plugin/), that provides the capability to create an executable jar including its dependencies.
### Using the application
Browse to `http://localhost:8080/index.html`, type a message in the text field and click _Broadcast message_ to send the message to all WebSocket clients. A new WebSocket connection will be created for each tab you open and the message will be broadcasted to them as well.
Alternatively to the test page, you can broadcast a message with [curl][]:
```bash
curl -X POST \
'http://localhost:8080/api/messages' \
-H 'Content-Type: application/json' \
-d '{
"message": "Hello from curl!"
}'
```And [Postman][] also can be used to target the REST API. Refer to the [`src/main/postman`](src/main/postman) directory for the collection files.
[CDI events]: https://docs.oracle.com/javaee/7/tutorial/cdi-adv005.htm
[cURL]: https://curl.haxx.se/
[Postman]: https://www.getpostman.com/