Professional Documents
Culture Documents
LARAVEL APP
Olayinka Omole May 10th, 2018
You will need PHP and Laravel (5.4 or above) installed on your machine. You should have a working
knowledge of PHP and JavaScript.
Realtime noti cations are now very common in modern web applications, as site owners want to keep
users engaged and informed of happenings on their platform. Noti cations are also a great way to build
addiction, and make sure users keep coming back to your platform to see "what's new".
With Laravel and some Pusher magic, I will be demonstrating how to build a realtime app, with desktop
noti cations, utilising the Noti cation API.
Our application
We will build a simple News noti cation module which will alert everyone on a website when a new post
is published. Ideally, this would be part of a larger app, but we will build it in isolation here to showcase
our desktop noti cations.
At the end of this tutorial, you will have learned how to:
To follow along, you will need a working knowledge of PHP and JavaScript. Basic knowledge of Laravel
and Vue.js are also needed. Laravel 5.4 and Vue.js 2.3 are used.
Introduction to Pusher
Pusher is a service that makes it super easy to add realtime functionality to web and mobile applications.
We will be using it in our application, so sign up to a free account here, create an app, and copy out the
app credentials (App ID, Key and Secret) from the “App Keys” section.
Setup and con guration
For starters, let us set up our app, and do the necessary con guration. We will call the app news-talk .
To create a new app with the Laravel installer, run this command:
The next set of commands should be run in the app's root directory.
Installing the Laravel Frontend Dependencies (these include Bootstrap, Axios, Vue.js and a couple of
other things which are nice to have):
npm install
Installing Laravel Echo and Pusher-js which we will use to listen for broadcast events:
Next, we will do some more minor con guration to let Laravel know we will be using Pusher to manage
our broadcasts.
# ./.env
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=your_pusher_add_id
PUSHER_APP_KEY=your_pusher_app_key
PUSHER_APP_SECRET=your_pusher_app_secret
You can edit some more optional con guration for Pusher in the ./config/broadcasting.php le
generated by Laravel. You can see other options here.
Note: If you created your app in a different cluster to the default us-east-1 , you must
con gure the cluster option. It is optional if you chose the default option.
Finally, we will con gure Echo to use Pusher. We do that by uncommenting and editing the values at the
bottom of resources/assets/js/bootstrap.js :
// ./resources/assets/js/bootstrap.js
The -m and c ags are for automatically generating the migration and controller les respectively.
Next, we edit the generated migration le located in the ./database/migrations folder. We adjust the up
Then, after editing the .env with your database details, you can create the table with this command:
# ./app/Post.php
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['title', 'description'];
}
Saving a post
Next, we will add a route and controller method to save a new post.
We will be making an API call from the front-end to save our posts, so we can add the new route to the
API routes at ./routes/api.php . Whatever route de ned here will be pre xed by api , and belongs to the
api middleware group.
# ./routes/api
Route::post('/post', 'PostController@store');
# ./app/Http/Controllers/PostController.php
use App\Post;
/**
* Saves a new post to the database
*/
public function store(Request $request) {
// ...
// validation can be done here before saving
// with $this->validate($request, $rules)
// ...
Laravel allows for easy de nition of events and listeners out of the box. It also includes helper functions
and classes to allow us easily trigger and broadcast events.
# ./app/Events/PostPublished.php
public $post;
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn() {
return new Channel('posts');
}
The broadcastOn method returns the channel that we want to broadcast our event on. The Channel class
is used for broadcasting on public channels. PrivateChannel and PresenceChannel are for private
channels (these require authentication for access). You can read more about the various Pusher
channels here.
By default, Laravel broadcasts all of an event class’ public properties as its payload… broadcastWith
helps us override that behaviour and choose what we want to send.
Dispatching events
In our app, we want to dispatch the PostPublished event after a post has been saved. In Laravel, we can
dispatch events using the Event Facade, or the event() helper function.
To dispatch our PostPublished event, we can edit the store method in the PostController , and place
the event call right after the post is saved:
# ./app/Http/Controllers/PostController.php
use App\Events\PostPublished;
// save post and assign return value of created post to $post array
$post = Post::create($data);
# ./app/Http/Controllers/PostController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Events\PostPublished;
use App\Post;
/**
/
* Saves a new post to the database
*/
public function store(Request $request) {
// ...
// validation can be done here before saving
// with $this->validate($request, $rules)
// ...
// save post and assign return value of created post to $post array
$post = Post::create($data);
Now that we are done with building the backend, we can proceed to create our view and event listener
on the Frontend.
<!DOCTYPE html>
<html lang="{{ config('app.locale') }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>News Talk</title>
<style>
.container {
.container {
padding-top: 100px;
}
</style>
<div id="app">
<!-- home Vue component -->
<home></home>
</div>
Most of the code above is boilerplate Laravel HTML content with relevant scripts and CSS les attached.
We will generate them later on.
We also included a Vue component ( home ) which hasn't been de ned yet. Let us go ahead to create and
de ne it.
i t
<script>
export default {
data() {
return {
newPostTitle: "",
newPostDesc: ""
}
},
created() {
this.listenForChanges();
},
methods: {
addPost(postName, postDesc) {
// check if entries are not empty
if(!postName || !postDesc)
return;
In the above code, we de ne two methods. addPost() and listenForChanges . The addPost method
makes a post request to our API with the required payload when a user adds a new post.
In the listenForChanges method we use Echo to subscribe to the posts channel which is the channel
In the listenForChanges method, we use Echo to subscribe to the posts channel, which is the channel
we are broadcasting to, from our backend. We also listen for PostPublished events, and de ne a callback
that activates our desktop noti cation whenever an event is red.
We are using the noti cations API for desktop noti cations. We rst request permission to send desktop
noti cations, then notify the user once permission is granted.
We can also check if a browser supports desktop noti cations this way:
if (window.Notification) {
console.log('Notifications are supported!');
} else {
alert('Notifications aren\'t supported on your browser! :(');
}
We create an instance of the Noti cation object with our post title as the body. An optional icon
// ./resources/assets/js/app.js
Vue.component('home', require('./components/Home.vue'));
Now, we can navigate to the app's homepage to see it in action. If you use Laravel Valet, you can also
share the app, and visit it via another device to test the desktop noti cations.
Conclusion
We have learned how to build an event-driven basic realtime app enabled with desktop noti cations,
thanks to Laravel and Pusher. As a next step, you could also learn how to build a chat application with
Pusher, and integrate desktop noti cations whenever a user receives a message... awesome, right?
If you’ve thought of any other great ways to use Pusher and Laravel, let us know in the comments!
The entire code for this tutorial is hosted on Github. You can look through and ask questions if you need
more information.
CHANNELS
Products Developers
Channels Docs
Beams Tutorials
Status
Support
Sessions
Company Connect
Contact Sales Twitter
Customer stories Medium
Security LinkedIn
Careers GitHub
Blog
Pusher Limited is a company registered in England and Wales (No. 07489873) whose registered of ce is at 160 Old Street, London, EC1V
9BW.