SDK quickstart
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 for Windows.
Understand the tech
The following figure shows the workflow of sending and receiving peer-to-peer messages using Chat SDK.
- Clients retrieve an authentication token from your app server.
- Users log in to Chat using the app key, 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 Chat.
- 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 Key for the project.
-
Internet access.
Ensure that no firewall is blocking your network communication.
- A device running Windows 7 or higher.
- Visual Studio IDE 2019 or later
- .Net Framework 4.5.2 or later
Project setup
This section shows how to integrate Chat SDK into your project.
-
Create a Windows project
- Open Visual Studio and Create a C# WPF app.
- Set the Active solution configuration as Debug and Active solution platform as x64.
-
Integrate the Chat SDK
-
Download the Chat SDK NuGet package.
-
In Solution Explorer, right-click
<project-name>
and select Manage NuGet Packages.... -
On the
NuGet: <project-name>
page, select the Gear icon at the top right. -
In Options,
- Click + at the top right, then select the new package source.
- Set Name, then set Source to the parent directory of the Nuget package resides.
- Click OK.
-
Go back to the
NuGet: <project-name>
page, open the Package source drop-down list at the top right, then select the package you just added. -
In Browse on the
NuGet: <project-name>
page, select agora_chat_sdk, then click Install.If you don't see agora_chat_sdk, refresh the page.
-
In Preview Changes, click OK
-
You have installed Chat SDK.
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.
Create the UI
In your project, create a simple UI that consists of the following elements:
- A
Button
to Join or Leave Agora Chat. - A
TextBox
box to specify the recipient user ID. - A
TextBox
box to enter a text message. - A
Button
to send the text message. - A scrollable layout to display sent and received messages.
To add the UI framework to your Windows project, In Solution Explorer, open <project-name>
> MainWindow. xaml
, then replace the file contents with the following:
Handle the system logic
This section shows you how to add Chat SDK headers and setup logging. To do this:
-
Import the Agora namespaces
In the Solution Explorer, select
<project-name>
>MainWindow.xaml
, and double-click it to open theMainWindow.xaml.cs
file. Add the following lines to the namespaces list in the file: -
Log events and show status updates to your users
In the
MainWindow
class, add the following helper method:
Send and receive messages
When a user opens the app, you instantiate and initialize a SDKClient
. When the user clicks 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 another registered user. When the app receives a message from Agora Chat, 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.
To implement this workflow in your app, take the following steps:
-
Declare connection variables
In the
MainWindow
class, add the following declarations: -
Set up Chat when the app starts
When the app starts, you create an instance of
SDKClient
and set up delegates to handle connection and Chat events. To do this, replace theMainWindow()
constructor in theMainWindow
class with the following: -
Initialize the
SDKClient
To implement peer-to-peer messaging, you use Chat SDK to initialize a
SDKClient
instance. In theMainWindow
class, add the following method: -
Log in to Agora Chat
When a user clicks Join, your app logs in to Agora Chat. When they click Leave, the app logs out. To implement this logic, add the following method after
showLog()
in theMainWindow
class: -
Send a message
When a user types a message and presses the Send button, the app sends the message to the recipient. To do this, add the following method to the
MainWindow
class, afterJoinLeave_Click()
: -
Set up callbacks for handling Chat events
To set up callbacks for handling connection and Chat events, you implement the
IConnectionDelegate
andIChatManagerDelegate
interfaces in theMainWindow
class. Update theMainWindow
class definition as follows: -
Handle connection events
To implement all
IConnectionDelegate
callbacks, add the following code to theMainWindow
class: -
Handle Chat events
To read and display received chat messages, you add code to the
OnMessagesReceived
callback of theIChatManagerDelegate
. To implement allIChatManagerDelegate
callbacks, add the following code to theMainWindow
class: -
Display chat messages
To display sent and received messages, add the following method to the
MainWindow
class: -
Clean up when the window is closed
Add the following method to the
MainWindow
class:
Test your implementation
To ensure that you have implemented Peer-to-Peer Messaging in your app:
-
Create an app instance for the first user:
-
Register a user in Agora Console and Generate a user token.
-
In the
MainWindow
class, updateuserId
,token
, andappKey
with values from Agora Console. To get your app key, see Get Chat project information. -
Click the Start button. You see the following interface:
-
-
Create an app instance for the second user:
-
Register a second user in Agora Console and generate a user token.
-
Build a second instance of your app after updating
userId
andtoken
with values for the second user. Make sure you use the sameappKey
as for the first user. -
Click the Start button to launch the second instance.
-
-
On each instance, click Join to log in to Agora Chat.
-
Edit the recipient name in each app instance to show the user ID of the user logged in to the other instance.
-
Type a message in the Message box of either instance and press
>>
. The message is sent and appears on the other app. -
Press Leave to log out from 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.
- Avoid blocked callbacks
Each callback of a Chat client instance is triggered from internal threads. To avoid blocking internal threads, Agora recommends that you execute your operations in other threads when callbacks are triggered.
API reference
- AgoraChat.SDKClient Class
- SDKClient.InitWithOptions()
- SDKClient.LoginWithAgoraToken()
- SDKClient.Logout()
- AgoraChat.Message Class
- CreateTextSendMessage()
- AgoraChat.ChatManager Class
- ChatManager.SendMessage()
- AddChatManagerDelegate()
- RemoveChatManagerDelegate()
- AddConnectionDelegate()
- DeleteConnectionDelegate()
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.