AMINEDGE
AMINEDGE

Reputation: 447

Laravel broadcasting channel function won't fire

So I'm gonna use laravel broadcasting for a chat app,

I followed Laravel Broadcasting approach,

PUSHER_APP_ID
PUSHER_APP_KEY
PUSHER_APP_SECRET
PUSHER_APP_CLUSTER
'options' => [
    'cluster' => 'us2',
    'encrypted' => true,
],

to pusher array inside connections array based on my channel info in pusher panel

Here is my event class

<?php

namespace App\Events;
use App\User;
use App\TherapyMessage;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\AppLog;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;

class TherapyMessageSent implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * User that sent the message
     *
     * @var User
     */
    public $user;

    /**
     * Message details
     *
     * @var Message
     */
    public $message;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(User $user, TherapyMessage $message)
    {
        $this->user = $user;
        $this->message = $message;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        $message_id = $this->message->id;
        $user = $this->user;

        AppLog::create([
            'file_name' => __FILE__,
            'message' => "broadcast before send with Message ID of $message_id from $user->full_name"
        ]);

        return new PrivateChannel("therapy-chat.$message_id");
    }
}

protected $listen = [
    Registered::class => [
        SendEmailVerificationNotification::class,
        TherapyMessageSent::class
    ],
];

I also imported the event namespace next to other namespaces inside the file: use \App\Events\TherapyMessageSent;

Here is the channel that I defined inside routes/channels.php file:


use App\AppLog;

Broadcast::channel('therapy-chat.{message_id}', function ($user, $message_id) {

    AppLog::create([
        'file_name' => __FILE__,
        'message' => "broadcast sending with Message ID of $message_id to $user->full_name"
    ]);

    if (!Auth::check()) return false;

    $message = \App\TherapyMessage::find($message_id);

    if (!$message) {

        AppLog::create([
            'file_name' => __FILE__,
            'message' => "Message with ID of $message_id Not Found for broadcasting"
        ]);

        return false;
    }

    $will_send = false;

    if ($therapist = $user->therapist) {
        $will_send = $therapist->id === $message->therapist_id;
    } else if ($patient = $user->patient) {
        $will_send = $message->patient_id === $patient->id;
    }

    if ($will_send) {

        AppLog::create([
            'file_name' => __FILE__,
            'message' => "Message with ID of $message_id broadcasted to $user->full_name"
        ]);
    }

    return $will_send;
});

Finally, this is my controller method:

public function sendToTherapist(Request $request) {

    $validation = \Validator::make($request->all(), ['message' => 'required']);

    if ($validation->fails()) return $this->validationError($validation);

    $user = \Auth::user();
    $patient = $user->patient;
    $therapist = $patient->therapist;

    if (!$therapist) return $this->errorWithMessage('Patient does not have Therapist');

    $message = \App\TherapyMessage::create([
        'patient_id' => $patient->id,
        'therapist_id' => $therapist->id,
        'type' => TherapyMessageType::TEXT,
        'text' => $request->message,
        'sender_role' => TherapyMessageSenderRole::PATIENT
    ]);

    broadcast(new TherapyMessageSent($user, $message))->toOthers();

    return $this->success(['id' => $message->id]);
}

As you see in the code above I filled $user and $message with correct values and the request works without any error

I think the channel method won't even be fired, as I check the AppLog table when I call broadcast method, only the log inside TherapyMessageSent event broadcastOn function is saved and even the log that I save at the beginning of channels.php method, isn't saved so I think this method is never executed.

If anyone could help me with the problem, I'd be thankful.

Upvotes: 0

Views: 1339

Answers (0)

Related Questions