14 KiB
Flic to PWA WebPush Backend
This project provides a self-hosted backend service that listens for HTTP requests from Flic smart buttons and triggers Web Push notifications to specific Progressive Web App (PWA) instances. The goal is to allow a Flic button press (Single Click, Double Click, Hold) to trigger actions within the PWA via push messages handled by a Service Worker.
It's designed to be run as a Docker container and integrated with Traefik v3 for SSL termination and routing.
Features
- Receives GET requests on
/webhook. - Receives POST requests on
/subscribeto manage button-PWA mappings. - Uses HTTP headers
Button-NameandTimestampfrom the Flic request. - Gets
click_typefrom URL path. - Looks up the target PWA push subscription based on the
Button-Nameheader in a JSON file. - Sends a Web Push notification containing the click details (action, button, timestamp) to the corresponding PWA subscription.
- Integrates with Traefik v3 via Docker labels.
- Configurable via environment variables (
.envfile). - Optional Basic Authentication for securing the
/webhookand/subscribeendpoints. - CORS configuration for allowing requests (needed if your PWA management interface interacts with the
/subscribeendpoint).
Prerequisites
- Docker: Install Docker
- Traefik: A running Traefik v3 instance configured with SSL (Let's Encrypt recommended) and connected to a Docker network named
traefik. You need to know your certificate resolver name. - Domain Name: A domain or subdomain pointing to your Traefik instance (e.g.,
webpush.virtonline.eu). This will be used for the webhook URL. - Flic Hub/Service: Configured to send HTTP requests for button actions. You'll need the serial number(s) of your Flic button(s).
- Node.js & npm/npx (Optional): Needed only locally to generate VAPID keys easily. Not required for running the container.
- PWA Push Subscription Details: You need to obtain the Push Subscription object (containing
endpoint,keys.p256dh,keys.auth) from your PWA after the user grants notification permission.
System Architecture
Subscription Flow
Interaction Flow
Project Structure
Setup
-
Clone the Repository:
git clone https://gitea.virtonline.eu/2HoursProject/flic-webhook-webpush.git cd flic-webhook-webpush -
Generate VAPID Keys: Web Push requires VAPID keys for security. Generate them once and store them into
.env. You can usenpx:npx web-push generate-vapid-keysThis will output a Public Key and a Private Key.
-
Configure Environment Variables:
- Copy the example
.envfile:cp .env.example .env - Edit the
.envfile with your specific values:VAPID_PUBLIC_KEY: The public key generated in step 2. Your PWA will also need this key when it subscribes to push notifications.VAPID_PRIVATE_KEY: The private key generated in step 2. Keep this secret!VAPID_SUBJECT: Amailto:orhttps:URL identifying you or your application (e.g.,mailto:admin@yourdomain.com). Used by push services to contact you.PORT: (Default:3000) The internal port the Node.js app listens on. Traefik will map to this.SUBSCRIPTIONS_FILE: (Default:/app/subscriptions.json) The path inside the container where the button-to-subscription mapping is stored.BASIC_AUTH_USERNAME: (Optional) Username for Basic Authentication. If set along withBASIC_AUTH_PASSWORD, authentication will be enabled for/webhookand/subscribe.BASIC_AUTH_PASSWORD: (Optional) Password for Basic Authentication. If set along withBASIC_AUTH_USERNAME, authentication will be enabled.ALLOWED_ORIGINS: Comma-separated list of domains allowed by CORS. Include your PWA's domain if it needs to interact directly (e.g., for setup). Example:https://my-pwa.com.ALLOWED_METHODS: (Default:POST,OPTIONS) Standard methods needed.ALLOWED_HEADERS: (Default:Content-Type,Authorization) Standard headers needed.MAX_NOTIFICATION_RETRIES: (Default:3) Number of retry attempts for failed push notifications. Must be a number.INITIAL_RETRY_DELAY_MS: (Default:1000) Initial delay in milliseconds before first retry. Must be a number.DNS_TIMEOUT_MS: (Default:5000) DNS resolution timeout in milliseconds. Must be a number.HTTP_TIMEOUT_MS: (Default:10000) HTTP request timeout in milliseconds. Must be a number.LOG_LEVEL: (Default:info) Controls verbosity of logs. Valid values areerror,warn,info, ordebug. Usedebugto see detailed header information and other diagnostic messages.TRAEFIK_SERVICE_HOST: Your public domain for this service (e.g.,webpush.virtonline.eu).TRAEFIK_CERT_RESOLVER: The name of your TLS certificate resolver configured in Traefik (e.g.,le,myresolver).
- Copy the example
-
Configure Traefik Labels:
- Copy the example
labelsfile:cp labels.example labels
- Copy the example
-
Prepare Subscription Mapping File:
- Edit the
subscriptions.jsonfile - Add entries mapping your Flic button's serial number (as a lowercase string key) to the PWA
PushSubscriptionobject.{ "game": { // <-- Replace with your actual Flic Button name (lowercase recommended) "endpoint": "https://your_pwa_push_endpoint...", "expirationTime": null, "keys": { "p256dh": "YOUR_PWA_SUBSCRIPTION_P256DH_KEY", "auth": "YOUR_PWA_SUBSCRIPTION_AUTH_KEY" } } // Add more entries for other buttons if needed } - Ensure this file contains valid JSON.
- Edit the
Running the Service
-
Build the Docker Image: Make sure you are in the
flic-webhook-webpushdirectory.docker build -t flic-webhook-webpush:latest . -
Run the Container: This command runs the container in detached mode (
-d), names it, connects it to thetraefiknetwork, passes environment variables from the.envfile, applies the Traefik labels from thelabelsfile, and mounts thesubscriptions.jsonfile into the container.docker run -d --name flic-webhook-webpush \ --network traefik \ --env-file .env \ --label-file labels \ --mount type=bind,src=./subscriptions.json,dst=/app/subscriptions.json \ flic-webhook-webpush:latest -
Check Logs: Monitor the container logs to ensure it started correctly and to see incoming webhook requests or errors.
docker logs -f flic-webhook-webpushYou should see messages indicating the server started, configuration details, and subscription loading status.
-
Verify Traefik: Check your Traefik dashboard to ensure the
flic-webhook-webpushservice and router are discovered and healthy.
Flic Button Configuration
In your Flic app or Flic Hub SDK interface:
- Select your Flic button.
- Add an "Internet Request" action.
- Fill in the following details:
- Set
GETmethod. - Set URL with query parameter:
https://<your_domain>/webhook/SingleClick(Replace<your_domain>with your actual service domain, e.g.,webpush.virtonline.eu). - If Basic Authentication is enabled:
- Set the
UsernameandPasswordfields to the values from yourBASIC_AUTH_USERNAMEandBASIC_AUTH_PASSWORDenvironment variables.
- Set the
- If Basic Authentication is disabled:
- Leave the
UsernameandPasswordfields empty.
- Leave the
- Tap on
Save action.
- Set
- Repeat for Double Click and/or Hold events, changing the
click_typeparameter accordingly (e.g.,/DoubleClick).
API Endpoints
-
POST /subscribe- Description: Adds or updates the Web Push subscription associated with a specific Flic button ID.
- Authentication: Optional Basic Authentication via
Authorizationheader ifBASIC_AUTH_USERNAMEandBASIC_AUTH_PASSWORDare configured. - Request Body: JSON object containing:
button_id(string, required): The unique identifier for the Flic button (lowercase recommended, e.g., "game", "lights").subscription(object, required): The PushSubscription object obtained from the browser's Push API.
{ "button_id": "game", "subscription": { "endpoint": "https://your_pwa_push_endpoint...", "expirationTime": null, "keys": { "p256dh": "YOUR_PWA_SUBSCRIPTION_P256DH_KEY", "auth": "YOUR_PWA_SUBSCRIPTION_AUTH_KEY" } } } - Responses:
201 Created: Subscription saved successfully.400 Bad Request: Missing or invalidbutton_idorsubscriptionobject in the request body.401 Unauthorized: Missing or invalid Basic Authentication credentials (if authentication is enabled).500 Internal Server Error: Failed to save the subscription to the file.
-
GET /webhook/:click_type- Description: Receives Flic button events.
- Authentication: Optional Basic Authentication via
Authorizationheader ifBASIC_AUTH_USERNAMEandBASIC_AUTH_PASSWORDare configured. - URL Parameters:
click_type(required): The type of button press (e.g.,SingleClick,DoubleClick, orHold).
- Required Headers:
Button-Name(required): The identifier of the Flic button (sent by the Flic system).
- Optional Headers:
Timestamp: Timestamp of the button event (sent by the Flic system).Button-Battery-Level: The battery level percentage of the button (sent by the Flic system).
- Push Notification Payload (
datafield): The service sends a JSON payload within the push notification. The client-side Service Worker can access this data viaevent.data.json(). The structure is:{ "action": "SingleClick", // or DoubleClick, Hold "button": "game-button", // Normalized button name (lowercase) "timestamp": "2024-03-28T15:00:00.000Z", // ISO 8601 timestamp or current server time "batteryLevel": 100 // Integer percentage (0-100) or 'N/A' if not provided } - Responses:
200 OK: Webhook received, push notification sent successfully.400 Bad Request: MissingButton-Nameheader orclick_typeURL parameter.401 Unauthorized: Missing or invalid Basic Authentication credentials (if authentication is enabled).404 Not Found: No subscription found insubscriptions.jsonfor the givenButton-Name.410 Gone: The push subscription associated with the button was rejected by the push service (likely expired or revoked).500 Internal Server Error: Failed to send the push notification for other reasons.
Testing the Webhook
Once your service is up and running, you can test the webhook endpoint using curl or any API testing tool. This example assumes Basic Authentication is enabled.
Note: Replace <username>, <password>, <your_domain>, and <button_name> with your actual values.
# Generate Base64 credentials (run this once)
# echo -n '<username>:<password>' | base64
# Example using generated Base64 string (replace YOUR_BASE64_CREDS)
curl -X GET "https://<your_domain>/webhook/SingleClick" \
-H "Authorization: Basic YOUR_BASE64_CREDS" \
-H "Button-Name: <button_name>" \
-H "Timestamp: $(date -u +%Y-%m-%dT%H:%M:%SZ)" \
-H "Button-Battery-Level: 100" \
# Example using curl's built-in Basic Auth (-u)
curl -X GET "https://<your_domain>/webhook/SingleClick" \
-u "<username>:<password>" \
-H "Button-Name: <button_name>" \
-H "Timestamp: $(date -u +%Y-%m-%dT%H:%M:%SZ)" \
-H "Button-Battery-Level: 100" \
The expected response should be:
{"message":"Push notification sent successfully"}
If successful, the above response indicates that:
- Your webhook endpoint is properly configured
- The button ID was found in your subscriptions.json file
- The web push notification was successfully sent to the registered PUSH API endpoint (e.g. https://jmt17.google.com/fcm/send/cf907M...)
If you receive a different response, refer to the Troubleshooting section below.
Troubleshooting
- Check Backend Logs:
docker logs flic-webhook-webpush. Look for errors related to configuration, file access, JSON parsing, authentication, or sending push notifications.- To see detailed debug information including all headers received from the Flic button, set
LOG_LEVEL=debugin your .env file.
- To see detailed debug information including all headers received from the Flic button, set
- Check Traefik Logs:
docker logs traefik. Look for routing errors or certificate issues. - Verify
.env: Ensure all required variables are set correctly, especially VAPID keys and Traefik settings. - Verify
labels: Double-check that variables were correctly substituted manually and match your.envand Traefik setup. - Verify
subscriptions.json: Ensure it's valid JSON and the button serial number (key) matches exactly what Flic sends (check backend logs for "Received webhook: Button=..."). Check if the subscription details are correct. Case sensitivity matters for the JSON keys (button serials). - Check Flic Configuration: Ensure the URL, Method,
click_typeparameter, and authentication details (Username/Password if enabled) are correct in the Flic action setup. Usecurlor Postman to test the endpoint manually first. - PWA Service Worker: Remember that the PWA needs a correctly registered Service Worker to receive and handle the incoming push messages. Ensure the PWA subscribes using the same
VAPID_PUBLIC_KEYconfigured in the backend's.env.