2021年1月22日星期五

Laravel Websocket Send Message to Particular Android Devices

I am working on laravel application using websockets. I have configured websockets from the library

Building a real time application like uber or ola.

Customer are creating a trip with start and end location.

In backend selecting the nearest driver location and need to broadcast trip to nearest drivers

Event is broadcasted when new trip has been created.

EventServiceProvider

\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  }  
https://stackoverflow.com/questions/65855936/laravel-websocket-send-message-to-particular-android-devices January 23, 2021 at 01:08PM

没有评论:

发表评论