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:
^3.7.2 - Flutter: Use a Flutter SDK version compatible with Dart
3.7.2.
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: ^26.2.0 -
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';
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. The app client should obtain an access token from your app server, which calls the Register User Server API. For this quickstart, use a token for
userId1returned by your app server:
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', (event) {
// Handle connection status change
print(event.status);
});
- 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.
NCEngine.connect() uses OperationHandler<String>, but this callback does not report success as error == null. In the current Flutter wrapper implementation, success returns userId together with NCError(code: 0), so use error?.isSuccess == true.
await NCEngine.connect(
ConnectParams(token: '<your-token>'),
(userId, error) {
if (error?.isSuccess == true) {
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();