Send your first message
Verify the single-chat and group-chat message sending flow in a Flutter application.
Install and initialize the OpenIM Flutter SDK, authenticate a user, and send your first text message in a single-chat or group-chat conversation. Before proceeding, complete the setup steps outlined in Before you start.
For single-chat conversations, supply the recipient's userID. For group-chat conversations, supply the target groupID. Never specify both destinations simultaneously.
Step 1: Install the SDK
Add the dependency to your Flutter project:
flutter pub add flutter_openim_sdkThis command selects the latest compatible release from pub.dev and updates pubspec.yaml alongside your lockfile. Commit the lockfile to source control, and verify SDK initialization, login, and message delivery whenever you upgrade dependencies.
Step 2: Initialize the SDK
Follow Environment-specific implementation to resolve your persistent app data directory and initialize the client. Provide an OnConnectListener to confirm network readiness before issuing messaging requests.
import 'package:flutter_openim_sdk/flutter_openim_sdk.dart';
final initialized = await OpenIM.iMManager.initSDK(
platformID: platformID,
apiAddr: session.apiAddr,
wsAddr: session.wsAddr,
dataDir: dataDir,
listener: connectListener,
);Step 3: Log in
Authenticate the user session:
await OpenIM.iMManager.login(
userID: session.userID,
token: session.token,
);HTTP login completion and WebSocket connection readiness are two distinct stages. Await the OnConnectListener.onConnectSuccess callback before sending messages.
Step 4: Prepare the destination
For a single-chat conversation, specify an existing recipient's user ID:
const targetUserID = 'user_b';For a group chat, specify a group ID in which the current user is a valid member permitted to send messages:
const targetGroupID = 'group_123';Step 5: Create and send a text message
Create a local Message object first, then pass it to sendMessage(). Creating a message does not transmit data over the network or trigger events on the recipient's client.
final message = await OpenIM.iMManager.messageManager.createTextMessage(
text: 'Hello, OpenIM',
);
final sentMessage = await OpenIM.iMManager.messageManager.sendMessage(
message: message,
userID: targetUserID,
offlinePushInfo: OfflinePushInfo(
title: 'New message',
desc: 'You received a new message',
),
);
appendOutgoingMessage(sentMessage);When sending to a group chat, pass groupID instead of userID:
final sentMessage = await OpenIM.iMManager.messageManager.sendMessage(
message: message,
groupID: targetGroupID,
offlinePushInfo: OfflinePushInfo(
title: 'New group message',
desc: 'A new message was posted in the group',
),
);Once the Future completes, the sender app can append the returned Message to the UI using clientMsgID as the deduplication and merge key. The recipient receives the incoming message through message listeners (see Receive messages for implementation details). Sender dispatch success and recipient delivery represent two distinct asynchronous phases; verify both during testing.
Next steps
Was this page helpful?