SDK quickstart
Updated
A highly reliable global communication platform where users can chat one-to-one, in groups or in chat rooms.
Instant messaging enhances user engagement by enabling users to connect and form a community within the app. Increased engagement can lead to increased user satisfaction and loyalty to your app. An instant messaging feature can also provide real-time support to users, allowing them to get help and answers to their questions quickly. The Chat SDK enables you to embed real-time messaging in any app, on any device, anywhere.
This page guides you through implementing peer-to-peer messaging into your app using the Chat SDK.
Understand the tech
The following figure shows the workflow of sending and receiving peer-to-peer messages using Chat SDK.
Chat SDK workflow
- Clients retrieve an authentication token from your app server.
- Users log in to Chat using the App ID, their user ID, and token.
- Clients send and receive messages through Chat as follows:
- Client A sends a message to Client B. The message is sent to the Agora Chat server.
- The server delivers the message to Client B. When Client B receives a message, the SDK triggers an event.
- Client B listens for the event to read and display the message.
Prerequisites
In order to follow the procedure on this page, you must have:
-
A valid Agora account.
-
An Agora project for which you have enabled Chat.
-
The App ID for the project.
-
Internet access.
Ensure that no firewall is blocking your network communication.
-
Flutter 2.10 or higher.
-
Dart 2.16 or higher.
-
If your target platform is iOS:
- macOS
- Xcode 12.4 or higher with Xcode Command Line Tools
- CocoaPods
- An iOS emulator or a physical iOS device running iOS 10.0 or higher
-
If your target platform is Android:
- macOS or Windows
- Android Studio 4.0 or higher with JDK 1.8 or higher
- An Android emulator or a physical Android device running Android SDK API 21 or higher
Project setup
To integrate Chat into your app, do the following:
-
Set up a Flutter environment for a Chat project
In the terminal, run the following command:
flutter doctorFlutter checks your development device and helps you set up your local development environment. Make sure that your system passes all the checks.
-
Create a new Flutter app
In the IDE of your choice, create a new Flutter Application project.
You can also create a new project from the terminal using the following command:
flutter create <--insert project name--> -
Add Chat SDK to your project
Add the following line to the
<project_folder>/pubspec.yamlfile underdependencies:dependencies: ... # For x.y.z, fill in a specific SDK version number. For example, 1.0.9 agora_chat_sdk: ^x.y.z ...To get the latest device Chat SDK version number, visit pub.dev.
You can also add the Chat SDK dependency to your project by running the following command in the project folder:
flutter pub add agora_chat_sdk -
Use the Flutter framework to download dependencies to your app
Open a terminal window and execute the following command in the project folder:
flutter pub get -
Add platform specific requirements
Update the minimum version requirements for your target platforms as follows:
-
Android
-
In the
<project_folder>/android/app/build.gradlefile, set the AndroidminSdkVersionto21underdefaultConfig {.android { defaultConfig { minSdkVersion 21 } } -
In the
<project_folder>/android/app/proguard-rules.profile, add the following lines to prevent code obfuscation:-keep class com.hyphenate.** {*;} -dontwarn com.hyphenate.**
-
-
iOS
Open the
<project_folder>/ios/Runner.xcodeprojfile in Xcode, and select TARGETS > Runner in the left sidebar. In the General > Deployment Info section, set the minimum iOS version toiOS 10.0.
-
Implement peer-to-peer messaging
This section shows how to use the Chat SDK to implement peer-to-peer messaging in your app, step by step.
Handle the system logic
Import the Chat SDK package and set up the app framework.
-
Add the Chat SDK package
Open the file
/lib/main.dartand add the followingimportstatement at the top of the file:import 'package:agora_chat_sdk/agora_chat_sdk.dart'; -
Declare global variables
In
/lib/main.dartadd the following declaration after theimportstatements:// Global key to access the scaffold messenger final GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>(); -
Enable SnackBar messages
Update the root widget to add the
scaffoldMessengerKeyfor showing SnackBar messages, and set the background color. In/lib/main.dart, replace theMyAppclass with the following:class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', scaffoldMessengerKey: scaffoldMessengerKey, theme: ThemeData( scaffoldBackgroundColor: const Color(0xFFECE5DD), ), home: const MyHomePage(title: 'Agora Chat Quickstart'), ); } } -
Set up the app framework
To set up a Flutter app framework for Chat, you do the following:
- Create a stateful widget
- Declare variables to manage the connection and access UI elements
- Add a method to display information to the user
To do this, replace the
_MyHomePageStateclass with the following:class _MyHomePageState extends State<MyHomePage> { static const String appId = "<App ID from Agora console>"; static const String userId = "<User ID of the local user>"; String token = "<Your authentication token>"; late ChatClient agoraChatClient; bool isJoined = false; ScrollController scrollController = ScrollController(); TextEditingController messageBoxController = TextEditingController(); String messageContent = "", recipientId = ""; final List<Widget> messageList = []; showLog(String message) { scaffoldMessengerKey.currentState?.showSnackBar(SnackBar( content: Text(message), )); } }
Build the UI
In the quickstart app, you create a simple UI that consists of the following elements:
- An
ElevatedButtonto log in or out of Agora Chat - A
TextFieldto specify the recipient user ID - A
TextFieldto enter a text message - An
ElevatedButtonto send the text message - A
ListView.builderto display sent and received messages
To create this UI, add the following build method to the _MyHomePageState class:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
padding: const EdgeInsets.all(10),
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
Row(
children: [
Expanded(
child: SizedBox(
height: 40,
child: TextField(
decoration: const InputDecoration(
filled: true,
fillColor: Colors.white,
hintText: "Enter recipient's userId",
),
onChanged: (chatUserId) => recipientId = chatUserId,
),
),
),
const SizedBox(width: 10),
SizedBox(
width: 80,
height: 40,
child: ElevatedButton(
onPressed: joinLeave,
child: Text(isJoined ? "Leave" : "Join"),
),
),
],
),
const SizedBox(height: 10),
Expanded(
child: ListView.builder(
controller: scrollController,
itemBuilder: (_, index) {
return messageList[index];
},
itemCount: messageList.length,
),
),
Row(children: [
Expanded(
child: SizedBox(
height: 40,
child: TextField(
controller: messageBoxController,
decoration: const InputDecoration(
filled: true,
fillColor: Colors.white,
hintText: "Message",
),
onChanged: (msg) => messageContent = msg,
),
),
),
const SizedBox(width: 10),
SizedBox(
width: 50,
height: 40,
child: ElevatedButton(
onPressed: sendMessage,
child: const Text(">>"),
),
),
]),
],
),
),
);
}Send and receive messages
When a user opens the app, you instantiate and initialize a ChatClient. When the user taps the Join button, the app logs in to Agora Chat. When a user types a message in the text box and then presses Send, the typed message is sent to Agora Chat. When the app receives a message from the server, the message is displayed in the message list. This simple workflow enables you to rapidly build a Chat client with basic functionality.
The following figure shows the API call sequence for implementing this workflow.
API call sequence
To implement this workflow in your app, take the following steps:
-
Set up Chat when the app starts
When the app starts, you create an instance of the
ChatClientand set up callbacks to handle Chat events. To do this, add the followinginitStatemethod to the_MyHomePageStateclass:@override void initState() { super.initState(); setupChatClient(); setupListeners(); } -
Instantiate the
ChatClientTo implement peer-to-peer messaging, you use Chat SDK to initialize a
ChatClientinstance. In the_MyHomePageStateclass, add the following method afterinitState.void setupChatClient() async { ChatOptions options = ChatOptions.withAppId( appId, autoLogin: false, ); agoraChatClient = ChatClient.getInstance; await agoraChatClient.init(options); // Notify the SDK that the Ul is ready. After the following method is executed, callbacks within ChatRoomEventHandler and ChatGroupEventHandler can be triggered. await ChatClient.getlnstance.startCallback(); } -
Handle and respond to Chat events
To receive notification of Chat events such as connection, disconnection, and token expiration, you add a
ConnectionEventHandler. To handle message delivery and new message notifications, you add aChatEventHandler. When you receive theonMessageReceivednotification, you display the message to the user.In the
_MyHomePageStateclass, add the following methods aftersetupChatClient:void setupListeners() { agoraChatClient.addConnectionEventHandler( "CONNECTION_HANDLER", ConnectionEventHandler( onConnected: onConnected, onDisconnected: onDisconnected, onTokenWillExpire: onTokenWillExpire, onTokenDidExpire: onTokenDidExpire ), ); agoraChatClient.chatManager.addEventHandler( "MESSAGE_HANDLER", ChatEventHandler(onMessagesReceived: onMessagesReceived), ); } void onMessagesReceived(List<ChatMessage> messages) { for (var msg in messages) { if (msg.body.type == MessageType.TXT) { ChatTextMessageBody body = msg.body as ChatTextMessageBody; displayMessage(body.content, false); showLog("Message from ${msg.from}"); } else { String msgType = msg.body.type.name; showLog("Received $msgType message, from ${msg.from}"); } } } void onTokenWillExpire() { // The token is about to expire. Get a new token // from the token server and renew the token. } void onTokenDidExpire() { // The token has expired } void onDisconnected () { // Disconnected from the Chat server } void onConnected() { showLog("Connected"); } -
Log in to Agora Chat
When a user presses Join, your app logs in to Agora Chat. When a user presses Leave, the app logs out of Agora Chat.
To implement this logic, in the
_MyHomePageStateclass, add the following method beforeshowLog:void joinLeave() async { if (!isJoined) { // Log in try { await agoraChatClient.loginWithToken(userId, token); showLog("Logged in successfully as $userId"); setState(() { isJoined = true; }); } on ChatError catch (e) { if (e.code == 200) { // Already logged in setState(() { isJoined = true; }); } else { showLog("Login failed, code: ${e.code}, desc: ${e.description}"); } } } else { // Log out try { await agoraChatClient.logout(true); showLog("Logged out successfully"); setState(() { isJoined = false; }); } on ChatError catch (e) { showLog("Logout failed, code: ${e.code}, desc: ${e.description}"); } } } -
Send a message
To send a message to Agora Chat when a user presses the Send button, add the following method to the
_MyHomePageStateclass, afterjoinLeave:void sendMessage() async { if (recipientId.isEmpty || messageContent.isEmpty) { showLog("Enter recipient user ID and type a message"); return; } var msg = ChatMessage.createTxtSendMessage( targetId: recipientId, content: messageContent, ); ChatClient.getInstance.chatManager.addMessageEvent( "UNIQUE_HANDLER_ID", ChatMessageEvent( onSuccess: (msgId, msg) { _addLogToConsole("on message succeed"); }, onProgress: (msgId, progress) { _addLogToConsole("on message progress"); }, onError: (msgId, msg, error) { _addLogToConsole( "on message failed, code: ${error.code}, desc: ${error.description}", ); }, ), ); ChatClient.getInstance.chatManager.removeMessageEvent("UNIQUE_HANDLER_ID"); agoraChatClient.chatManager.sendMessage(msg); } -
Display chat messages
To display the messages the current user has sent and received in your app, add the following method to the
_MyHomePageStateclass:void displayMessage(String text, bool isSentMessage) { messageList.add(Row(children: [ Expanded( child: Align( alignment: isSentMessage ? Alignment.centerRight : Alignment.centerLeft, child: Container( padding: const EdgeInsets.all(10), margin: EdgeInsets.fromLTRB( (isSentMessage ? 50 : 0), 5, (isSentMessage ? 0 : 50), 5), decoration: BoxDecoration( color: isSentMessage ? const Color(0xFFDCF8C6) : const Color(0xFFFFFFFF), ), child: Text(text), ), ), ), ])); setState(() { scrollController.jumpTo(scrollController.position.maxScrollExtent + 50); }); } -
Remove event handlers on exit
When a user closes the app, you remove the message and connection event handlers. To do this, add the following
disposemethod to the_MyHomePageStateclass:@override void dispose() { agoraChatClient.chatManager.removeEventHandler("MESSAGE_HANDLER"); agoraChatClient.removeConnectionEventHandler("CONNECTION_HANDLER"); super.dispose(); }
Test your implementation
To ensure that you have implemented Peer-to-Peer Messaging in your app:
-
Create an app instance for the first user:
-
In the
_MyHomePageStateclass, updateappId,token, anduserIdwith values from Agora Console. To get your App ID, see Get Chat project information. -
Connect a physical test device to your development device.
-
In your IDE, click Run app or launch your app from the terminal using
flutter run lib/main.dart. A moment later you see the project installed on your device.
-
Create an app instance for the second user:
-
Register a second user in Agora Console and generate a user token.
-
In the
_MyHomePageStateclass, updateuserIdandtokenwith values for the second user. Make sure you use the sameappIdas for the first user. -
Run the modified app on a device emulator or a second physical test device.
-
-
On each device, click Join to log in to Agora Chat.
-
Edit the recipient name on each device to show the user ID of the user logged in to the other device.
-
Type a message in the Message box of either device and press
>>.The message is sent and appears on the other device.
-
Press Leave to log out of Agora Chat.
Reference
This section contains content that completes the information in this page, or points you to documentation that explains other aspects to this product.
- For more code samples, see Samples and demos.
Next steps
In a production environment, best practice is to deploy your own token server. Users retrieve a token from the token server to log in to Chat. To see how to implement a server that generates and serves tokens on request, see Secure authentication with tokens.
