WebSocket Events

Real-time vehicle updates using WebSocket connections.

Vehicle Updates Format

Expected message format for vehicle updates

Update Vehicle Data

{
  "vehicles": [
    {
      "id": 123,
      "latitude": 34.0522,
      "longitude": -118.2437,
      "battery_level": 85,
      "state": "DOCKED"
    }
  ]
}

Valid Vehicle States

  • DOCKED - Vehicle is at depot
  • EN_ROUTE_TO_PICKUP - Vehicle heading to pickup
  • AWAITING_PASSENGER - Vehicle waiting for passenger
  • IN_RIDE - Vehicle in active ride
  • RETURNING - Vehicle returning to depot

Connection Example

JavaScript

const socket = new WebSocket(
  'ws://your-domain/ws/vehicles/'
);

// Send vehicle updates
socket.send(JSON.stringify({
  vehicles: [{
    id: 123,
    latitude: 34.0522,
    longitude: -118.2437,
    battery_level: 85,
    state: "DOCKED"
  }]
}));

Implementation Notes

  • Updates are processed asynchronously
  • Each message can update multiple vehicles
  • All fields (latitude, longitude, battery_level, state) are required
  • Vehicle must exist in database (valid id required)
  • State must be one of the valid vehicle states

Broadcast Updates

Receiving vehicle update broadcasts

To receive broadcasts about vehicle updates, connect to the vehicle updates WebSocket:

const updates = new WebSocket(
  'ws://your-domain/ws/vehicle_updates/'
);

updates.onmessage = (event) => {
  const update = JSON.parse(event.data);
  console.log('Vehicle update received:', update);
};

Broadcasts will be sent whenever vehicle data is updated through the main WebSocket connection.