web-push-php
web-push-php copied to clipboard
504 Gateway Time-out When sendNotification > 500
Thanks for this library. I have integrated it into my website and conducted a test submission.
Size subscriptions | Time 1 (s) | Time 2 (s) |
---|---|---|
100 | 4 | 3.7 |
200 | 8 | 7.4 |
400 | 14 | 14.4 |
800 | 504 Gateway Time-out | 504 Gateway Time-out |
It seems that my code was not optimal when sending in bulk! Give me a solution to solve this
Code:
$webPush - > setDefaultOptions([
'TTL' => 86400, // if undelivered after 1 day, expire the notification
]);
$subscriptions = $this - > getReceiverSubscriptions();
foreach($subscriptions AS $subscription) {
$authData = json_decode($subscription['data'], true);
$payload['subscriber_id'] = $subscription['endpoint_id'];
$array = [
'endpoint' => $subscription['endpoint'],
'publicKey' => $authData['key'],
'authToken' => $authData['token'],
'contentEncoding' => $authData['encoding']
];
try {
$webPush - > setAutomaticPadding($this - > getEndpointPadding($subscription['endpoint']));
$subObj = Subscription::create($array);
$webPush - > sendNotification(
$subObj,
json_encode($payload)
);
} catch (\Exception $e) {
// generally indicates that the payload is too big which at ~3000 bytes shouldn't happen for a typical alert...
continue;
}
}
foreach($webPush - > flush() as $report) {
$results[] = $report - > jsonSerialize();
}