Skip to main content

Connect

Establish a connection to the Nexconn chat server to start using messaging features.

Prerequisites

  • The SDK is initialized. See Initialize.
  • You have obtained a valid access token for the user. See the Server API documentation for User Registration.

Get an access token

The client SDK does not provide an API to generate tokens. Your app server must call the Server API to register a user and obtain an access token.

The access token is a unique credential for a user. The same user ID can request multiple tokens, and all valid tokens can be used to connect.

Connect to the server

Call NCEngine.connect with the user's access token. You only need to call connect once per app lifecycle — the SDK handles reconnection automatically.

TypeScript
NCEngine.connect({ token: '<Your-Token>' }).then((res) => {
if (res.isOk) {
console.log('Connected. User ID:', res.data.userId);
} else {
console.warn('Connection failed. Code:', res.code, res.msg);
}
});

Parameters

ParameterTypeRequiredDescription
tokenstringYesThe user's access token obtained from the Server API

Return value

NCEngine.connect() returns Promise<NCResult<ConnectResult>>.

FieldTypeDescription
isOkbooleanWhether the connection succeeded. Equivalent to code === 0.
codenumberStatus code. 0 means success.
dataConnectResult | undefinedReturned data when the connection succeeds.

ConnectResult

FieldTypeDescription
userIdstringThe connected user's ID
warning

All business APIs (channels, messages, etc.) require a successful connection. Call them only after connect resolves with isOk === true.

Connection best practices

  • Call connect only once. The SDK's built-in reconnection mechanism handles disconnections automatically.
  • Register event listeners before calling connect to avoid missing events during connection setup.
  • Store the access token securely on the client side. Avoid exposing it in logs or URLs.