Skip to content

openimsdk/open-im-sdk-reactnative

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

React Native Client SDK for OpenIM ๐Ÿ‘จโ€๐Ÿ’ป๐Ÿ’ฌ

Use this SDK to add instant messaging capabilities to your application. By connecting to a self-hosted OpenIM server, you can quickly integrate instant messaging capabilities into your app with just a few lines of code.

The iOS SDK core is implemented in OpenIM SDK Core. Using gomobile, it can be compiled into an XCFramework for iOS integration. iOS interacts with the OpenIM SDK Core through JSON, and the SDK exposes a re-encapsulated API for easy usage. In terms of data storage, iOS utilizes the SQLite layer provided internally by the OpenIM SDK Core.

The android SDK core is implemented in OpenIM SDK Core. Using gomobile, it can be compiled into an AAR file for Android integration. Android interacts with the OpenIM SDK Core through JSON, and the SDK exposes a re-encapsulated API for easy usage. In terms of data storage, Android utilizes the SQLite layer provided internally by the OpenIM SDK Core.

The React Native Client SDK uses the NativeModule system to expose instances of Java/Objective-C classes to JavaScript (JS) as JS objects, thereby allowing you to execute arbitrary native code from within JS.

Tips ๐Ÿ””

  1. Starting from v3.8.3-patch.10, the package name has been changed from open-im-sdk-rn to @openim/rn-client-sdk.

  2. operationID Parameter: This parameter is used for backend log querying. Starting from v3.8.3-patch.10.2, the operationID parameter is optional for all APIs (the SDK will auto-generate one if not provided). For earlier versions, this parameter is required and must be passed explicitly.

  3. Event Binding API: Starting from v3.8.3-patch.10.2, you can use OpenIMSDK.on() to listen for events with better TypeScript type hints. Earlier versions must use the OpenIMEmitter object. Both approaches remain compatible with the latest version.

  4. The v3.5.1 contains significant disruptive updates. If you need to upgrade, please check the incoming data and the returned data.

Documentation ๐Ÿ“š

Visit https://docs.openim.io for detailed documentation and guides.

For the SDK reference, see https://docs.openim.io/sdks/quickstart/reactNative.

Installation ๐Ÿ’ป

Adding Dependencies

yarn add @openim/rn-client-sdk

Usage ๐Ÿš€

The following examples demonstrate how to use the SDK. TypeScript is used, providing complete type hints.

Importing the SDK and init

import OpenIMSDK from '@openim/rn-client-sdk';
import RNFS from 'react-native-fs';

RNFS.mkdir(RNFS.DocumentDirectoryPath + '/tmp');

OpenIMSDK.initSDK({
  platformID: 2,  // 1: ios, 2: android
  apiAddr: 'http://your-server-ip:10002',
  wsAddr: 'ws://your-server-ip:10001',
  dataDir: RNFS.DocumentDirectoryPath + '/tmp',
  logFilePath: RNFS.DocumentDirectoryPath + '/tmp',
  logLevel: 5,
  isLogStandardOutput: true,
});

Logging In and Listening for Connection Status

import OpenIMSDK from '@openim/rn-client-sdk';

OpenIMSDK.login({
  userID: 'IM user ID',
  token: 'IM user token',
});

OpenIMSDK.on('onConnecting', () => {
  console.log('onConnecting');
});

OpenIMSDK.on('onConnectSuccess', () => {
  console.log('onConnectSuccess');
});

OpenIMSDK.on('onConnectFailed', ({ errCode, errMsg }) => {
  console.log('onConnectFailed', errCode, errMsg);
});

For versions prior to v3.8.3-patch.10.2:

import OpenIMSDKRN, { OpenIMEmitter } from '@openim/rn-client-sdk';

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);
});

To log into the IM server, you need to create an account and obtain a user ID and token. Refer to the access token documentation for details.

Receiving and Sending Messages ๐Ÿ’ฌ

OpenIM makes it easy to send and receive messages. By default, there is no restriction on having a friend relationship to send messages (although you can configure other policies on the server). If you know the user ID of the recipient, you can conveniently send a message to them.

import OpenIMSDK from '@openim/rn-client-sdk';
import type { MessageItem } from '@openim/rn-client-sdk';

OpenIMSDK.on('onRecvNewMessages', (messages: MessageItem[]) => {
  console.log('onRecvNewMessages', messages);
});

const message = await OpenIMSDK.createTextMessage('hello openim');

OpenIMSDK.sendMessage({
  recvID: 'recipient user ID',
  groupID: '',
  message,
})
  .then(() => {
    // Message sent successfully โœ‰๏ธ
  })
  .catch(err => {
    // Failed to send message โŒ
    console.log(err);
  });

For versions prior to v3.8.3-patch.10.2:

import OpenIMSDKRN, { OpenIMEmitter } from '@openim/rn-client-sdk';

OpenIMEmitter.addListener('onRecvNewMessages', (messages) => {
  console.log('onRecvNewMessages', messages);
});

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);
  });

Examples ๐ŸŒŸ

You can find a demo web app that use the SDK in the openim-reactnative-demo repository.

or:

Use the examples(only the simplest SDK calls) under this project to try it out:

yarn

yarn example android

yarn example ios

Note for iOS: When running the iOS example project, you may encounter dependency installation errors or build failures. Please refer to iOS Example Project Running Notes for detailed solutions.

Community ๐Ÿ‘ฅ

Community Meetings ๐Ÿ“†

We want anyone to get involved in our community and contributing code, we offer gifts and rewards, and we welcome you to join us every Thursday night.

Our conference is in the OpenIM Slack ๐ŸŽฏ, then you can search the Open-IM-Server pipeline to join

We take notes of each biweekly meeting in GitHub discussions, Our historical meeting notes, as well as replays of the meetings are available at Google Docs ๐Ÿ“‘.

Who are using OpenIM ๐Ÿ‘€

Check out our user case studies page for a list of the project users. Don't hesitate to leave a ๐Ÿ“comment and share your use case.

License ๐Ÿ“„

OpenIM is licensed under the Apache 2.0 license. See LICENSE for the full license text.