How openimsdk/openim-sdk-core works
Project type and purpose
• This is a cross-platform SDK core implemented in Go.
• It provides IM networking (HTTP/WebSocket), message encoding/decoding, local storage, synchronization, and callback management.
• It targets multiple platforms via direct Go builds (Windows/MacOS/Linux), GoMobile for iOS/Android, and WebAssembly (wasm).
Top-level structure & architectural patterns
• internal/: Domain modules (conversation_msg, group, relation, user, third, interaction). Each module handles its own API calls, data conversion, sync logic, and notifications.
• pkg/: Shared infrastructure—network clients, DB helpers, cache, syncers, common utilities, config, error handling, version management.
• wasm/: WebAssembly target—cmd (entrypoint), event_listener (JS callbacks), exec, indexdb (client-side storage models), wasm_wrapper (JS bindings).
• open_im_sdk/ & open_im_sdk_callback/: Language-specific bindings/callers and callback client for mobile/desktop wrappers.
• test/ & integration_test/ & msgtest/: Test clients simulating app behavior; demonstrate SDK usage and validate interfaces.
Core components
a. SDK Core (Go)
• internal modules: Conversation, Group, Relation, User, Third-party files, Interaction (long-conn & WS manager)
• pkg infra: Network (HTTP & WS client), DB layer (SQLite via pkg/db), Cache (in-memory caches), Syncer (state machine), Utils
b. Platform Wrappers
• GoMobile bindings (iOS/Android) & C-API via open_im_sdk/…
• WebAssembly layer (wasm/cmd → wasm_wrapper → JS)
c. External Services
• OpenIM Server APIs (HTTP REST + WebSocket)
• Local persistence (SQLite for native, IndexedDB for WebAssembly)
d. Test Clients
• Unit tests (test/)
• Integration tests (integration_test/)
• msgtest load/pressure modules
Relationships & data flows
• Initialization: load config → establish WS + HTTP connections (pkg/cliconf → pkg/network → internal/interaction).
• Message send/receive:
• App call → open_im_sdk (binding) → internal API (e.g., internal/conversation_msg) → network layer → server
• Server push → WS client → interaction module → dispatch to appropriate internal module → invoke callbacks (open_im_sdk_callback)
• Sync & persistence:
• On login or reconnection → syncer invokes internal modules → fetch from server → pkg/db persists → pkg/cache updates
• WebAssembly: same flows but route through wasm_wrapper and store data in IndexedDB.
Architectural patterns & principles
• Modular layering: “internal” for domain logic, “pkg” for shared infra.
• Adapter/Decorator: pkg/sdk_params_callback for converting between Go structs and platform-specific callback formats.
• Stateful syncer: pkg/syncer drives full/incremental sync workflows.
• Observer/Callback: subscription model in internal/interaction and open_im_sdk_callback.