Status monitoring events

Use addEventListener to monitor the IM connection status. Handle different business logic or display prompts based on the connection state.

  • Set it during the app lifecycle.

  • Clear event listeners when the app is destroyed.

Event Description
Events.CONNECTING IM connecting
Events.CONNECTED IM connected
Events.DISCONNECT IM disconnected, handle reconnection in the business layer
Events.SUSPEND IM connection interrupted, SDK will attempt to reconnect

Sample code


const Events = RongIMLib.Events

/**

* Event status when connecting

*/

RongIMLib.addEventListener(Events.CONNECTING, () => {

console.log('Connecting...')

})

/**

* Event triggered when connected to the server

*/

RongIMLib.addEventListener(Events.CONNECTED, () => {

console.log('Connected successfully')

})

/**

* Event triggered when manually calling disconnect or when the user is kicked offline

*/

RongIMLib.addEventListener(Events.DISCONNECT, (code) => {

console.log('Connection interrupted, handle reconnection in the business layer ->', code)

})

/**

* Event triggered when there’s a connection issue, SDK will attempt to reconnect

*/

RongIMLib.addEventListener(Events.SUSPEND, (code) => {

console.log('Connection interrupted, SDK will attempt to reconnect, no action needed in the business layer')

// Starting from version 5.1.2, the event callback includes the interruption status code

console.log(`code -> ${code}`)

})

Note

Avoid calling the connect method in the Events.DISCONNECT callback to prevent mutual kick deadlock or connection deadlock.