我正在使用 websockets 开发 laravel 应用程序。我已经从库中配置了 websocket
构建像 uber 或 ola 这样的实时应用程序。
客户正在创建具有起点和终点位置的旅行。
在后端选择最近的司机位置,需要向最近的司机广播行程
创建新行程时广播事件。
事件服务提供者
\App\Events\NewTripHasCreatedEvent::class => [
\App\Listeners\SendTripCreationNotificationToTheDriverListener::class,
],
NewTripHasCreatedEvent
class NewTripHasCreatedEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $trip;
public $trip_details;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Trip $trip, $trip_details)
{
$this->trip = $trip;
$this->trip_details = $trip_details;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
SendTripCreationNotificationToTheDriverListener
public function handle(NewTripHasCreatedEvent $event)
{
Find nearest driver
$km = 0.008997742; //1 km = 0.008997742 degree
$drivers_distance = 5;
$query = Driver::Distance('location', $trip->from_location, $drivers_distance * $km)->take(5)->pluck('user_id');
//Here send notification to Driver
}