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
[email protected]has contains significant disruptive updates. If you need to upgrade, please check the incoming data and the returned data.Unlike other SDKS,
React Native SDKoperationID 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);
});