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
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)
PNTimeoutCategory - response was not received before specified timeout.
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.
These status events may indicate network issues:
PNNetworkIssuesCategory. Unity V4 has exclusive statuses that can reveal deeper information about the network from the SDK APIs.
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 Unity V4 and indicates that SDK reported network stability and it was instructed to reconnect. This status is going to be the first status fired after a connectivity disruption.
This status is exclusive to Unity V4 and indicates that the SDK reported network instability and it 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 SDK.
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
The Unity V4 platform ships with reconnection policy disabled by default and can be enabled by setting
PNConfiguration to one of the following values:
PNReconnectionPolicy.NONE- indicates that
NOaction will taken when there is a network or internet issue.
PNReconnectionPolicy.LINEAR- SDK will try to reconnect each 3 seconds.
PNReconnectionPolicy.EXPONENTIAL- SDK uses the Exponential Backoff algorithm to reconnect when there is a network or internet issue. SDK uses
32seconds. See: https://en.wikipedia.org/wiki/Exponential_backoff for more details.
On some platforms, application owners have more visibility into networking conditions than the SDK. You can call the pubnub.Reconnect() on each of the SDKs to force the SDK to try and reach out PubNub.