Send your first message
Send and receive the first one-to-one text message in a React Native application.
Verify that realtime messaging is operational by sending and receiving a one-to-one text message once the SDK is initialized and authenticated. Before proceeding, complete the initialization and sign-in steps in Authenticate and manage a session, and prepare two authenticated user accounts (one sender and one recipient).
Create and send a text message
Invoke createTextMessage() on the sender client to generate a MessageItem, then call sendMessage() to dispatch it to the recipient. Replace user_b with the actual recipient's user ID. For a direct 1-on-1 message, specify recvID and pass an empty string for groupID.
const message = await OpenIMSDK.createTextMessage('Hello, OpenIMSDK');
await OpenIMSDK.sendMessage({
recvID: 'user_b',
groupID: '',
message,
});Receive the message
Register the onRecvNewMessage listener on the recipient client before the message is sent. When the server delivers the message, the callback receives the incoming MessageItem:
function handleNewMessage(message: MessageItem) {
console.log("onRecvNewMessage", message);
}
// Register listener
OpenIMSDK.on('onRecvNewMessage', handleNewMessage);For complete message lifecycle handling and receipt status, see Receive messages.
Was this page helpful?