SSE Notification Dropdown Component Documentation
1. Overview
This document provides instructions for developers on how to implement and use the real-time SSE (Server-Sent Events) notification dropdown component. The component is built with Vue.js 2 and is designed to work with a Laravel backend. It supports two real-time update methods: a simple Database Polling stream and a high-performance Redis Pub/Sub stream.Key Features
- Real-Time Updates: Pushes notifications from the server to the client without page reloads.
- Dual Backend Support: Choose between simple polling or high-performance Redis.
- Highly Customizable: Uses props and slots for easy customization of appearance and behavior.
- Efficient: Far more resource-friendly than traditional client-side polling (e.g.,
setInterval). - Standalone: Manages its own state, including unread counts and notification lists.
2. Frontend Usage (Vue Component)

NotificationDropdown.vue component into a parent Vue component or Blade file.
2.1. Prerequisites
Ensure you haveaxios installed in your project for the “Mark all as read” functionality.
2.2. Component Props
| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
title | String | No | 'Notifications' | The title displayed at the top of the dropdown list. |
sseUrl | String | Yes | - | The URL endpoint for the Server-Sent Events stream. |
markReadUrl | String | Yes | - | The URL endpoint for the “Mark all as read” POST request. |
initialNotifications | Array | No | [] | An array of pre-loaded notification objects to display on page load. |
initialUnreadCount | Number | No | 0 | The initial number of unread notifications to display on the badge. |
2.3. Component Slots
a. anchor
This slot allows you to customize the trigger icon/button for the dropdown.
Example:
b. item
This slot provides full control over how each notification in the list is rendered. It receives the notification object as a scoped property.
Example:
2.4. Complete Parent Component Example
Here is how you would set up a parent component (or a Blade file with a Vue instance) to use the notification dropdown.a. Blade File (dashboard.blade.php)
Note: The database queries are written to support both standard numeric/UUID user IDs and MongoDBObjectIds. You can simplify this to->where('notifiable_id', $userId)if you are not using MongoDB.
3. Backend Setup (Laravel)
The backend provides the SSE stream and the endpoint to mark notifications as read.3.1. Shared Components (Required for Both Methods)
a. Notification Resource
This resource standardizes the JSON format for notifications sent to the frontend.app/Http/Resources/NotificationResource.php
b. NotificationController & Routes
This controller contains the logic for the SSE streams and marking notifications as read.
routes/web.php
3.2. Method 1: Database Polling Stream (Simple)
This method queries the database every few seconds for new notifications.a. Controller Method
app/Http/Controllers/NotificationController.php
b. Mark All Read Method
app/Http/Controllers/NotificationController.php
3.3. Method 2: Redis Pub/Sub Stream (Recommended)
This event-driven method is highly efficient and provides instant updates.a. Prerequisites & Configuration
- Install Redis server and the
phpredisextension. - Install Predis:
composer require predis/predis - Configure
.env:
b. Event, Observer, and Channel Auth
- Create Event:
NotificationResource.
app/Events/NotificationCreated.php
- Create Observer:
created method, dispatch the event whenever a new notification is saved to the database.
app/Observers/NotificationObserver.php
- Register Observer: In
app/Providers/EventServiceProvider.php, register the observer in thebootmethod:
- Authorize Channel: In
routes/channels.php, authorize the private channel:
c. Controller Method
app/Http/Controllers/NotificationController.php
3.4. How to Send Notifications (Works for Both Methods)
Simply use Laravel’s standard notification system. The backend setup (either polling or the observer) will automatically handle the rest.4. Server Configuration (Important!)
SSE connections are long-lived and can cause504 Gateway Timeout errors if the server is not configured correctly. You must disable proxy buffering for the SSE routes.
Nginx
Apache
Dokumentasi Komponen Notifikasi Dropdown Berbasis SSE
1. Ringkasan
Dokumen ini menyediakan instruksi bagi developer untuk mengimplementasikan dan menggunakan komponen dropdown notifikasi real-time berbasis SSE (Server-Sent Events). Komponen ini dibangun dengan Vue.js 2 dan dirancang untuk bekerja dengan backend Laravel. Komponen ini mendukung dua metode pembaruan real-time: stream Database Polling yang sederhana dan stream Redis Pub/Sub yang berperforma tinggi.Fitur Utama
- Pembaruan Real-Time: Mendorong notifikasi dari server ke klien tanpa perlu memuat ulang halaman.
- Dukungan Backend Ganda: Pilih antara polling sederhana atau Redis yang berperforma tinggi.
- Sangat Mudah Disesuaikan: Menggunakan props dan slots untuk kustomisasi tampilan dan fungsionalitas.
- Efisien: Jauh lebih hemat sumber daya dibandingkan polling tradisional di sisi klien (misalnya,
setInterval). - Mandiri: Mengelola statusnya sendiri, termasuk jumlah notifikasi yang belum dibaca dan daftar notifikasi.
2. Penggunaan Frontend (Komponen Vue)
Bagian ini membahas cara mengintegrasikan komponenNotificationDropdown.vue ke dalam komponen induk Vue atau file Blade.
2.1. Prasyarat
Pastikan Anda telah menginstalaxios di proyek Anda untuk fungsionalitas “Tandai semua telah dibaca”.
2.2. Properti Komponen (Props)
| Prop | Tipe | Wajib | Default | Deskripsi |
|---|---|---|---|---|
title | String | Tidak | 'Notifications' | Judul yang ditampilkan di bagian atas daftar dropdown. |
sseUrl | String | Ya | - | URL endpoint untuk stream Server-Sent Events. |
markReadUrl | String | Ya | - | URL endpoint untuk request POST “Tandai semua telah dibaca”. |
initialNotifications | Array | Tidak | [] | Array berisi objek notifikasi yang dimuat awal saat halaman dibuka. |
initialUnreadCount | Number | Tidak | 0 | Jumlah awal notifikasi yang belum dibaca untuk ditampilkan pada badge. |
2.3. Slot Komponen
a. anchor
Slot ini memungkinkan Anda untuk mengkustomisasi ikon/tombol pemicu untuk dropdown.
Contoh:
b. item
Slot ini memberikan kontrol penuh atas bagaimana setiap notifikasi dalam daftar ditampilkan. Slot ini menerima objek notification sebagai properti scoped.
Contoh:
2.4. Contoh Lengkap Komponen Induk
Berikut adalah cara menyiapkan komponen induk (atau file Blade dengan instance Vue) untuk menggunakan dropdown notifikasi.a. File Blade (dashboard.blade.php)
Catatan: Query database ditulis untuk mendukung ID pengguna standar (numerik/UUID) danObjectIdMongoDB. Anda dapat menyederhanakannya menjadi->where('notifiable_id', $userId)jika Anda tidak menggunakan MongoDB.
3. Penyiapan Backend (Laravel)
Backend menyediakan stream SSE dan endpoint untuk menandai notifikasi telah dibaca.3.1. Komponen Bersama (Dibutuhkan untuk Kedua Metode)
a. Notification Resource
Resource ini berfungsi untuk menstandarisasi format JSON notifikasi yang dikirim ke frontend.app/Http/Resources/NotificationResource.php
b. NotificationController & Routes
Controller ini berisi logika untuk stream SSE dan menandai notifikasi telah dibaca.
routes/web.php
3.2. Metode 1: Stream Database Polling (Sederhana)
Metode ini melakukan query ke database setiap beberapa detik untuk mencari notifikasi baru.a. Metode Controller
app/Http/Controllers/NotificationController.php
b. Metode Mark All Read
app/Http/Controllers/NotificationController.php
3.3. Metode 2: Stream Redis Pub/Sub (Disarankan)
Metode berbasis event ini sangat efisien dan memberikan pembaruan instan.a. Prasyarat & Konfigurasi
- Instal server Redis dan ekstensi
phpredis. - Install Predis:
composer require predis/predis - Konfigurasi
.env:
b. Event, Observer, dan Otorisasi Channel
- Buat Event:
NotificationResource Anda.
app/Events/NotificationCreated.php
- Buat Observer:
created, panggil event setiap kali notifikasi baru disimpan ke database.
app/Observers/NotificationObserver.php
- Daftarkan Observer: Di
app/Providers/EventServiceProvider.php, daftarkan observer di metodeboot:
- Otorisasi Channel: Di
routes/channels.php, otorisasi private channel:
c. Metode Controller
app/Http/Controllers/NotificationController.php
3.4. Cara Mengirim Notifikasi (Berlaku untuk Kedua Metode)
Cukup gunakan sistem notifikasi standar Laravel. Penyiapan backend (baik polling maupun observer) akan menangani sisanya secara otomatis.4. Konfigurasi Server (Penting!)
Koneksi SSE berjalan lama dan dapat menyebabkan error504 Gateway Timeout jika server tidak dikonfigurasi dengan benar. Anda harus menonaktifkan proxy buffering untuk route SSE.
Nginx
Apache
Addendum A: Testing with Artisan Command
To facilitate development and testing, a custom Artisan command is provided to send a test notification directly to any user.A.1. Setup
a. Test Notification Class
app/Notifications/TestNotification.php
b. Artisan Command
app/Console/Commands/SendTestNotification.php
A.2. Usage
a. Basic Command
b. Command with Custom Options
Adendum A: Pengujian dengan Perintah Artisan
Untuk memfasilitasi pengembangan dan pengujian, disediakan sebuah perintah Artisan kustom untuk mengirim notifikasi tes langsung ke pengguna manapun.A.1. Penyiapan
a. Kelas Notifikasi Tes
app/Notifications/TestNotification.php
b. Perintah Artisan
app/Console/Commands/SendTestNotification.php
