iOSiOSCocoaObjective-CPubNub Network Lifecycle for iOS

It is users responsibility to prepare client's configuration instance and pass it during PubNub client instantiation. As long as there is a reference to the client instance it will be alive. The initialized client doesn't perform any activities until activated by user commands.

Client doesn't check for active connection on its own and instead relies on network API response.

On a user command, the client prepares a REST request to PubNub API and passes it to the networking API for processing. If there were no issues and the response arrived in time (timeout can be specified), the results are parsed and wrapped into objects which are returned using callbacks.

If there was an issue during request processing, networking API will return information about the error which will then be parsed and wrapped into objects and returned to the caller.

In case of network issues, Realtime API's will return status object with category set to PNNetworkIssuesCategory.

There are also a set of other error categories:

PNNetworkIssuesCategory - (in case there was no connection at the moment when the client tried to send request)

PNTLSConnectionFailedCategory - TLS handshake issues and in most cases because of poor connection quality and packets loss and delays.

PNTimeoutCategory - response was not received before specified timeout.

PNUnknownCategory - non 200 HTTP response code from the server was received.

Heartbeat API can be used as way to detect when network issues appear. Heartbeats send a request at intervals (which is set during client instance configuration) and utilize observer's callback to report successful and failed attempts. There is a chance that the network will get spotty between calls to this API and the socket with active long-poll (subscribe API for realtime messages) may not notice those issues and listen for events from a broken pipe.

There are three status events which may tell what there is some network issues: PNTimeoutCategory, PNUnexpectedDisconnectCategory and PNNetworkIssuesCategory. Objective-C has exclusive statuses that can reveal deeper information about the network from the browser API's.

This status may arrive if non-subscribe request didn't receive a response from the server in time. This may happen only if:

  • Issues on service itself.
  • There is a proxy between client and PubNub service which didn't send the request or didn't return a response in time
  • There are issues with spotty network signal quality (or a lot of noise on the band)

No connection at the moment when request has been sent. This status is returned immediately to calling API through callback.

  • During subscription API usage, the client stumbled on a network issue or an unexpected response has been received.
  • There were network issues and the networking API reported back to our SDK to generate error.
  • service itself (may happen only if data provided to client has been malformed and break composed URI) or intermediate software (proxy server) returned response which can't be parsed by subscribe parser.

This status is exclusive to Objective-C and indicates that browser reported network stability and the SDK was instructed to reconnect. This status is going to be the first status fired after a connectivity disruption.

This status is exclusive to Objective-C and indicates that the browser reported network instability and the SDK will lose connection to the host. This status will be fired, but there are no guarantees on the chain of events (subscribe may timeout first) depending on the browser.

There is PNUnexpectedDisconnectCategory which can be generated in the middle of client lifecycle while it subscribed to real-time data channels.

In case that this status arrives, client starts pinging PubNub services using Time API every 10 seconds. As soon as a request is processed successfully, client will try to restore subscription on channels on which it was subscribed previously. If configured to keep timetoken on subscription restore, client will try to catch up on missed messages.

When client completes subscription restore after PNUnexpectedDisconnectCategory it will generate PNReconnectedCategory to notify observers what client has been reconnected to real-time data channels.

If more than 100 messages will arrive (from PubNub in-memory messages cache), it is better to use history API to fetch potentially missed messages. To be notified about this case, requestMessageCountThreshold should be set to 100 - this will tell client to send PNRequestMessageCountExceededCategory status.

PubNub client allow user to describe behavior in case of network issue. For this purpose there is two parameters in PNConfiguration: restoreSubscription and catchUpOnSubscriptionRestore. Both values by default set to YES which configure client to restore subscription after it has been in unexpected disconnect state (because of network issues) and use last received time token to try catch up on missed messages.

  1. restoreSubscription: instruct on whether client should restore subscription to channels after network issues or not. If set to NO after network failure client will clean up list of channels on which it subscribed and after network will go up again won't do anything.
  2. catchUpOnSubscriptionRestore: instruct client whether after subscription restore (if restoreSubscription is set to YES) should use time token from previous subscribe loop (time token which has been received with last message/event while client has been connected). If property set to YES client will request and use new time token and all messages which has been sent while client was offline will be lost (sure they can be received with history calls).
  3. keepTimeTokenOnListChange: this property not related to reconnection logic, but also may confuse users. If set to YES client will reuse previous time token when user subscribe to new channels. If set to NO client will use time token which will be received with subscription using new channels. If client has been subscribed to some channels with tt1 and subscribe to new channels, it will ignore tt2 (received in response for subscribe with new channels from PubNub service) and use tt1 to complete subscribe loop if set to YES. This also mean, what if since tt1 in new channels was some messages - they will be received as well (this is what some users reported what old messages has been received). If property set to NO client will use tt2 to complete subscribe loop.

The Apple ecosystem will begin pinging the time endpoint at regular intervals until the connection is restored or application goes into the background.

On some platforms, application owners have more visibility into networking conditions than the SDK. You can subscribe on same set of channels or empty list and client will terminate currently active long-poll request and start new one.

For the Apple SDKs, you can just subscribe to the same set of channels or an empty array to force a manual reconnect.

403 response on a subscribe event occurs when Access Manager is enabled and the user doesn't have access to the channel or the grant TTL has expired. In this case you need to create a new auth key, grant access to the user (from your server) and subscribe again.

For more info please check: http://www.pubnub.com/docs/ios-objective-c/pam-security#handling-permission-denied-errors

To completely disable PubNub's logger (even debug messages on client initialization) it is possible to set Preprocessing Macros in Build Settings: PUBNUB_DISABLE_LOGGER (just add keys itself w/o any values will work).

After macro added, code which is responsible for data output to stdout and file won't be compiled and calls to logger won't do anything.

If project has been added with CocoaPods, it is possible to add this macro automatically, if next code will be added into Podfile:

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'PUBNUB_DISABLE_LOGGER'] if target.name =~ /^PubNub/
        end
    end
end

This hook will add macro for you, when dependency will be installed on pod install or pod update call.