Skip to main content

Send your first message

This tutorial walks you through the basic integration flow for the Nexconn Chat SDK on Flutter — from importing the SDK and initializing the engine to establishing a connection and exchanging messages.

Prerequisites

  • Register a developer account. After registration, the console automatically creates your first app and generates an App Key for the Development environment using the Singapore data center.
  • Get the App Key for the development environment. If you don't want to use the default app, create a new one and get its App Key and App Secret.
tip

Each app has two separate App Keys, one for the Development environment and one for the Production environment. Data is isolated between environments. Before your app goes live, switch to the Production App Key for final testing and release.

Requirements

  • Dart: ≥ 2.15.0 < 4.0.0
  • Flutter: ≥ 2.5.0

Demo project

The Chat SDK for Flutter provides a demo project without any chat UI. It shows how to call each API.

https://github.com/rongcloud/im-flutter-wrapper

Import the SDK

The Chat SDK for Flutter is distributed as a plugin package that includes both the Dart layer and native Android and iOS platform code.

  1. Add the dependency to your project's pubspec.yaml.

    yaml
    dependencies:
    flutter:
    sdk: flutter

    ai_nexconn_chat_plugin: x.y.z
  2. Run flutter pub get in your project directory to download the plugin.

  3. Import the package.

    Dart
    import 'package:ai_nexconn_chat_plugin/ai_nexconn_chat_plugin.dart';

Disable Android code obfuscation

For ProGuard rules, see the Android documentation.

If you don't need obfuscation, disable it. Otherwise, flutter build apk may obfuscate Android code and cause .so library errors.

Groovy
android {
buildTypes {
release {
// Enables code shrinking, obfuscation, and optimization for only
// your project's release build type.
minifyEnabled false

// Enables resource shrinking, which is performed by the
// Android Gradle plugin.
shrinkResources false
}
}
...
}

Initialize the Chat SDK

Call NCEngine.initialize() before you connect to the server. Get your App Key from the console and configure InitParams. See Initialize the SDK for details.

InitParams encapsulates areaCode (data center region code), naviServer (navigation server address), fileServer (file server address), statisticServer (statistics server address), and enablePush (push toggle). See Engine configuration for details.

info

NCEngine is a static class — all APIs are called directly on NCEngine without creating an instance.

Dart
await NCEngine.initialize(InitParams(
appKey: '<your-app-key>',
));

Listen for messages

Register a MessageHandler to receive real-time and missed messages.

Callback parameters:

  • message — The received message object.
  • left — The remaining message count in the current package. After the client connects, the server delivers compensation messages in packages of up to 200. The client parses and delivers them one by one.
  • offline — Whether the message is a missed message.
  • hasPackage — Whether undelivered message packages remain on the server.
Dart
NCEngine.addMessageHandler('msg-handler', MessageHandler(
onMessageReceived: (event) {
// Handle received message
},
));

Connect to the server

  1. Register a user to simulate messaging. In production, the app client should obtain an access token from your app server, which calls the Register User Server API. For quick testing, use the Get Token API on the console's API debugging page to get a token for userId 1:
HTTP
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
{"code":200,"userId":"1","token":"gxld6GHx3t1eDxof1qtxxYrQcjkbhl1V@sgyu.cn.example.com;sgyu.cn.example.com"}
  1. Set a connection status handler to track the connection state in real time. You can show connection status in the UI, such as "Network disconnected" or "Reconnected." Register the handler for your app lifecycle and remove it when you no longer need it. See Connection status handler.
Dart
NCEngine.addConnectionStatusHandler('conn-handler', (status) {
// Handle connection status change
});
  1. Call NCEngine.connect() to connect the user to the server. The SDK has an automatic reconnection mechanism, so call connect only once per app lifecycle. See Connect.
Dart
await NCEngine.connect(
ConnectParams(token: '<your-token>'),
(userId, error) {
if (error == null) {
print('Connected as: $userId');
}
},
);

Send a message

Create a channel instance and call channel.sendMessage():

Dart
final channel = DirectChannel('<target-user-id>');
await channel.sendMessage(
SendMessageParams(
messageParams: TextMessageParams(
text: 'Hello!',
),
),
callback: SendMessageCallback(
onMessageSaved: (message) {
print('Message saved locally');
},
onMessageSent: (code, message) {
print('Message sent, code: $code');
},
),
);

Log out

Dart
await NCEngine.disconnect();

Destroy the engine

Dart
await NCEngine.destroy();