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.
- The reconnection timeout configured by the client when it creates its session
- The default reconnection timeout of 60 seconds (60000 ms)
const session = await diffusion.connect({ host : 'host_name', reconnect : { // Specify the timeout in milliseconds timeout : reconnection_time } });
var session = Diffusion.Sessions // Specify the timeout in milliseconds .ReconnectionTimeout(1000) .Open("url");
final Session session = Diffusion .sessions() // Specify the timeout in milliseconds .reconnectionTimeout(5000) .open("ws://localhost:8080");
RECONNECTION_STRATEGY_T *reconnection_strategy = make_reconnection_strategy_repeating_attempt(retry_count, retry_delay); reconnection_strategy_set_timeout(reconnection_strategy, timeout); SESSION_T *session = session_create( url, principal, credentials, NULL, reconnection_strategy, NULL);
let configuration = PTDiffusionMutableSessionConfiguration() // Specify the reconnection timeout (seconds) configuration.reconnectionTimeout = 10; // Use the configuration to open a new session... PTDiffusionSession.open(with: URL(string: "url")!, configuration: configuration) { (session, error) in // 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. if (session == nil) { print("Failed to open session: %@", error!.localizedDescription) return } // At this point we now have a connected session. print("Connected.") }
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 :
int numberOfMessages = 1000; var session = Diffusion.Sessions.RecoveryBufferSize(numberOfMessages).Open("url");
final Session session = Diffusion .sessions() // Specify the buffer size in number of messages .recoveryBufferSize(200) .open("ws://localhost:8080");
DIFFUSION_SESSION_FACTORY_T *session_factory = diffusion_session_factory_init(); diffusion_session_factory_principal(session_factory, principal); diffusion_session_factory_credentials(session_factory, credentials); diffusion_session_factory_recovery_buffer_size(session_factory, recovery_buffer_size); SESSION_T *session = session_create_with_session_factory(session_factory, url);
let configuration = PTDiffusionMutableSessionConfiguration() // Specify the recovery buffer size (number of messages) configuration.recoveryBufferSize = 1000; // Use the configuration to open a new session... PTDiffusionSession.open(with: URL(string: "url")!, configuration: configuration) { (session, error) in // 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. if (session == nil) { print("Failed to open session: %@", error!.localizedDescription) return } // At this point we now have a connected session. print("Connected.") }
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.