Optimising Ably connections to minimize battery usage

By default, Ably sends a heartbeat to connected clients every 15 seconds. (Through websocket ping frames if supported, else an app-level protocol message on platforms where websocket pings can't be detected clientside, or where xhr streaming or long polling is being used).

 

This is intended to strike a reasonable balance between battery usage for clients and detecting dropped or unstable connections.

 

In some client libraries (ably-js and ably-java) you can customise this interval if you want to tweak this balance. This is done by setting the `heartbeatInterval` value (in milliseconds) in the `transportParams` client option, which functions as a request to Ably to send heartbeats at that interval. For example, in ably-js, to have a heartbeat every 30s:

 

var realtime = new Ably.Realtime({ key: <key>, transportParams: { heartbeatInterval: 30000 }});

 

You can set it anything between 5000 (5 seconds) and 1800000 (half an hour).

 

Using longer values can extend the time that it takes both for the server to detect that the connection has dropped, as well as for the client to detect that the connection has dropped, in cases where e.g. the internet connection dies unexpectedly and the tcp connection is not properly terminated. You may want to experiment to find a good balance between connection liveness sensitivity and battery life. So while we allow up to half an hour so that you can experiment freely, in practice we would recommend against setting it to anywhere near that high. (Also note that high values can cause your 'peak connections' usage in your package to by higher than expected, if the server can't detect improperly terminated dropped connections in a reasonable time).