I am using a simplepush.php script to send voip pushes from user to user. My app can potentially make many of these push requests depending on how many users it acquires. Every example of simplepush.php that I found seems to explicitly close the connection at the end - here is my script (see last line):
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'voip.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client($apnsUrl, $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
$body['info'] = array(
'roomname' => $roomName,
'uuid' => $uuid
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
// Close the connection to the server
fclose($fp);
Please note: I am using the legacy APNs binary interface to send notifications instead of an HTTP/2 request because all the simplepush scripts used it. I am not well versed in PHP, but it seems like the script is closing the connection at the end of every call: fclose($fp);
But according to Apple I should leave the connection open:
Best Practices for Managing Connections Keep your connections with APNs open across multiple notifications; do not repeatedly open and close connections. APNs treats rapid connection and disconnection as a denial-of-service attack. https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW6
But since I'm using the legacy binary interface, should I actually be closing the connection after every call? Or am I misunderstanding the fclose($fp);
function here? Any clarity on the appropriate way to handle the connection when using this binary would be much appreciated!