Just a second...

Reconnect to the Diffusion server

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

Diffusion keeps client sessions in the DISCONNECTED state for a period of time, during which the client can reconnect to the same session. The length of time the Diffusion server keeps a client session in the DISCONNECTED state for is configured for the connector that the client uses. For more information, see Configuring connectors.

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 the Diffusion server. The period of time that the Diffusion server 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 reconnection timeout configured on the Diffusion server for the connector that the client connects on
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 the Diffusion server, data sent between the client and the Diffusion server 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 the Diffusion server and from the Diffusion server to client. When reconnecting, the client notifies the Diffusion server of the last message received and the earliest message it can send again. The Diffusion server resends any missing messages and instructs the client to resume from the appropriate message.

To be able to send messages again, the Diffusion server 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 the Diffusion server. 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 the Diffusion server.

In Java and Android, you can configure the size of this buffer, in messages, when creating your session on the Diffusion server:

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.

Configuring the recovery buffer on the Diffusion server

The recovery buffers on the Diffusion server can be configured on a per-connector basis in the Connectors.xml configuration file. For more information, see Configuring connectors.