'flutter laravel websockets Private Channel trigger current location

i have laravel and set up the websockets and all is good and in my flutter app i connect to websockets private channel and its good but here is the bug is that i listen to the server events but i need the app to send data to channel and let all users on same channel see the data the flutter dont (trigger) data on channel (the delivery boy send his current location to the channel and the customer listen to the delivery location that updated)

The Flutter Code

import 'package:pusher_client/pusher_client.dart';
import 'package:location/location.dart';
import 'package:flutter/material.dart';

class DeliveryLocationTrackingProvider extends ChangeNotifier {
  late Channel _channel;

  // the user's initial location and current location
  // as it moves
  late LocationData _currentLocation;
  // wrapper around the location API
  late Location _location;

  Location get location => this._location;
  LocationData get currentLocation => this._currentLocation;

  set location(Location location) {
    this._location = location;
  }

  set currentLocation(LocationData currentLocation) {
    this._currentLocation = currentLocation;
  }

  late PusherClient pusher;
  late Channel channel;
  late PusherOptions pusherOptions;
  //
  connectToPusher() {
    PusherAuth auth = PusherAuth(
      'http://shahwanji.com/broadcasting/auth',
      headers: {
        'Authorization': 'Bearer ${SharedManager.shared.token}',
        'Content-Type': 'application/json'
      },
    );

    pusherOptions = PusherOptions(
      host: '*********.com',
      wsPort: 6001,
      encrypted: false,
      auth: auth,
    );

    pusher = PusherClient(
      '****************',
      pusherOptions,
      enableLogging: true,
    );

    pusher.connect();

    pusher.onConnectionStateChange((state) {
      print("currentState: ${state?.currentState} -- -- ");
    });

    pusher.onConnectionError((error) {
      print("error: ${error?.message}");
    });
  }

  disconnectFromPusher() {
    // // Disconnect from pusher service
    pusher.disconnect();
  }

  Future<Channel> connectToChannel(String channelName) async {
    await this.connectToPusher();

    return pusher.subscribe(channelName);
  }
  // ////////

  listenToMove() async {
    // create an instance of Location
    this._location = new Location();

    // set the initial location by pulling the user's
    // current location from the location's getLocation()
    this._currentLocation = await this._location.getLocation();

    // subscribe to changes in the user's location
    // by "listening" to the location's onLocationChanged event

    this._connect();

    this._location.onLocationChanged.listen((LocationData cLoc) {
      //   // cLoc contains the lat and long of the
      //   // current user's position in real time,
      //   // so we're holding on to it
      this._currentLocation = cLoc;
      print("cLoc.latitude ${cLoc.latitude}");
      _channel.trigger('client-update-location', {
        'the-data-latitude': cLoc.latitude,
        'the-data-longitude': cLoc.longitude,
      });

      notifyListeners();
    });
  }

  _connect() async {
    this._channel = await connectToChannel(
        'private-delivery.${SharedManager.shared.delivery.id}');
  }

  listenLocation() async {
    await this._connect();

    this._channel.bind("update-location", (PusherEvent? event) {
      print("update-location update-location ${event?.data}");
      _channel.trigger('client-update-location',
          {'the-data-latitude': 0.0, 'the-data-longitude': 0.0});
    });

    _channel.bind("pusher:subscription_succeeded", (PusherEvent? event) {
      _channel.trigger('client-update-location',
          {'the-data-latitude': 0.0, 'the-data-longitude': 0.0});
      print('event.data ${event?.data}');
    });
  }
}

Laravel Code

<?php

namespace App\Events\Delivery;

use App\Models\Delivery;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class TrackDeliveryLocationEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public Delivery $delivery;
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Delivery $delivery)
    {
        $this->delivery = $delivery;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('delivery.' . $this->delivery->id);
    }

    // the event name we listion to
    public function broadcastAs()
    {
        return 'update-location';
    }

    // the data send with every send
    public function broadcastWith()
    {
        return [
            'the-data' => 'Hi In Test',
        ];
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source