Loading...
Loading...
Build real-time order tracking and delivery driver systems with PubNub
npx skill4agent add pubnub/skills pubnub-order-delivery-driver| Reference | Purpose |
|---|---|
| delivery-setup.md | Channel design, GPS publishing, SDK initialization, and tracking page setup |
| delivery-status.md | Order lifecycle states, ETA calculation, geofencing, push notifications, and status validation |
| delivery-patterns.md | Dispatch coordination, driver-customer chat, fleet dashboards, privacy controls, and proof of delivery |
import PubNub from 'pubnub';
const pubnub = new PubNub({
publishKey: 'pub-key',
subscribeKey: 'sub-key',
userId: 'driver-1234'
});
let lastPublishedLocation = null;
function publishDriverLocation(latitude, longitude, heading, speed) {
const location = {
lat: latitude,
lng: longitude,
heading: heading,
speed: speed,
timestamp: Date.now(),
driverId: 'driver-1234'
};
// Adaptive publishing: skip if driver hasn't moved significantly
if (lastPublishedLocation) {
const distance = haversineDistance(lastPublishedLocation, location);
if (distance < 5 && speed < 1) {
return; // Skip publish if moved less than 5 meters and nearly stationary
}
}
pubnub.publish({
channel: 'driver.driver-1234.location',
message: location
});
lastPublishedLocation = location;
}function subscribeToOrderTracking(orderId, driverId) {
pubnub.subscribe({
channels: [
`order.${orderId}.status`,
`driver.${driverId}.location`
]
});
pubnub.addListener({
message: (event) => {
if (event.channel.includes('.status')) {
updateOrderStatusUI(event.message);
} else if (event.channel.includes('.location')) {
updateDriverMarkerOnMap(event.message);
recalculateETA(event.message);
}
}
});
}async function updateOrderStatus(orderId, newStatus, metadata = {}) {
const statusUpdate = {
orderId: orderId,
status: newStatus,
timestamp: Date.now(),
...metadata
};
await pubnub.publish({
channel: `order.${orderId}.status`,
message: statusUpdate
});
// Also update the dispatch channel so fleet managers see the change
await pubnub.publish({
channel: 'dispatch.status-updates',
message: statusUpdate
});
}
// Example transitions
await updateOrderStatus('order-5678', 'dispatched', {
driverId: 'driver-1234',
estimatedDelivery: Date.now() + 25 * 60 * 1000
});