You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+35Lines changed: 35 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,8 @@ What else would you want? Head over to the example app [ViewController.swift](ht
17
17
*[Installation](#installation)
18
18
*[Configuration](#configuration)
19
19
*[Connection](#connection)
20
+
*[Connection state changes](#connection-state-changes)
21
+
*[Reconnection](#reconnection)
20
22
*[Subscribing to channels](#subscribing)
21
23
*[Binding to events](#binding-to-events)
22
24
*[Globally](#global-events)
@@ -204,6 +206,39 @@ class ViewController: UIViewController, ConnectionStateChangeDelegate {
204
206
}
205
207
```
206
208
209
+
The different states that the connection can be in are:
210
+
211
+
*`Connecting` - the connection is about to attempt to be made
212
+
*`Connected` - the connection has been successfully made
213
+
*`Disconnecting` - the connection has been instructed to disconnect and it is just about to do so
214
+
*`Disconnected` - the connection has disconnected and no attempt will be made to reconnect automatically
215
+
*`Reconnecting` - an attempt is going to be made to try and re-establish the connection
216
+
*`ReconnectingWhenNetworkBecomesReachable` - when the network becomes reachable an attempt will be made to reconnect
217
+
218
+
219
+
### Reconnection
220
+
221
+
There are three main ways in which a disconnection can occur:
222
+
223
+
* The client explicitly calls disconnect and a close frame is sent over the websocket connection
224
+
* The client experiences some form of network degradation which leads to a heartbeat (ping/pong) message being missed and thus the client disconnects
225
+
* The Pusher server closes the websocket connection; typically this will only occur during a restart of the Pusher socket servers and an almost immediate reconnection should occur
226
+
227
+
In the case of the first type of disconnection the library will (as you'd hope) not attempt a reconnection.
228
+
229
+
If there is network degradation that leads to a disconnection then the library has the [Reachability](https://github.com/ashleymills/Reachability.swift) library embedded and will be able to automatically determine when to attempt a reconnect based on the changing network conditions.
230
+
231
+
If the Pusher servers close the websocket then the library will attempt to reconnect (by default) a maximum of 6 times, with an exponential backoff. The value of `reconnectAttemptsMax` is a public property on the `PusherConnection` and so can be changed if you wish.
232
+
233
+
All of this is the case if you have the client option of `autoReconnect` set as `true`, which it is by default. If the reconnection strategies are not suitable for your use case then you can set `autoReconnect` to `false` and implement your own reconnection strategy based on the connection state changes.
234
+
235
+
There are a couple of properties on the connection (`PusherConnection`) that you can set that affect how the reconnection behaviour works. These are:
236
+
237
+
*`public var reconnectAttemptsMax: Int? = 6` - if you set this to `nil` then there is no maximum number of reconnect attempts and so attempts will continue to be made with an exponential backoff (based on number of attempts), otherwise only as many attempts as this property's value will be made before the connection's state moves to `.Disconnected`
238
+
*`public var maxReconnectGapInSeconds: Double? = nil` - if you want to set a maximum length of time (in seconds) between reconnect attempts then set this property appropriately
239
+
240
+
Note that the number of reconnect attempts gets reset to 0 as soon as a successful connection is made.
self.debugLogger?("[PUSHER DEBUG] Waiting \(timeInterval) seconds before attempting to reconnect (attempt \(reconnectAttempts +1) of \(reconnectAttemptsMax!))")
0 commit comments