Reverb has many potential use cases, one of them being presence channels. They allow us to see who's online in real time. For example, we can show a list of people viewing the page/document:
This tutorial will take a simple "Document view" page and add a list of users viewing the page, updating it in real-time whenever someone joins/leaves the page. We'll use Reverb's presence channels to do this.
Our current project is really simple. We have a Document
model and a DocumentController
with a show()
method. This method returns a view with the document's content.
app/Http/Controllers/DocumentController.php
public function show(Document $document){ return view('documents.show', compact('document'));}
This returns a simple view with the document's content:
resources/views/documents/show.blade.php
<x-app-layout> {{-- ... --}} <div class="py-12"> <div class="max-w-7xl mx-auto sm:px-6 lg:px-8"> <div class="bg-white overflow-hidden shadow-xl sm:rounded-lg"> <div class="p-6 sm:px-20 bg-white border-b border-gray-200"> <div class="text-2xl"> {{ $document->title }} </div> <div class="mt-6 text-gray-500"> {{ $document->description }} </div> </div> </div> </div> </div></x-app-layout>
This gives us a very simple page with the document's title and description:
We want to add a list of users currently viewing the document. This list needs to be updated in real-time as people enter and leave the page. Here's exactly what we want:
So, let's start by installing Reverb.
To install Reverb, we have to call this command:
php artisan install:broadcasting
This will ask us if we want to install Reverb, enter yes
and press Enter
.
Once the package installs - it will ask if we want Node dependencies to be installed. Enter yes
and press Enter
.
That's it! Reverb is installed and ready to be used. No more configuration is needed at this point.
Next, we need to implement channels in our application. This is done by...