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 sessionConfiguration = [PTDiffusionMutableSessionConfiguration new];

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

    [PTDiffusionSession openWithURL:url
                      configuration:sessionConfiguration
                  completionHandler:^(PTDiffusionSession *newSession, NSError *error)
     {
         if (newSession) {
             NSLog(@"Session open");
         } else {
             NSLog(@"Session Failed to open with error: %@", error);
         }
     }];
					
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, 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 client to Diffusion Cloud and from Diffusion Cloud to client. 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 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

JavaScript®, Java™, Android™, and C clients can retain a buffer of messages that they have sent to Diffusion Cloud. In the case when 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 Java and Android, you can configure the size of this buffer, in messages, when creating your session on Diffusion Cloud:

Java and Android
final Session 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.