Sendbird Chat SDK for Flutter
Through Chat SDK for flutter, you can efficiently integrate real-time chat into your client app. On the client-side implementation, you can initialize, configure and build the chat with minimal effort. On the server-side, Sendbird ensures reliable infra-management services for your chat within the app. This readme provides essential information on the Chat SDK’s structure, supplementary features, and the installation steps.
It is simple to implement chat in your client app with Sendbird Chat SDK for Flutter: a user logs in, sees a list of channels, selects or creates an open channel or a group channel, and, through the use of the channel event handler, sends messages to the channel, while also receiving them from other users within the channel.
Find out more about Sendbird Chat for Flutter at Sendbird Docs. If you have any comments or questions regarding bugs and feature requests, visit Sendbird community.
This section shows you the prerequisites you need to check for using Sendbird Chat SDK for Flutter.
The minimum requirements for Chat SDK for Flutter are:
- Xcode or Android studio
- Dart 2.10.4 or above
- Flutter 1.22.0 or higher
This section gives you information you need to get started with Sendbird Chat SDK for Flutter. Follow the simple steps below to build the Chat SDK into your client app.
The fastest way to test Sendbird Chat SDK for Flutter is to build your chat app on top of our sample app. To create a project for the sample app, download the app from our GitHub repository. The link is down below.
A Sendbird application comprises everything required in a chat service including users, message, and channels. To create an application:
- Go to the Sendbird Dashboard and enter your email and password, and create a new account. You can also sign up with a Google account.
- When prompted by the setup wizard, enter your organization information to manage Sendbird applications.
- Lastly, when your dashboard home appears after completing setup, click Create + at the top-right corner.
Only one Sendbird application can be integrated per app for your service regardless of the platform. All users within your Sendbird application can communicate with each other across all platforms. This means that your iOS, Android, and web client app users can all send and receive messages with one another without any further setup.
Note: All data is limited to the scope of a single application, and users in different Sendbird applications can't chat with one other.
Installing the Chat SDK is a simple process if you’re familiar with using external packages or SDK’s in your projects. Follow the steps below via pub
.
- Add following dependency in
pubspec.yaml
.
dependencies:
sendbird_sdk: ^3.0.11
- Run
flutter pub get
command in your project directory.
You can use all classes and methods just with the one import statement as shown below.
import 'package:sendbird_sdk/sendbird_sdk.dart'
Follow the step-by-step instructions below to authenticate and send your first message.
In order to use the features of Sendbird Chat SDK for Flutter, you should initiate the Sendbirdsdk
instance through user authentication with Sendbird server. This instance communicates and interacts with the server based on an authenticated user account, allowing the client app to use the Chat SDK's features.
Here are the steps to sending your first message using the Chat SDK:
Initializing the Chat SDK allows it to respond to the connection and state changes in your client app. Pass the APP_ID
of your Sendbird application as an argument to the appId
parameter in the constructor of SendbirdSdk
. The constructor of SendbirdSdk
creates an instance, thus should be called a single time across your client app. It is recommended that the code for initialization be implemented in the user login view.
final sendbird = SendbirdSdk(appId: APP_ID);
A user can log in and connect a user to Sendbird server by using a unique user ID or with a user ID and an access token.
To connect to Sendbird server, a user is required to log in with a unique ID. A new user can authenticate with any untaken user ID, which gets automatically registered to the Sendbird system. An existing ID can log in directly. The ID must be unique within a Sendbird application to be distinguished from others, such as a hashed email address or phone number in your service.
try {
final user = await sendbird.connect(userId: USER_ID);
} catch (e) {
// error
}
By using Chat Platform API, you can create a user and issue a unique access token to each user, or issue an access token for an existing user. Once an access token is issued, a user is required to provide the access token to log in to your Sendbird application.
- Using the Chat Platform API, create a Sendbird user account with the information submitted when a user signs up or signs in to your service.
- Save the user ID along with the issued access token to your securely managed persistent storage.
- When a user attempts to log in to the application, load the user ID and access token from the storage, and then pass them to
connect()
method. - Periodically replacing the user's access token is recommended for account security.
try {
final user = await sendbird.connect(userId: USER_ID, accessToken: ACCESS_TOKEN);
} catch (e) {
// error
}
To manage user access to your Sendbird application, go to Settings > Application > Security > Access token permission setting on your Sendbird dashboard. You can change settings to prevent the users without an access token from logging in to your application or restrict their access to read and write messages.
For security reasons, you can also use a session token when a user logs in to Sendbird server instead of an access token. Learn more about Access token vs. Session token from the Chat Platform API guide.
Create an open channel. Once created, all users in your Sendbird application can easily participate in the channel.
try {
final channel = await OpenChannel.createChannel();
} catch (e) {
// error
}
You can also create a group channel by inviting users as new members to the channel.
Note: The majority of the methods used in the following steps are all asynchronous. This means, with asynchronous methods, your client app must receive a result via
await
orthen()
callbacks from Sendbird server through completion handlers before moving on to the next step.
Enter the channel to send and receive messages.
try {
final channel = await OpenChannel.getChannel(CHANNEL_URL);
await channel.enter();
} catch (e) {
// error
}
Finally, send a message to the channel. There are three types of messages: a user message in a plain text, a file message in a binary file, such as an image or PDF, and an admin message in a plain text sent through the dashboard or Chat Platform API.
try {
final params = UserMessageParams()
..message = MESSAGE
..data = DATA
..customType = CUSTOM_TYPE;
final preMessage = openChannel.sendUserMessage(params, onCompleted:(message, error){
if (error != null) {
// error
} else {
// message is sent successfully
}
});
// use preMessage to populate your chat messages early
} catch (e) {
// error
}