Adding real-time communication
Now that we are done with adding the routes, authentication, and database configuration and manipulation code, we are ready to add the code for letting us communicate between the frontend and backend in real time. First, we need to create an event
class in our Laravel backend so that we can call the event
function to broadcast the event as we did in MessageController
.
To do that, we run the php artisan make:event MessageSent
command to create the MessageSent
event class. The class should now be in the backend/app/Events/MessageSent.php
file. Once the file is created, we replace what is inside the file with the following code:
<?php namespace App\Events; ... class MessageSent implements ShouldBroadcast { use InteractsWithSockets, SerializesModels; public $user; public $message; public function __construct(User $user, Message ...