Backend Development 6 min read

Integrating Pusher Real-Time Notifications into a Laravel Application

This guide explains how to set up real-time notifications in a Laravel application using Pusher, covering prerequisites, project setup, package installation, configuration of .env, creating events, broadcasting, client-side listening with Laravel Echo, testing, and advanced customization features.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Integrating Pusher Real-Time Notifications into a Laravel Application

In today's fast‑moving digital world, real‑time updates are essential for user experience, and integrating Pusher with Laravel provides a straightforward way to add dynamic notifications.

Prerequisites

Basic knowledge of Laravel, Composer installed, and a free Pusher account are required.

Set up your Laravel project

If you don't have a project, create one with Composer:

composer create-project --prefer-dist laravel/laravel laravelRealTime
cd laravelRealTime

Install required packages

Install the Pusher PHP SDK:

composer require pusher/pusher-php-server

Configure Pusher in the application

Obtain your Pusher credentials (app_id, key, secret, cluster) and add them to the .env file:

PUSHER_APP_ID=your_app_id
PUSHER_APP_KEY=your_key
PUSHER_APP_SECRET=your_secret
PUSHER_APP_CLUSTER=your_cluster

After editing .env , run php artisan config:cache to refresh the configuration.

Build real‑time notifications

Create an event that will be broadcast:

php artisan make:event OrderStatusUpdated

Define the broadcast channel in the event class:

// App/Events/OrderStatusUpdated.php
public function broadcastOn()
{
    return new Channel('orders');
}

Listen for the event on the client using Laravel Echo and Pusher JS:

<script src="https://js.pusher.com/7.0/pusher.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/laravel-echo/dist/echo.js"></script>

<script>
Echo = new Echo({
    broadcaster: 'pusher',
    key: 'your_key',
    cluster: 'your_cluster',
    encrypted: true
});

Echo.channel('orders')
    .listen('OrderStatusUpdated', e => {
        console.log('订单状态已更新: ', e);
    });
</script>

Test the real‑time system

Add a route to trigger the event:

// routes/web.php
Route::get('/test-event', function () {
    event(new App\Events\OrderStatusUpdated('您的订单已发货!'));
    return "事件已发送!";
});

Visit the route and observe the notification in the browser console.

Advanced features and customization

Pusher offers private channels, presence channels, and custom payloads to enhance interactivity and security.

Conclusion

Following these steps integrates Pusher real‑time notifications into Laravel, improving responsiveness, user experience, flexibility, and security.

Real-timeweb developmentphpNotificationsLaravelPusher
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.