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.
- 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
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 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 to its session on the Diffusion server , 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 the Diffusion server and from the Diffusion server to the client session. 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 session 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
All Diffusion clients can retain a buffer of messages that they have sent to the Diffusion server . If 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 the Apple® , Java™ , Android™ and .NET APIs, you can configure the size of this buffer, in messages, when creating your session on the Diffusion server :
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.
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.