2
votes

We're using APNS for our app and after suddenly getting a lot of lag on connecting to the APNS servers I found out that we're supposed to leave the connection open. I have little understanding about how stream_socket_client works and can't seem to find the answers to these questions:

  • We are sending notifications to 2 different apps with 2 different certificates, is the same connection reused for both is there one open for each?
  • What is the lifespan of the connection? Does it automatically close or do we need to write in something that will close them from time to time?
  • How do I review the number of open connections and their information to ensure that we don't end up with duplicates or too many open connections?
  • Are their any advantages to using STREAM_CLIENT_ASYNC_CONNECT instead of STREAM_CLIENT_PERSISTENT? Is async also persistent?

Here's the snippet of our connection code for your review, it is called every time a notification needs to be sent (we cannot batch notifications because our game is a turn-based word game - Wordspionage - with some tight time limits).

stream_context_set_option($ctx, 'ssl', 'local_cert', $cert); stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$url = 'ssl://gateway.push.apple.com:2195';

$fp = stream_socket_client(
    $url , $err,
    $errstr, 4, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

Thanks for the help!

1

1 Answers

1
votes

I can answer some of your questions :

  1. Each application requires a separate connection, since each app has its own push certificate which is required in order to establish the TLS connection with APNS servers.

  2. You don't have to ever close the connections. Apple may close it at any time (in which case you should open a new connection). It will close it for certain if you send invalid data (such as invalid device tokens, too long payload, etc...), but it may close it even when there are no errors.

  3. You should decide how many open connections you wish to maintain simultaneously (based on the frequency in which you send notifications and the number of threads sending notifications). You will need at least one connection for each application, but you may want more. For each open connection, you should continue sending notifications until the sending fails, in which case you can assume that connection is closed and open a new one to replace it.

  4. I don't know PHP, so I can't help you with that.

In addition, I suggest you read about Push Notification Throughput and Error Checking in this document.