Presence API#

API theo dõi trạng thái online/offline của user qua WebSocket và REST.

Base URL: https://app.socialking.vn/api/v1


1. WebSocket - Theo dõi Presence#

WS /presence/ws?token=YOUR_JWT_TOKEN#

Auth: JWT Token trong query string

Protocol: WebSocket

Kết nối:

const ws = new WebSocket('wss://app.socialking.vn/api/v1/presence/ws?token=YOUR_JWT');

Client gửi:

// Heartbeat (gửi định kỳ)
{ "type": "heartbeat", "activity": true }

// Ping
{ "type": "ping" }

Server trả về:

// Khi kết nối thành công
{ "type": "connected", "data": { "userId": "uuid", "status": "online" } }

// Heartbeat ACK
{ "type": "heartbeat_ack", "data": { "timestamp": "2026-03-30T10:30:00Z" } }

// Pong
{ "type": "pong" }

2. Số user online#

GET /presence/online-count #

Auth: Không cần

Rate Limit: 30 req/phút

Response (200):

{
  "success": true,
  "count": 42
}

3. Danh sách user online#

GET /presence/online-users #

Auth: Bearer Token (JWT)

Rate Limit: 10 req/phút

Query Parameters:

ParamTypeDefaultMô tả
limitnumber100Số lượng tối đa

Response (200):

{
  "success": true,
  "users": [
    {
      "userId": "uuid-string",
      "status": "online",
      "lastSeen": "2026-03-30T10:30:00Z",
      "deviceInfo": {
        "browser": "Chrome",
        "os": "Windows",
        "deviceType": "desktop"
      }
    }
  ]
}

4. Kiểm tra user online#

GET /presence/check/:userId #

Auth: Bearer Token (JWT)

Rate Limit: 30 req/phút

URL Params: userId (UUID)

Response (200):

{
  "success": true,
  "userId": "uuid-string",
  "online": true,
  "lastSeen": "2026-03-30T10:30:00Z"
}

5. Kiểm tra nhiều user#

POST /presence/check-multiple #

Auth: Bearer Token (JWT)

Rate Limit: 10 req/phút

Request Body:

{
  "userIds": ["uuid-1", "uuid-2", "uuid-3"]
}
FieldTypeRequiredValidation
userIdsUUID[]YesMax 100 IDs

Response (200):

{
  "success": true,
  "users": [
    {
      "userId": "uuid-1",
      "online": true,
      "status": "online",
      "lastSeen": "2026-03-30T10:30:00Z"
    },
    {
      "userId": "uuid-2",
      "online": false,
      "status": "offline",
      "lastSeen": "2026-03-29T18:00:00Z"
    }
  ]
}