PubNub is real time, with less than 250 milliseconds latency anywhere in the world. However, even though PubNub is low latency, a device's actual physical connection to the Internet could be extremely high latency, with 50% packet loss or more. In these cases, there are a set of best practices to adopt to help mitigate user issues.
In this case, we initialize the client instance with a shorter subscribe timeout so that if a high packet loss connection is established, we do not wait a full 310 seconds (default timeout) for a better connection. Normally, the 310 seconds is the most ideal length for a connection, but in high packet loss situations, this can cause gaps in messages as high packet loss connections wait minutes until a good connection finally establishes.
Objective-C
PNConfiguration *config = [PNConfiguration configurationWithPublishKey:@"demo" subscribeKey:@"demo"];
config.subscribeMaximumIdleTime = 155.0; // We are decreasing the subsribe timeout by half
config.heartbeatNotificationOptions = PNHeartbeatNotifyFailure; // We are opting in to failed heartbeat notifications
config.presenceHeartbeatValue = 60.0; // Make sure to set a value for heartbeats so that it triggers
config.presenceHeartbeatInterval = 29.0; // This is the time interval between client heartbeat request attempts
self.client = [PubNub clientWithConfiguration:config]; // Finally create the client
Swift
let config = PNConfiguration(publishKey: "demo", subscribeKey: "demo")
config.subscribeMaximumIdleTime = 155.0 // We are decreasing the subsribe timeout by half
config.heartbeatNotificationOptions = [.notifyFailure] // We are opting in to failed heartbeat notifications
config.presenceHeartbeatValue = 60.0 // Make sure to set a value for heartbeats so that it triggers
config.presenceHeartbeatInterval = 29.0 // This is the time interval between client heartbeat request attempts
self.client = PubNub.clientWithConfiguration(config)
When outgoing heartbeat requests from clients fail, this is a hint that the client may be experiencing network issues. Use this as an opportunity to tear down and re-establish subscriptions. The code below would be in whatever listener is added to the PubNub client instance created above:
Objective-C
- (void)client:(PubNub *)client didReceiveStatus:(PNStatus *)status {
if ((status.operation == PNHeartbeatOperation) && status.isError && (status.category != PNAccessDeniedCategory)) {
// We are going to react to any failed heartbeat operations here
// By subscribing to an empty array of channels, we force the client
// to re-establish its connection to PubNub, as a method to fight
// issues with packet loss
[client subscribeToChannels:@[] withPresence:YES];
} else {
// Do other status actions here
}
}
Swift
func client(_ client: PubNub, didReceive status: PNStatus) {
if ((status.operation == .heartbeatOperation) && status.isError && (status.category != .PNAccessDeniedCategory)) {
// We are going to react to any failed heartbeat operations here
// By subscribing to an empty array of channels, we force the client
// to re-establish its connection to PubNub, as a method to fight
// issues with packet loss
client.subscribeToChannels([], withPresence: true)
} else {
// Do other status actions here
}
}