đ Using the Demoâ
We strongly recommend running our framework-specific DEMO (feat-electron-ffi branch) first. This will not only give you a hands-on experience of IMSDK's features, but also help you quickly identify and resolve issues during actual integration.
Quick Integrationâ
1. Add Dependenciesâ
npm install @openim/wasm-client-sdk @openim/electron-client-sdk --save
2. Obtain WASM Static Resourcesâ
In the
node_modulesdirectory under your project root, find the@openim/wasm-client-sdksubdirectory and copy all files from theassetsfolder to your project's public resource directory (public).
- File list
openIM.wasm
sql-wasm.wasm
wasm_exec.js
-
After copying, include
wasm_exec.jsvia a script tag in yourindex.htmlfile -
Potential issues
If using Webpack 4, you may need to refer to this issue for configuration: How to import @openim/wasm-client-sdk in webpack4.x
3. Import SDKâ
- Main process
import OpenIMSDKMain from '@openim/electron-client-sdk';
...
new OpenIMSDKMain(libPath, mainWindow.webContents);
...
- Preload script
import '@openim/electron-client-sdk/lib/preload';
...
- Renderer process
import { getWithRenderProcess } from '@openim/electron-client-sdk/lib/render';
const { instance } = getWithRenderProcess();
export const IMSDK = instance;
4. Login & Set Listenersâ
Note: SDK initialization differs between Electron and Web environments. If you need to support both, handle them separately.
import { CbEvents, LogLevel } from '@openim/wasm-client-sdk';
import type { WSEvent } from '@openim/wasm-client-sdk/lib/types/entity';
IMSDK.on(CbEvents.OnConnecting, handleConnecting);
IMSDK.on(CbEvents.OnConnectFailed, handleConnectFailed);
IMSDK.on(CbEvents.OnConnectSuccess, handleConnectSuccess);
// Electron
await IMSDK.initSDK({
platformID: 'your-platform-id',
apiAddr: 'http://your-server-ip:10002',
wsAddr: 'ws://your-server-ip:10001',
dataDir: 'your-db-dir',
logFilePath: 'your-log-file-path',
logLevel: LogLevel.Debug,
isLogStandardOutput: true,
});
await IMSDK.login({
userID: 'your-user-id',
token: 'your-token',
});
// Web
await IMSDK.login({
userID: 'your-user-id',
token: 'your-token',
platformID: 5,
apiAddr: 'http://your-server-ip:10002',
wsAddr: 'ws://your-server-ip:10001',
logLevel: LogLevel.Debug,
});
function handleConnecting() {
// Connecting...
}
function handleConnectFailed({ errCode, errMsg }: WSEvent) {
// Connection failed â
console.log(errCode, errMsg);
}
function handleConnectSuccess() {
// Connection successful â
}
5. Send & Receive Messagesâ
import { CbEvents } from '@openim/wasm-client-sdk';
import type { WSEvent, MessageItem } from '@openim/wasm-client-sdk/lib/types/entity';
// Listen for new messages đŠ
IMSDK.on(CbEvents.OnRecvNewMessages, handleNewMessages);
const message = (await IMSDK.createTextMessage('hello openim')).data;
IMSDK.sendMessage({
recvID: 'recv user id',
groupID: '',
message,
})
.then(() => {
// Message sent successfully âī¸
})
.catch(err => {
// Failed to send message â
console.log(err);
});
function handleNewMessages({ data }: WSEvent<MessageItem[]>) {
// New message list đ¨
console.log(data);
}