Skip to main content

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)

SSE Notification Dropdown This section covers how to integrate the NotificationDropdown.vue component into a parent Vue component or Blade file.

2.1. Prerequisites

Ensure you have axios installed in your project for the “Mark all as read” functionality.

2.2. Component Props

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 MongoDB ObjectIds. 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
This event-driven method is highly efficient and provides instant updates.

a. Prerequisites & Configuration

  1. Install Redis server and the phpredis extension.
  2. Install Predis: composer require predis/predis
  3. Configure .env:

b. Event, Observer, and Channel Auth

  1. Create Event:
Configure the event to format the broadcast payload using your NotificationResource. app/Events/NotificationCreated.php
  1. Create Observer:
In the created method, dispatch the event whenever a new notification is saved to the database. app/Observers/NotificationObserver.php
  1. Register Observer: In app/Providers/EventServiceProvider.php, register the observer in the boot method:
  1. 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 cause 504 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 komponen NotificationDropdown.vue ke dalam komponen induk Vue atau file Blade.

2.1. Prasyarat

Pastikan Anda telah menginstal axios di proyek Anda untuk fungsionalitas “Tandai semua telah dibaca”.

2.2. Properti Komponen (Props)

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) dan ObjectId MongoDB. 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

  1. Instal server Redis dan ekstensi phpredis.
  2. Install Predis: composer require predis/predis
  3. Konfigurasi .env:

b. Event, Observer, dan Otorisasi Channel

  1. Buat Event:
Konfigurasikan event untuk memformat payload broadcast menggunakan NotificationResource Anda. app/Events/NotificationCreated.php
  1. Buat Observer:
Di metode created, panggil event setiap kali notifikasi baru disimpan ke database. app/Observers/NotificationObserver.php
  1. Daftarkan Observer: Di app/Providers/EventServiceProvider.php, daftarkan observer di metode boot:
  1. 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 error 504 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

A.2. Cara Penggunaan

a. Perintah Dasar
b. Perintah dengan Opsi Kustom