By Admin on Friday, 05 May 2023
Category: All

WebPush Notifications

From sgcWebSockets 2023.4.0 WebPush notifications are supported. The Web Push protocol is a technology used to deliver real-time notifications to users on the web. The three main features of the Web Push protocol are:

  1. Push Notifications: The Web Push protocol allows web applications to send notifications to users even when the application is not open or active. Users can receive push notifications on their devices, such as desktops, laptops, or mobile phones, without having to be on the website.
  2. User Consent: The Web Push protocol requires user consent before notifications can be sent. This means that users must opt-in to receive push notifications from a website, and they can also easily opt-out at any time.
  3. Encryption: The Web Push protocol uses encryption to secure the communication between the server and the user's device. This ensures that the notifications are delivered securely and cannot be intercepted or tampered with by third parties.

Introduction

The WebPush protocol is defined by the RFC 8030 (Delivery using HTTP Push) and RFC 8291 (Message Encryption).

Web Push is a standardized protocol for delivering push notifications to web browsers. It uses the Push API, which is a standard web API that enables websites to register and receive push messages. The Push API allows a website to send push messages to a user's browser, even when the user is not actively browsing the website.

To use Web Push, a website first needs to obtain a push subscription from the user's browser. The subscription consists of a unique endpoint URL and an encryption key. The endpoint URL is a URL that the website can use to send push messages to the user's browser, and the encryption key is used to encrypt and decrypt the push messages.

Once the website has obtained a push subscription, it can send push messages to the user's browser by making an HTTP request to the endpoint URL. The push message is sent in a special format called the Web Push Protocol Message, which consists of a set of headers and a payload. The headers contain information such as the encryption key and the TTL (time-to-live) of the message, while the payload contains the actual content of the message.

When the user's browser receives a push message, it first decrypts the message using the encryption key. It then displays the notification to the user, along with any additional actions that the user can take, such as dismissing the notification or opening the website.

To ensure the security and privacy of push messages, Web Push uses end-to-end encryption and requires that push subscriptions be obtained over a secure connection (e.g., HTTPS). Additionally, the protocol provides mechanisms for authenticating the sender of a push message and preventing abuse (e.g., by limiting the number of push messages that a website can send to a user). 

Configuration

  1. Attach a TsgcWSServer_API_WebPush to a WebSocket server using the Server property.
  2. Configure the public and private keys in the WebPush.VAPID property. (Registered users can download an executable that generates the VAPID keys for windows).
  3. Requires to deploy the openSSL 3.0.0 version
  4. In the WebPush.Endpoints property you can define your own endpoints to handle the webpush subscriptions, by default, accessing to the "/sgcWebPush.html" endpoint will show a simple webpage that enables to Subscribe to the WebPush notifications.
  5. Start the server and access to the endpoint configured to test it.

Handling Subscriptions

Every time a new client subscribes, the event OnWebPushSubscription is called, here you can save the subscription object into a database to store all subscriptions and reload if the server is restarted. 

The WebPush server has an internal property, called Subscriptions, where all Subscriptions are inserted or removed automatically. If the server is restarted, you can load the previously stored subscription using the methods Subscriptions.AddSubscription and Subscriptions.RemoveSubscription

Sending Notifications

Use this method to send a notification given a subscription object. The subscription object is just a class with the following properties


The message can be a string or an object of TsgcWebPushMessage 

Broadcast Notifications

​Using this method, the message will be sent to all subscribed clients, find below a simple example using delphi code.

​WebPush Video

This browser does not support the video element.

Delphi Source Code and Compiled Demo

Find below the delphi compiled demo for windows and the source code of the WebPush Server Demo. 

Related Posts