autoEstablishSocketConnection()
method of AppSettings builder class to true
. If you do not set this, the SDK will automatically apply the auto mode as the default behaviour for the WebSocket connection.
App State | Behaviour |
---|---|
App in foreground | Connected with WebSocket |
App in background | Immediately disconnected with WebSocket |
autoEstablishSocketConnection()
method to false
and then call the CometChat.connect()
method to establish the connection and the CometChat.disconnect()
method to disconnect the connection.
By default, if manual mode is activated, the SDK will disconnect the WebSocket connection after 30 seconds if the app goes into the background. This means that the WebSocket connection will remain alive for 30 seconds after the app goes into the background, but it will be disconnected after that time if no pings are received.
To keep the WebSocket connection alive even if your app goes in the background, you need to call the CometChat.ping()
method from your app within 30 seconds. This method sends a ping message to the CometChat server, which tells the server that the app is still active.
If you do not call the CometChat.ping()
method within 30 seconds, the SDK will disconnect the WebSocket connection. This means that you will lose any messages that are sent to your app while it is in the background.
App State | Behaviour |
---|---|
App in foreground | Call CometChat.connect() to create the WebSocket connection |
App in background | Disconnect the WebSocket connection if no ping is received within 30 seconds after the app goes in the background. |
autoEstablishSocketConnection()
method provided by the AppSettingsBuilder
class. This method takes a boolean value as an input. If set to true
, the SDK will manage the web-socket connection internally based on the default behaviour mentioned above. If set to false
, the web socket connection can will not be managed by the SDK and you will have to handle it manually. You can refer to the below code snippet for the same:
connect()
, disconnect()
and ping()
methods provided by the SDK.
connect()
method provided by the CometChat
class to establish the connection to the web-socket server. Please make sure that the user is logged in to the SDK before calling this method. You can use the CometChat.getLoggedInUser() method to check this. Once the connection is established, you will start receiving all the real-time events for the logged in user
disconnect()
method provided by the CometChat
class to break the established connection. Once the connection is broken, you will stop receiving all the real-time events for the logged in user.
CometChat.ping()
method in a loop. This will ensure that the ping message is sent to the server every 30 seconds, even if the app is not in the foreground.CometChat.ping()
method within 30 seconds of your app entering the background or after the previous ping() call.
CometChat.disconnect()
, then you will need to call the CometChat.connect()
method to create a new WebSocket connection.