Skip to content

Instantly share code, notes, and snippets.

@jzucker2
Last active March 28, 2018 23:48
Show Gist options
  • Save jzucker2/2eeaaf00e7490d19aad46317f06ea94b to your computer and use it in GitHub Desktop.
Save jzucker2/2eeaaf00e7490d19aad46317f06ea94b to your computer and use it in GitHub Desktop.
How to deal with high packet loss

High Packet Loss Mitigation

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.

Initializing PubNub client instance

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)

Customize failed heartbeat responses

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
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment