# Realtime Notifications (Doctor Vue)

Broadcast events for doctor/admin (via Laravel Echo + Reverb):

- Channel (private): `private-admin.{adminId}` (subscribe using Echo: `echo.private('admin.{adminId}')`)
- Events:
  - `notification.created`
    - Payload fields:
      - `id`, `type`, `type_label`
      - `title`, `body`, `icon_url`
      - `data`, `is_read`, `read_at`, `created_at`
  - `notification.read`
    - Payload fields: `notification_id`, `notification_ids`, `all`, `read_at`

---

## 1) Install

```bash
npm i laravel-echo pusher-js
```

## 2) Configure Echo (Reverb)

```js
// e.g. in resources/js/echo.js
import Echo from 'laravel-echo';
import Pusher from 'pusher-js';

window.Pusher = Pusher;

export function makeEcho({ token }) {
  return new Echo({
    broadcaster: 'reverb',
    key: import.meta.env.VITE_REVERB_APP_KEY, // backend: REVERB_APP_KEY
    wsHost: import.meta.env.VITE_REVERB_HOST, // backend: REVERB_HOST
    wsPort: Number(import.meta.env.VITE_REVERB_PORT), // backend: REVERB_PORT
    wssPort: Number(import.meta.env.VITE_REVERB_PORT),
    forceTLS: import.meta.env.VITE_REVERB_SCHEME === 'https', // backend: REVERB_SCHEME
    enabledTransports: ['ws', 'wss'],
    authEndpoint: '/broadcasting/auth',
    auth: {
      headers: {
        Authorization: `Bearer ${token}`,
        Accept: 'application/json',
      },
    },
  });
}
```

**Connection details (backend defaults):**

- Host/port/scheme come from `.env` and are used by Reverb client settings:
  - `REVERB_HOST`
  - `REVERB_PORT` (default is `443`; if you use `REVERB_SCHEME=http`, set it to `8080`)
  - `REVERB_SCHEME` (`http` or `https`)

---

## 3) Subscribe + listen

```js
// user side
const echo = makeEcho({ token });
const userId = 4;

// Note: the Sanctum token must belong to this userId (channel auth enforces it).
echo.private(`user.${userId}`)
  .listen('.notification.created', (payload) => {
    console.log('notification.created', payload);
  })
  .listen('.notification.read', (payload) => {
    console.log('notification.read', payload);
  });
```

```js
// doctor/admin side
const echo = makeEcho({ token });
const adminId = 10;

echo.private(`admin.${adminId}`)
  .listen('.notification.created', (payload) => {
    console.log('notification.created', payload);
  });
```

---

## 4) Mark notifications as read (optional, REST)

- Unread count: `GET /admin/doctor/notifications/unread-count`
- Mark single: `PATCH /admin/doctor/notifications/{notification}/read`
- Mark all: `PATCH /admin/doctor/notifications/read-all`

