Skip to main content

Monitor Connection Status

Feature Description​

Use addEventListener to monitor IM connection status, enabling different business processing or displaying prompts on the interface based on the connection state.

  • Recommended to set during the application lifecycle.

  • Event listeners should be cleared when the application is destroyed.

Connection Events​

EventDescription
Events.CONNECTINGIM connecting
Events.CONNECTEDIM connection established
Events.DISCONNECTIM connection lost - requires business layer reconnection handling
Events.SUSPENDIM connection interrupted - SDK will automatically attempt reconnection

Sample Code​

const Events = RongIMLib.Events
/**
* Connecting event status
*/
RongIMLib.addEventListener(Events.CONNECTING, () => {
console.log('Connecting...')
})

/**
* Triggered when successfully connected to server
*/
RongIMLib.addEventListener(Events.CONNECTED, () => {
console.log('Connection successful')
})

/**
* Triggered when manually calling disconnect or when user gets kicked offline
*/
RongIMLib.addEventListener(Events.DISCONNECT, (code) => {
console.log('Connection interrupted, requires business layer reconnection ->', code)
})

/**
* Triggered when connection issues occur - SDK handles reconnection automatically
*/
RongIMLib.addEventListener(Events.SUSPEND, (code) => {
console.log('Connection suspended, SDK will attempt reconnection (no business layer action required)')
// Starting from v5.1.2, event callback includes interruption code
console.log(`code -> ${code}`)
})
tip
  • Prior to v5.6.1, the code parameter in Events.DISCONNECT and Events.SUSPEND callbacks used ConnectionStatus type
  • From v5.7.0 onward, the code parameter in Events.DISCONNECT and Events.SUSPEND callbacks uses ErrorCode type