> For the complete documentation index, see [llms.txt](https://docs.moodymusic.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.moodymusic.xyz/api-reference/real-time-events.md).

# Real-Time Events

Moody Music uses two real-time communication channels: **Server-Sent Events (SSE)** for admin session management and **Redis Pub/Sub** for cross-service event propagation.

## Server-Sent Events (SSE)

### GET /api/admin/sse/:token

Opens an SSE stream for real-time session revocation notifications.

**Headers:** Standard SSE headers (`text/event-stream`, `no-cache`)

**Events:**

| Event Type        | Data | Description                                    |
| ----------------- | ---- | ---------------------------------------------- |
| `CONNECTED`       | `{}` | Initial connection confirmation                |
| `HEARTBEAT`       | `{}` | Sent every 30 seconds to keep connection alive |
| `SESSION_REVOKED` | `{}` | Sent when an admin revokes this session        |
| `SESSION_EXPIRED` | `{}` | Sent when the session expires                  |

### Usage

```javascript
const events = new EventSource('/api/admin/sse/' + token);
events.addEventListener('SESSION_REVOKED', () => {
  window.location.href = '/expired';
});
```

## Redis Pub/Sub

The backend publishes events to the `events:settings_update` Redis channel, which the bot subscribes to.

### Published Events

**USER\_PREMIUM\_ADD** — When a user purchases or is granted premium:

```json
{
  "type": "USER_PREMIUM_ADD",
  "userId": "123456789012345678",
  "username": "User Name",
  "durationDays": 30,
  "planType": "Monthly",
  "totalSlots": 1
}
```

**USER\_PREMIUM\_REVOKE** — When user premium is revoked:

```json
{
  "type": "USER_PREMIUM_REVOKE",
  "userId": "123456789012345678"
}
```

**SERVER\_PREMIUM\_ADD** — When server premium is activated:

```json
{
  "type": "SERVER_PREMIUM_ADD",
  "guildId": "123456789012345678",
  "userId": "123456789012345678",
  "durationDays": 30,
  "planType": "Monthly"
}
```

**SERVER\_PREMIUM\_REVOKE** — When server premium is deactivated:

```json
{
  "type": "SERVER_PREMIUM_REVOKE",
  "guildId": "123456789012345678"
}
```

**247\_UPDATE** — When 24/7 mode is toggled:

```json
{
  "type": "247_UPDATE",
  "guildId": "123456789012345678",
  "is247": true
}
```
