Let's begin by scaffolding a new Laravel app, and pulling in the necessary dependencies to begin broadcasting server-side events to the client.
Now that server-side events are properly being sent to Pusher, let's tackle the next layer of the onion. Using Laravel Echo (and the
pusher-js library), we'll open a channel and listen for all relevant messages. If one comes through, we can immediately alert the user. This way, the user can be notified of important changes, without needing to manually refresh the page.
Now that we understand the basic flow for broadcasting an event, let's make things a bit more visual. Imagine that you're working on a task list with a friend or spouse located anywhere in the world. Wouldn't it be nice if, each time you add a new task, they will be notified immediately? This way, you can both flesh out your project in real-time, without needing to constantly refresh your respective browsers to review any updates.
As a basic rule, if the event you're broadcasting is sensitive in nature (order details, direct messaging, etc.), then you should reach for a
PrivateChannel. This way, you can assign special authorization rules for the message. Let's review why this is so important.
Now that we understand what the problem is, we can implement a solution. In this episode, we'll switch to using a private channel. As part of this process, we can also specify any number of broadcasting authorization rules within
From time to time you'll want to trigger an event that is dispersed to all fellow clients connected to your private Pusher channel, without going through your backend server. A common example of this is the "JohnDoe is typing..." feedback you might receive when a friend is typing a text message reply. Let's review exactly how to allow for such functionality in this episode.
Presence channels build upon private channels, by providing extra awareness functionality. The most common example of this is a basic chat room. If we need to track all users, detect when new people join, or when others exit the room, a presence channel is precisely what you need. Let's add a similar feature to our project view.