In this episode, we dive into creating channels and broadcasting events in a Laravel application. We start off by running npm run dev
to build our assets and spin up the Reverb server in debug mode. With our environment ready, we open the channels
file and set up a simple public channel called chat
for a global chat app—no authentication required!
Next, we create a new event using Laravel's artisan command. You'll learn about the difference between regular events and broadcastable events, and how you can use the ShouldBroadcastNow
interface for instant broadcasting (no queue setup needed for now!). We set up our event to broadcast on the chat
channel by specifying it in the event's broadcastOn
method.
We also cover how to trigger this broadcast. Instead of dispatching the event the usual way, we use Laravel's broadcast
helper function in a dedicated route. This lets us test sending events directly to our WebSocket server.
Finally, we walk through firing off the event and show you what happens if your WebSocket server isn't running (spoiler: you get an error!). By the end of the episode, we've covered the essential setup for channels and broadcasting. Next time, we'll jump over to the frontend and listen for these events using Laravel Echo!