Browse SDKs · React Native
SDKsReact Native

Send your first message

Send and receive the first one-to-one text message in a React Native application.

Copy

This page verifies the OpenIM React Native SDK with one one-to-one text message. Before starting, complete Authenticate and manage a session and prepare two user accounts: one sender and one receiver. Both accounts must be signed in.

Create and send a text message

The sender calls createTextMessage() to create a MessageItem, then calls sendMessage() to send it to the receiver. Replace user_b in the example with the receiver's actual user ID. For a one-to-one message, fill in 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

The receiver should set the onRecvNewMessage listener before the sender sends the message. After the sender sends it, the listener receives the corresponding MessageItem:

function handleNewMessage(message: MessageItem) {
  console.log("onRecvNewMessage", message)
}

// Set listener
OpenIMSDK.on('onRecvNewMessage', handleNewMessage);

For complete message event and status handling, see Receive messages.