๐งญ Browser Integration Guideโ
This document provides a detailed guide on integrating the OpenIM WASM Client SDK into your Web / Electron project for quick setup and message communication.
๐ Quick Demoโ
We strongly recommend running our official sample project before starting integration:
๐ OpenIM Electron Demo
This Demo helps you:
- Quickly verify IM SDK core functionality
- Familiarize yourself with the complete login and messaging workflow
- Avoid path and configuration issues during integration
๐งฐ Requirementsโ
Before integrating the SDK, ensure:
- Node.js โฅ 16.x
- npm or yarn package manager
- Browser supports WebAssembly (all modern mainstream browsers support it)
- Frontend framework (e.g., React, Vue, Electron) is properly initialized
โ ๏ธ Note: If using Webpack 4, refer to this issue for additional configuration.
๐ฆ Install Dependenciesโ
npm install @openim/wasm-client-sdk
# or
yarn add @openim/wasm-client-sdk
๐ Configure WASM Static Resourcesโ
The SDK requires WebAssembly files to run. Copy the static resources to your project's public directory.
- Navigate to the
node_modules/@openim/wasm-client-sdkdirectory - Find the
assetsfolder - Copy the following files to your project's
publicdirectory:
openIM.wasm
sql-wasm.wasm
wasm_exec.js
- Add the following
<script>tag inpublic/index.html:
<script src="/wasm_exec.js"></script>
โ ๏ธ Paths must match the
coreWasmPathandsqlWasmPathparameters.
๐งญ Initialize SDKโ
Initialize the SDK in your project's entry file.
import { getSDK } from '@openim/wasm-client-sdk';
const IMSDK = getSDK({
coreWasmPath: "/openIM.wasm", // WASM core file path
sqlWasmPath: "/sql-wasm.wasm", // SQLite WASM file path
debug: true // Enable debug logging
});
๐ก It is recommended to call
getSDKearly in the application lifecycle, ensuring it is only initialized once globally.
๐ Login & Event Listenersโ
The SDK provides a series of event callbacks for monitoring connection status.
You can bind these events before logging in for subsequent status monitoring and reconnection handling.
import { getSDK, CbEvents } from '@openim/wasm-client-sdk';
const IMSDK = getSDK();
// Connection-related events
IMSDK.on(CbEvents.OnConnecting, () => console.log('โณ Connecting...'));
IMSDK.on(CbEvents.OnConnectSuccess, () => console.log('โ
Connected'));
IMSDK.on(CbEvents.OnConnectFailed, (e) => console.error('โ Connection failed', e));
IMSDK.on(CbEvents.OnUserTokenExpired, () => console.warn('โ ๏ธ Token expired'));
// Login
IMSDK.login({
userID: 'yourUserID',
token: 'yourToken',
platformID: 5, // 5 for Web
apiAddr: 'http://your-server-ip:10002',
wsAddr: 'ws://your-server-ip:10001',
})
.then(() => console.log('๐ Login successful'))
.catch(({ errCode, errMsg }) => console.error('๐ซ Login failed', errCode, errMsg));
๐ Best Practices:
- Bind events before logging in to avoid missing status changes.
- After token expiration, call
IMSDK.login()again to refresh the connection.- A successful
logincall does not mean the connection to IMServer is established โ listen for connection events to confirm status.
๐ฌ Send & Receive Messagesโ
Listen for Messagesโ
IMSDK.on(CbEvents.OnRecvNewMessages, ({ data: messages }) => {
messages.forEach((msg) => {
console.log('๐ฉ New message received:', msg);
});
});
Send a Text Messageโ
// Create a text message
const { data: message } = await IMSDK.createTextMessage('Hello OpenIM!');
// Send message
IMSDK.sendMessage({
recvID: 'targetUserID', // For private chat
groupID: '', // For group chat
message,
})
.then(({ data: succeedMessage }) => {
console.log('โ
Sent successfully', succeedMessage);
})
.catch(({ errCode, errMsg }) => {
console.error('๐ซ Send failed', errCode, errMsg);
});
๐ Tips:
- Use
recvIDfor private chats,groupIDfor group chats.- You can use
IMSDK.createImageMessageByFile(),IMSDK.createVideoMessageByFile(), and other APIs to send different message types.- The successfully sent message returned is the complete message structure populated by the server.
๐งช Troubleshootingโ
| Issue Type | Possible Cause | Solution |
|---|---|---|
โ Console shows window.Go() is undefined | Static resource wasm_exec.js not copied or path misconfigured | Verify files exist in the public directory and paths match initialization parameters |
โ ๏ธ WebAssembly initialization failed | Browser version too old or incorrect path | Use a modern browser, check coreWasmPath and sqlWasmPath paths |
| ๐ซ Connection failed | Wrong token, incorrect API address, or port blocked by firewall | Verify the platform passed when obtaining the token matches the platform used for IMSDK login, confirm apiAddr and wsAddr are accessible |
๐งญ Debugging Tipsโ
- Enable
debug: trueto view detailed logs - Use browser DevTools โ Network to monitor WebSocket connection status
- Use
IMSDK.getLoginStatus()to check login status
๐งฑ More APIsโ
The SDK provides rich APIs covering:
- โ User information management
- ๐ฉ Message history queries
- ๐งโ๐คโ๐ง Group operations
- ๐ก Custom message sending
- ๐ก๏ธ Conversation management, message recall, read receipts, etc.
๐ Full API documentation:
IMSDK API Reference