Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VAPID header caching #208

Merged
merged 4 commits into from
Feb 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ serviceWorkerRegistration.pushManager.subscribe({
})
```

#### Reusing VAPID headers

VAPID headers make use of a JSON Web Token (JWT) to verify your identity. That token payload includes the protocol and hostname of the endpoint included in the subscription and an expiration timestamp (usually between 12-24h), and it's signed using your public and private key. Given that, two notifications sent to the same push service will use the same token, so you can reuse them for the same flush session to boost performance using:

```php
$webPush->setReuseVAPIDHeaders(true);
```

### Notifications and default options
Each notification can have a specific Time To Live, urgency, and topic.
You can change the default options with `setDefaultOptions()` or in the constructor:
Expand Down
51 changes: 48 additions & 3 deletions src/WebPush.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ class WebPush
*/
private $automaticPadding = Encryption::MAX_COMPATIBILITY_PAYLOAD_LENGTH;

/**
* @var bool Reuse VAPID headers in the same flush session to improve performance
*/
private $reuseVAPIDHeaders = false;

/**
* @var array Dictionary for VAPID headers cache
*/
private $vapidHeaders = [];

/**
* WebPush constructor.
*
Expand Down Expand Up @@ -173,6 +183,10 @@ public function flush(?int $batchSize = null) : iterable
yield $result;
}
}

if ($this->reuseVAPIDHeaders) {
$this->vapidHeaders = [];
}
}

/**
Expand Down Expand Up @@ -256,11 +270,26 @@ private function prepare(array $notifications): array
throw new \ErrorException('Audience "'.$audience.'"" could not be generated.');
}

if (!$contentEncoding) {
throw new \ErrorException('Subscription should have a content encoding');
$vapidHeaders = null;
$cache_key = null;
if ($this->reuseVAPIDHeaders) {
$cache_key = implode('#', [$audience, $contentEncoding, crc32(serialize($vapid))]);
if (array_key_exists($cache_key, $this->vapidHeaders)) {
$vapidHeaders = $this->vapidHeaders[$cache_key];
}
}

$vapidHeaders = VAPID::getVapidHeaders($audience, $vapid['subject'], $vapid['publicKey'], $vapid['privateKey'], $contentEncoding);
if (!$vapidHeaders) {
if (!$contentEncoding) {
throw new \ErrorException('Subscription should have a content encoding');
}

$vapidHeaders = VAPID::getVapidHeaders($audience, $vapid['subject'], $vapid['publicKey'], $vapid['privateKey'], $contentEncoding);
}

if ($this->reuseVAPIDHeaders) {
$this->vapidHeaders[$cache_key] = $vapidHeaders;
}

$headers['Authorization'] = $vapidHeaders['Authorization'];

Expand Down Expand Up @@ -321,6 +350,22 @@ public function setAutomaticPadding($automaticPadding): WebPush
return $this;
}

/**
* @return bool
*/
public function getReuseVAPIDHeaders()
{
return $this->reuseVAPIDHeaders;
}

/**
* Reuse VAPID headers in the same flush session to improve performance
*/
public function setReuseVAPIDHeaders($enabled)
{
$this->reuseVAPIDHeaders = $enabled;
}

/**
* @return array
*/
Expand Down