Concept Explanation
What is a Message?
A Message is the basic unit of information transmission in WuKongIM, serving as the carrier for real-time communication between users. Each message contains information about the sender, receiver, content, and metadata.
Why are Messages Important?
- Communication Foundation: Messages are the core of instant messaging systems; all chat functionality is based on message transmission
- Data Carrier: Messages can transmit not only text but also various types of data like images, files, and locations
- Status Tracking: Each message has a unique identifier and status, making it easy to track message sending, receiving, and read status
Relationship with Other Concepts
- Channel: Messages are transmitted through channels, which define the message destination
- User: Messages have clear senders and receivers, all of whom are users in the system
- Conversation: Messages update corresponding conversation records, affecting conversation list display
Core Structure
Messages contain the following core attributes:
| Attribute | Type | Description |
|---|
message_id | integer | Message unique identifier |
message_seq | integer | Message sequence number within channel |
client_msg_no | string | Client message identifier |
from_uid | string | Sender user ID |
channel_id | string | Target channel ID |
channel_type | integer | Channel type (1=personal, 2=group) |
timestamp | integer | Message timestamp |
payload | string | Base64 encoded message content |
Message Example
{
"message_id": 123456789,
"message_seq": 1001,
"client_msg_no": "client_msg_123",
"from_uid": "user123",
"channel_id": "group123",
"channel_type": 2,
"timestamp": 1640995200,
"payload": "SGVsbG8gV29ybGQ="
}
| Endpoint | Method | Description |
|---|
/message/send | POST | Send single message |
/message/sendbatch | POST | Send batch messages |
/message/search | POST | Search historical messages |
/channel/messagesync | POST | Sync channel messages |
EasySDK Code Examples
Send Messages
import { WKIM, WKIMChannelType } from 'easyjssdk';
// Initialize SDK
const im = WKIM.init("ws://your-server.com:5200", {
uid: "your_user_id",
token: "your_token"
});
// Send text message
const textPayload = {
type: 1,
content: "Hello, World!"
};
const result = await im.send("friend_user_id", WKIMChannelType.Person, textPayload);
// Send image message
const imagePayload = {
type: 2,
url: "https://example.com/image.jpg",
width: 800,
height: 600
};
const imageResult = await im.send("group_id", WKIMChannelType.Group, imagePayload);
Listen for Messages
import { WKIMEvent } from 'easyjssdk';
// Listen for new messages
im.on(WKIMEvent.Message, (message) => {
console.log("Received new message:", message);
console.log("Message content:", message.payload);
console.log("Sender:", message.fromUid);
});
Message Type Handling
// Handle different content based on message type
im.on(WKIMEvent.Message, (message) => {
const payload = JSON.parse(message.payload);
switch (payload.type) {
case 1: // Text message
console.log("Text message:", payload.content);
break;
case 2: // Image message
console.log("Image message:", payload.url);
break;
case 100: // Custom message
console.log("Custom message:", payload);
break;
}
});
The message content payload field uses Base64 encoding, with the specific format defined by the application layer.