Just a second...

Reconnect to Diffusion Cloud

When clients connect to Diffusion™ Cloud over unreliable networks these connections can be lost. Clients can attempt to reconnect to Diffusion Cloud after they lose connection.

Diffusion Cloud keeps client sessions in the DISCONNECTED state for 60 seconds, during which time the client can reconnect to the same session.

Configuring reconnection on the client

Clients have reconnection enabled by default.

You can configure a reconnection timeout that restricts the amount of time the client can be disconnected and still reconnect to its session on Diffusion Cloud. The period of time that Diffusion Cloud keeps the session available for reconnect is the lowest of the following values:
  • The reconnection timeout configured by the client when it creates its session
  • The default reconnection timeout of 60 seconds (60000 ms)
When the reconnection timeout period configured by the client ends, the client stops attempting to reconnect and closes its session.
JavaScript
diffusion.connect({
    host : 'url',
    reconnect : {
        // Specify the timeout in milliseconds
        timeout : reconnection_time 
		}
    })
Apple
PTDiffusionMutableSessionConfiguration *const configuration =
    [PTDiffusionMutableSessionConfiguration new];

// Specify the timeout in seconds
configuration.reconnectionTimeout = @10;

[PTDiffusionSession openWithURL:[NSURL URLWithString:@"url"]
                  configuration:configuration
              completionHandler:^(PTDiffusionSession *session, NSError *error)
 {
     // Check error is `nil`, then use session as required.
     // Ensure to maintain a strong reference to the session beyond the lifetime
     // of this callback, for example by assigning it to an instance variable.
 }];
Java and Android
final Session session = Diffusion
        .sessions()
        // Specify the timeout in milliseconds
        .reconnectionTimeout(reconnection_time)						
        .open("url");
.NET
var session = Diffusion.Sessions
        // Specify the timeout in milliseconds
        .ReconnectionTimeout(reconnection_time)
		.Open("url");
C
reconnection_strategy_set_timeout(&reconnection_strategy, reconnection_time);
SESSION_T *session = session_create(url, NULL, NULL, NULL, &reconnection_strategy, NULL);

Set the value of the reconnection timeout to zero to disable reconnection. If no reconnection timeout is specified, a default of 60 seconds (60000 ms) is used.

You can also define your own custom reconnection behavior using reconnection strategies. For more information, see Specifying a reconnection strategy.

If no custom reconnection strategy is defined, the client attempts to reconnect at five second intervals until the reconnection timeout is reached.

Reliable reconnection

If a client loses connection to Diffusion Cloud, data sent between the client and Diffusion Cloud in either direction might be lost in transmission. If this happens and the client reconnects to its session on Diffusion Cloud, lost data might cause the client state or topic data to be incorrect.

To prevent any data being lost, the reconnection process re-synchronizes the streams of messages from the client session to Diffusion Cloud and from Diffusion Cloud to the client session. When reconnecting, the client notifies Diffusion Cloud of the last message received and the earliest message it can send again. The Diffusion Cloud server resends any missing messages and instructs the client session to resume from the appropriate message.

To be able to send messages again, Diffusion Cloud maintains a recovery buffer of sent messages. Some types of client also maintain a recovery buffer of sent messages that can be sent again if necessary.

If a message has been lost and is no longer present in the recovery buffer, the server will abort the reconnection. If reconnection succeeds, delivery of all messages is assured.

Configuring the recovery buffer on the client

All Diffusion Cloud clients can retain a buffer of messages that they have sent to Diffusion Cloud. If messages from the client are lost in transmission during a disconnection and subsequent reconnection, the client can resend the missing messages to Diffusion Cloud.

In the Apple®, Java™, Android™ and .NET APIs, you can configure the size of this buffer, in messages, when creating your session on Diffusion Cloud:

Apple
PTDiffusionMutableSessionConfiguration *const configuration =
    [PTDiffusionMutableSessionConfiguration new];

configuration.recoveryBufferSize = 1000;

[PTDiffusionSession openWithURL:[NSURL URLWithString:@"url"]
                  configuration:configuration
              completionHandler:^(PTDiffusionSession *session, NSError *error)
{
    // Check error is `nil`, then use session as required.
    // Ensure to maintain a strong reference to the session beyond the lifetime
    // of this callback, for example by assigning it to an instance variable.
}];
Java and Android
final Session session = Diffusion
        .sessions()
        .recoveryBufferSize(number_of_messages)							
        .open("url");
.NET
var session = Diffusion.Sessions.RecoveryBufferSize( number_of_messages ).Open("url");

The default size of the recovery buffer is 128 messages.

The larger this buffer is, the greater the chance of successful reconnection. However, a larger buffer of messages increases the memory footprint of a client.