Tài liệu API và WebSocket Integration

1. Register/Login Customer/Driver
Thực hiện đăng ký/đăng nhập cho Customer/Driver
2. Register Pusher

import Pusher from "pusher-js";

// Configure Pusher client
const pusher = new Pusher(import.meta.env.VITE_REVERB_APP_KEY, { //jwmobrei3pmoleii6tab
    wsHost: import.meta.env.VITE_REVERB_HOST, // apiv3.thuelai.vn
    wsPort: import.meta.env.VITE_REVERB_PORT, // 8080
    wssPort: import.meta.env.VITE_REVERB_SCHEME, // wss
    forceTLS: false,
    disableStats: true,
    enabledTransports: ["ws", "wss"],
    cluster: "mt1",
    authEndpoint: "/api/customer/broadcasting/auth",
    auth: {
        headers: {
            Authorization: "Bearer TOKEN",
            Accept: "application/json",
        },
    },
});

// Subscribe to private channel
const channel = pusher.subscribe("private-chillgo.customer.1");

// Listen for events
channel.bind("DriverStatusUpdated", function (data) {
    console.log("Received event:", data);
});

// Handle connection states
pusher.connection.bind("connected", () => {
    console.log("Connected to Reverb websockets");
});

pusher.connection.bind("error", (err) => {
    console.error("Connection error:", err);
});

// Export for reuse if needed
export default pusher;
Tương tự với Driver thay endpoint = /api/driver/broadcasting/auth
3. Create Order
Request Example:

{
    "service_id":2,
    "pick_up_address":"Hồ phú đô",
    "pick_up_latitude":21.0257737,
    "pick_up_longitude":105.7433512,
    "drop_off_address":"Hồ hoàn kiếm",
    "drop_off_latitude":21.0284297,
    "drop_off_longitude":105.7782687
}
Response Example:

{
    "code": 200,
    "message": "Success",
    "data": {
        "customer_id": 1,
        "booking_service_id": 2,
        "created_at": "2025-04-21T02:33:55.000000Z",
        "updated_at": "2025-04-21T02:33:55.000000Z",
        "pick_up_address": "Hồ phú đô",
        "pick_up_latitude": 21.0257737,
        "pick_up_longitude": 105.7433512,
        "drop_off_address": "Hồ hoàn kiếm",
        "drop_off_latitude": 21.0284297,
        "drop_off_longitude": 105.7782687,
        "number_hour": 0,
        "number_day": 0,
        "distance": 6.069,
        "start_date": null,
        "end_date": null,
        "requirement": "",
        "note": "",
        "number_driver": 1,
        "base_price": 321000,
        "info_price": "{\"base_price\":321000,\"vat\":0,\"platform_fee\":0,\"final_price\":321000}",
        "final_price": 321000,
        "order_status_id": 1,
        "id": 14
    }
}
4. Driver nhận được event thông báo cuốc xe mới

{
    "event":"chillgo.driver.ride.new",
    "data":"{\"ride\":{\"id\":5,\"pick_up_address\":\"Hồ phú đô\",\"pick_up_latitude\":\"21.0257737\",\"pick_up_longitude\":\"105.7433512\",\"drop_off_address\":\"Hồ hoàn kiếm\",\"drop_off_latitude\":\"21.0284297\",\"drop_off_longitude\":\"105.7782687\",\"distance\":\"6.07\",\"final_price\":\"321000.00\",\"status\":{\"id\":1,\"label\":\"Đang tìm tài xế\"}},\"time\":\"2025-04-21 02:33:55\"}",
    "channel":"private-chillgo.driver.1"
}
5. Driver Actions
accept: nhận cuốc
reject: từ chối cuốc

API: api/driver/booking/accept

Chú ý truyền lên là ride id, distance_to_pickup (km), estimated_time_to_pickup(phút)
Response Example:

{
    "code": 200,
    "message": "Success",
    "data": {
        "message": "Your application has been recorded"
    }
}
6. Demo Accept Driver

Chạy link http://localhost:8000/demo-accept-driver để chấp nhận cuốc xe

Chú ý là demo nên 1 cuốc chỉ nên có 1 thằng nhận để dễ làm.

Hiện tại đang làm đơn giản sắp xếp thằng gần nhất dựa trên khoảng cách tài xế gửi lên. Nếu làm nhiều tài xế thì chú ý cái này

7. Socket Events
Driver Event:

{"event":"chillgo.driver.ride.changed","data":"{\"ride\":{\"id\":5,\"pick_up_address\":\"Hồ phú đô\",\"pick_up_latitude\":\"21.0257737\",\"pick_up_longitude\":\"105.7433512\",\"drop_off_address\":\"Hồ hoàn kiếm\",\"drop_off_latitude\":\"21.0284297\",\"drop_off_longitude\":\"105.7782687\",\"distance\":\"6.07\",\"final_price\":\"321000.00\",\"status\":{\"id\":2,\"label\":\"Tìm thấy tài xế\"}},\"time\":\"2025-04-21 02:40:54\"}","channel":"private-chillgo.driver.1"}
Customer Event:

{"event":"chillgo.customer.ride.changed","data":"{\"ride\":{\"id\":5,\"pick_up_address\":\"Hồ phú đô\",\"pick_up_latitude\":\"21.0257737\",\"pick_up_longitude\":\"105.7433512\",\"drop_off_address\":\"Hồ hoàn kiếm\",\"drop_off_latitude\":\"21.0284297\",\"drop_off_longitude\":\"105.7782687\",\"distance\":\"6.07\",\"final_price\":\"321000.00\",\"status\":{\"id\":2,\"label\":\"Tìm thấy tài xế\"}},\"time\":\"2025-04-21 02:40:54\"}","channel":"private-chillgo.customer.1"}
8. Trạng thái cuốc xe
Các trạng thái:
ID Trạng thái
1Đang tìm tài xế
2Tìm thấy tài xế
3Tài xế đang di chuyển
4Tài xế đã đến nơi
5Tài xế đang chờ khách
6Tài xế đã đón khách
7Đang di chuyển
8Đã hoàn thành chuyến
9Đã đánh giá chuyến đi
10Tài xế hủy chuyến
11Khách hàng hủy chuyến
12Hệ thống hủy chuyển
13Admin hủy chuyển
Luồng trạng thái:
  • 2 => [3, 10]
  • 3 => [4, 10]
  • 4 => [5, 10]
  • 5 => [6, 10]
  • 6 => [7,10]
  • 7=>[8]
  • 8=>[9]
Trạng thái 6 và 8 cần up 5 ảnh lên
API Update Status:

api/driver/booking/status

Request: ride_id, status images[]


{
    "code": 200,
    "message": "Success",
    "data": {
        "message": "Ride status updated successfully"
    }
}