Home
/
Chat
/
Flutter

Send your first message

The Chat SDK simplifies chat features into an effortless and straightforward process. To send your first message, do the following steps:


Step 1: Initialize the Chat SDK

To allow the Chat SDK to respond to the connection and state changes in your Flutter client app, you first initialize the Chat SDK in the app. Pass in the App ID of the Sendbird application you created earlier in the Sendbird Dashboard for the initialization.

Note: The constructor of a SendbirdSdk instance must be called once across your Flutter client app. We recommend that you initialize the Chat SDK in the main.dart and use its variable across your application.

Light Color Skin
Copy
// Initialize SendbirdSdk instance to use APIs in your app.

final sendbird = SendbirdSdk(appId: APP_ID);

Step 2: Connect to Sendbird server

Connect a user to Sendbird server using their unique user ID. Any untaken user ID is automatically registered as a new user to your Sendbird application before being connected, while an existing ID is allowed to log in directly.

Note: Go to the Authentication page to learn more about authenticating with an access token.

Light Color Skin
Copy
// The USER_ID below should be unique to your Sendbird application.

try {
    final user = await sendbird.connect(userId: USER_ID);
    // The user is connected to Sendbird server.
} catch (e) {
    // Handle error.
}

Step 3: Create a new open channel

Create an open channel. Once created, all users in your Sendbird application can easily participate in the channel.

Note: In a similar fashion, you can create a group channel by inviting users as new members to the channel.

Light Color Skin
Copy
try {
    final openChannel = await OpenChannel.createChannel();
    // An open channel is successfully created.
    // you can get the open channel's data from the result object.
    ...
} catch (e) {
    // handle error.
}

Step 4: Enter the channel

Enter the channel to send and receive messages.

Light Color Skin
Copy
// The CHANNEL_URL below can be retrieved using the openChannel.channelUrl.

try {
    final openChannel = await OpenChannel.getChannel(CHANNEL_URL);
    // Call the instance method of the result object in the "openChannel" parameter of the callback method.
    await openChannel.enter();
    // The current user successfully enters the open channel as a participant,
    // and can chat with other users in the channel by using APIs.
} catch (e) {
    // Handle error.
}

Step 5: Send a message to the channel

Finally, send a message to the channel. There are three types: a user message is a plain text, a file message is a binary file, such as an image or PDF, and an admin message is a plain text sent through the Sendbird Dashboard or Platform API.

Light Color Skin
Copy
try {
    final params = UserMessageParams()
        ..message = MESSAGE
        ..data = DATA
        ..customType = CUSTOM_TYPE;

    final preMessage = openChannel.sendUserMessage(params, onCompleted: (msg, error) {
        // The message is successfully sent to the channel.
        // The current user can receive messages from other users through the onMessageReceived() method of an event handler.
    });
} catch (e) {
    // Handle error.
}