webpush-rfc8188 0.1.0

RFC 8188 WEB PUSH IMPLEMENTATION


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

webpush-rfc8188

RFC 8188 Web Push implementation

Description

Fast RFC 8188 (Message Encryption for Web Push) implementation built with Claude Sonnet 4. Provides complete support for sending push notifications to web browsers with message encryption, VAPID authentication and Web Push protocol delivery.

Example

import std.exception : enforce;
import std.logger;

import vibe.http.client;
import vibe.data.json;

import webpush;

struct Message {
    MessageData data;
}

struct MessageData {
    string title;
    @name("body")
    string _body;
}

auto sendRequest(WebPushRequest webpushRequest) {
    requestHTTP(webpushRequest.endpoint, 
        (scope HTTPClientRequest req) {
            req.method = HTTPMethod.POST;
            foreach (kv; webpushRequest.headers) {
                req.headers[kv.key] = kv.value;
            }
            req.bodyWriter.write(webpushRequest.payload);
        },
        (scope HTTPClientResponse res) {
            if (res.statusCode >= 200 && res.statusCode < 300) {
                infof("✅ SUCCESS: Push notification accepted (status: %s)", res.statusCode);
            } else {
                error("❌ ERROR: Push notification failed with status: %s", res.statusCode);

                try {
                    import vibe.stream.operations;
                    auto errorBody = res.bodyReader.readAllUTF8();
                    if (errorBody.length > 0) {
                        errorf("  Error body: %s", errorBody);
                    }
                } catch (Exception e) {
                    tracef("  Could not read error body: %s", e.msg);
                }
            }
        }
    );
}

void main() {
    import std.logger : globalLogLevel, LogLevel;
    globalLogLevel = LogLevel.trace;
    
    // Browser subscription data (obtained from service worker)
    auto endpoint = "https://fcm.googleapis.com/fcm/send/...";
    auto p256dh = "BEl62iUYgUivxIkv69yViA3..."; // User's public key
    auto auth = "BTBZMqHH6r4Tts7J_aSIgg"; // Authentication secret
    
    // VAPID keys (generate once for your application)
    auto serverPrivateKey = "your-vapid-private-key-here";
    auto serverPublicKey = "your-vapid-public-key-here";
    
    // Create JWT token and notification request
    auto jwtToken = createJWT(endpoint, serverPrivateKey, 3600);
    auto webpushRequest = createWebPushNotificationRequest(
        endpoint, 
        jwtToken, 
        p256dh, 
        auth, 
        serverPublicKey,
        serializeToJsonString(Message(MessageData("Hello World", "This is a push notification!")))
    );
    
    // Send the notification
    sendRequest(webpushRequest);
}
Authors:
  • Sergej Efimov
Dependencies:
vibe-stream:tls, vibe-serialization
Versions:
0.1.0 2025-Aug-14
~main 2025-Aug-14
Show all 2 versions
Download Stats:
  • 2 downloads today

  • 12 downloads this week

  • 19 downloads this month

  • 19 downloads total

Score:
0.4
Short URL:
webpush-rfc8188.dub.pm