Using Demo đâ
We strongly recommend that you first run the framework-related example DEMO we prepared for you. This will not only give you an intuitive experience of OpenIMSDK's capabilities, but will also help you quickly locate and solve problems during the actual integration process.
Tips âī¸â
The
open-im-sdk-rn@3.5.1
has contains significant disruptive updates. If you need to upgrade, please check the incoming data and the returned data.Unlike other SDKS,
React Native SDK
operationID is not optional, but required.(operationID is used for back-end log query)
Integration Steps ( React Native CLI )â
1. Add Dependenciesâ
yarn add open-im-sdk-rn
2. Init SDKâ
import OpenIMSDKRN from 'open-im-sdk-rn';
import RNFS from 'react-native-fs';
RNFS.mkdir(RNFS.DocumentDirectoryPath + '/tmp');
OpenIMSDKRN.initSDK({
platformID: 2, // 1: ios, 2: android
apiAddr: 'http://your-server-ip:10002',
wsAddr: 'ws://your-server-ip:10001',
dataDir: RNFS.DocumentDirectoryPath + '/tmp',
logLevel: 5,
isLogStandardOutput: true,
}, 'opid');
3. Log in and set listeningâ
import OpenIMSDKRN, { OpenIMEmitter } from 'open-im-sdk-rn';
OpenIMSDKRN.login({
userID: 'IM user ID',
token: 'IM user token',
}, 'opid');
OpenIMEmitter.addListener('onConnecting', () => {
console.log('onConnecting');
});
OpenIMEmitter.addListener('onConnectSuccess', () => {
console.log('onConnectSuccess');
});
OpenIMEmitter.addListener('onConnectFailed', ({ errCode, errMsg }) => {
console.log('onConnectFailed', errCode, errMsg);
});
4. Send and receive messagesâ
import OpenIMSDKRN, { OpenIMEmitter } from 'open-im-sdk-rn';
import type { MessageItem } from 'open-im-sdk-rn';
OpenIMEmitter.addListener('onRecvNewMessages', (data: MessageItem[]) => {
console.log('onRecvNewMessages', data);
});
const message = await OpenIMSDKRN.createTextMessage('hello openim', 'opid');
OpenIMSDKRN.sendMessage({
recvID: 'recipient user ID',
groupID: '',
message,
}, 'opid')
.then(() => {
// Message sent successfully âī¸
})
.catch(err => {
// Failed to send message â
console.log(err);
});