Skip to main content

Integration steps

1. Add Dependency

npm install @openim/client-sdk --save

2. Import SDK

import { getSDK } from '@openim/client-sdk';

const IMSDK = getSDK();

3. Login

import { getSDK, CbEvents, CallbackEvent } from '@openim/client-sdk';

const IMSDK = getSDK();

IMSDK.on(CbEvents.OnConnecting, handleConnecting);
IMSDK.on(CbEvents.OnConnectFailed, handleConnectFailed);
IMSDK.on(CbEvents.OnConnectSuccess, handleConnectSuccess);

IMSDK.login({
userID: 'IM user ID',
token: 'IM user token',
platformID: 5,
wsAddr: 'ws://your-server-ip:10001',
apiAddr: 'http://your-server-ip:10002',
});

function handleConnecting() {
// Connecting...
}

function handleConnectFailed({ errCode, errMsg }: CallbackEvent) {
// Connection failed ❌
console.log(errCode, errMsg);
}

function handleConnectSuccess() {
// Connection successful ✅
}

4. Sending and receiving messages

import { CbEvents, CallbackEvent, MessageItem } from '@openim/client-sdk';

// Listenfor new messages 📩
IMSDK.on(CbEvents.OnRecvNewMessages, handleNewMessages);

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

IMSDK.sendMessage({
recvID: 'recipient user ID',
groupID: '',
message,
})
.then(() => {
// Message sent successfully ✉️
})
.catch(err => {
// Failed to send message ❌
console.log(err);
});

function handleNewMessages({ data }: CallbackEvent<MessageItem[]>) {
// New message list 📨
console.log(data);
}