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.
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.
-
Add the dependency to your project's
pubspec.yaml.yamldependencies:
flutter:
sdk: flutter
ai_nexconn_chat_plugin: x.y.z -
Run
flutter pub getin your project directory to download the plugin. -
Import the package.
Dartimport '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.
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.
NCEngine is a static class — all APIs are called directly on NCEngine without creating an instance.
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.
NCEngine.addMessageHandler('msg-handler', MessageHandler(
onMessageReceived: (event) {
// Handle received message
},
));
Connect to the server
- 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
userId1:
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"}
- 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.
NCEngine.addConnectionStatusHandler('conn-handler', (status) {
// Handle connection status change
});
- Call
NCEngine.connect()to connect the user to the server. The SDK has an automatic reconnection mechanism, so callconnectonly once per app lifecycle. See Connect.
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():
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
await NCEngine.disconnect();
Destroy the engine
await NCEngine.destroy();