A zerodep-web-push-java example using Spring WebFlux.
Also, this example demonstrates how to work with Reactor Netty HTTP Client
This example is sightly more complex but practical compared to basic and vertx.
In this example, we demonstrate the error handling on requesting the delivery of push notifications.
This is the example for v2. The example for v1 is here.
- JDK17+
- A library for generating ECDSA key pairs(e.g. OpenSSL)
- A browser supporting Push API such as Google Chrome, Microsoft Edge and Firefox
-
Set up and run the application.
git clone https://github.com/st-user/zerodep-web-push-java.git cd zerodep-web-push-java/examples/webflux mkdir .keys cd .keys openssl ecparam -genkey -name prime256v1 -noout -out my-private.pem openssl pkcs8 -in my-private.pem -topk8 -nocrypt -out my-private_pkcs8.pem openssl ec -in my-private.pem -pubout -conv_form uncompressed -out my-pub.pem cd ../Linux/mac OS
./mvnw clean ./mvnw spring-boot:runWindows
./mvnw.cmd clean ./mvnw.cmd spring-boot:run -
Open your browser and access
http://localhost:8080; -
Click the
subscribebutton. -
Enter an arbitrary
messageand click thesendbutton. -
You should see a push notification!
-
(Optional) Since push notifications are handled in the background, we can get them even if we close the browser.
- Close the browser and open a terminal instead.
- Make the application send a push notification by using a command like the following:
curl -X POST http://localhost:8080/sendMessage \ -H 'Content-Type: application/json' \ -d '{ "message": "Message sent with curl." }'
In general, storing private keys in plain text on a file system is not recommended (In the above example, we do that for simplicity).
However, such a method is still useful depending on architectures.
For example:
-
Using Secret Manager secrets in Cloud Run (Google Cloud)
Mount each secret as a volume, which makes the secret available to the container as files.