So we're going to build real-time notifications into a Laravel application by creating a controller dedicated to triggering notifications and a view to allow users to create those notifications.
But first, lets' review some of the key Pusher concepts.
Three fundamental concepts in Pusher are: channels, events and event data.
Channels are used to identify the context of data within your application; this frequently maps to data being stored in a database. Since we're building a notification feature it'll make sense to name our channel notifications
.
If Channels identify the data then Events represent something happening to that data. If we think about databases this could be any CRUD (Create, Read, Update, Delete) event. In the context of notifications this could mean:
For our feature let's give the event a name of new-notification
. We'll use this instead of created
as it provides better semantics.
Each event has an Event Data payload. In our simple use case we're going to supply just the text for the notification. But in a more real world use case you would also likely provide information that can be used to allow the user to perform some sort of action on the notification e.g. click through to view more detailed information.
So, our data may look like:
$data = array('text' => 'this is a notification');
Or even:
class Notification
{
public $text;
public function __construct($text)
{
$this->text = $text;
}
}
$data = new Notification('this is a notification');
The Pusher PHP library (and all other libraries) will handles serialising and deserialising data. In both cases above, triggering an event with the $data
event payload will result in the same information being sent through Pusher and received by the client. The JSON representation of the above event data will look as follows:
{"text": "this is a notification"}
Now that you've an overview of channels, events and event data the following code will make much more sense to you:
$pusher->trigger('notifications',
'new-notification',
['text' => 'this is a notification']);
It's time to trigger a notification events.