Skip to main content

๐Ÿงญ 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.

  1. Navigate to the node_modules/@openim/wasm-client-sdk directory
  2. Find the assets folder
  3. Copy the following files to your project's public directory:
openIM.wasm
sql-wasm.wasm
wasm_exec.js
  1. Add the following <script> tag in public/index.html:
<script src="/wasm_exec.js"></script>

โš ๏ธ Paths must match the coreWasmPath and sqlWasmPath parameters.


๐Ÿงญ 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 getSDK early 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 login call 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 recvID for private chats, groupID for 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 TypePossible CauseSolution
โŒ Console shows window.Go() is undefinedStatic resource wasm_exec.js not copied or path misconfiguredVerify files exist in the public directory and paths match initialization parameters
โš ๏ธ WebAssembly initialization failedBrowser version too old or incorrect pathUse a modern browser, check coreWasmPath and sqlWasmPath paths
๐Ÿšซ Connection failedWrong token, incorrect API address, or port blocked by firewallVerify the platform passed when obtaining the token matches the platform used for IMSDK login, confirm apiAddr and wsAddr are accessible

๐Ÿงญ Debugging Tipsโ€‹

  • Enable debug: true to 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


๐Ÿ“š Referencesโ€‹