Overview
Sync messages from a specified channel, supporting message retrieval by sequence number range.
Request Body
Required Parameters
Current logged-in user ID
Channel type (1=personal channel, 2=group channel)
Optional Parameters
Starting message sequence number (inclusive)
Ending message sequence number (exclusive)
Maximum number of messages to return, maximum 10000
Pull mode (0=pull down, 1=pull up)
curl -X POST "http://localhost:5001/channel/messagesync" \
-H "Content-Type: application/json" \
-d '{
"login_uid": "user123",
"channel_id": "group123",
"channel_type": 2,
"start_message_seq": 1000,
"end_message_seq": 1100,
"limit": 50,
"pull_mode": 0
}'
[
{
"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="
},
{
"message_id": 123456790,
"message_seq": 1002,
"client_msg_no": "client_msg_124",
"from_uid": "user456",
"channel_id": "group123",
"channel_type": 2,
"timestamp": 1640995260,
"payload": "SGkgdGhlcmU="
}
]
Response Fields
The response is an array of message objects, each containing:
Base64 encoded message content
Status Codes
| Status Code | Description |
|---|
| 200 | Message sync successful |
| 400 | Request parameter error |
| 403 | No access permission |
| 500 | Internal server error |
Use Cases
Chat History Loading
Load Recent Messages:
// Load last 50 messages
const messages = await syncChannelMessages({
login_uid: "user123",
channel_id: "group123",
channel_type: 2,
limit: 50,
pull_mode: 0
});
Load Older Messages:
// Load messages before a specific sequence
const olderMessages = await syncChannelMessages({
login_uid: "user123",
channel_id: "group123",
channel_type: 2,
end_message_seq: 1000,
limit: 50,
pull_mode: 1
});
Message Search and Export
Export Chat History:
async function exportChatHistory(channelId, channelType, loginUid) {
let allMessages = [];
let startSeq = 0;
const batchSize = 1000;
while (true) {
const messages = await syncChannelMessages({
login_uid: loginUid,
channel_id: channelId,
channel_type: channelType,
start_message_seq: startSeq,
limit: batchSize,
pull_mode: 0
});
if (messages.length === 0) break;
allMessages = allMessages.concat(messages);
startSeq = messages[messages.length - 1].message_seq + 1;
}
return allMessages;
}
Offline Message Sync
Sync Missed Messages:
async function syncMissedMessages(channelId, channelType, loginUid, lastSeq) {
const missedMessages = await syncChannelMessages({
login_uid: loginUid,
channel_id: channelId,
channel_type: channelType,
start_message_seq: lastSeq + 1,
limit: 1000,
pull_mode: 0
});
return missedMessages;
}
Best Practices
- Reasonable Range: Avoid syncing too many messages at once
- Pagination: Use limit parameter to control return quantity
- Error Handling: Handle network errors and permission errors
- Caching Strategy: Reasonably cache synced messages
- Performance Optimization: Adjust sync frequency based on actual needs
- Permission Check: Verify user has access to the channel before syncing
- Rate Limiting: Implement rate limiting to prevent excessive API calls