# Subscribe to System Events

Subscribe to system-wide notifications and alerts

System-wide events including maintenance notifications, rate limit alerts, and security events.

These events are broadcast to all connected clients and don't require specific subscriptions.


Scalar Galaxy Events sends these messages to you.

Address: `system/events`

## Servers

- `wss://galaxy.scalar.com` (Production, wss)
- `ws://localhost:8080` (Development, ws)
- `kafka://kafka.galaxy.scalar.com:9092` (Event Bus, kafka)
- `mqtt://mqtt.galaxy.scalar.com:1883` (Mqtt Bridge, mqtt)

## Authorization

Connecting to `production` takes one of:

- `bearerAuth` (http bearer): JWT Bearer token authentication for WebSocket connections
- `httpApiKey` (httpApiKey in query `api_key`): API key for WebSocket authentication

## Messages

### System Maintenance Event

System maintenance has been scheduled or is in progress

Published for planned and emergency maintenance events

Content type: `application/json`

Payload: `SystemMaintenancePayload`

Example payload:

```json
{
  "eventId": "evt_1234567902",
  "eventType": "system.maintenance",
  "timestamp": "2024-01-15T21:00:00Z",
  "maintenance": {
    "type": "scheduled",
    "status": "starting",
    "duration": "PT2H",
    "description": "Database optimization and security updates",
    "affectedServices": [
      "api",
      "events",
      "storage"
    ]
  },
  "schedule": {
    "startTime": "2024-01-15T21:00:00Z",
    "endTime": "2024-01-15T23:00:00Z"
  },
  "metadata": {
    "source": "system-monitor",
    "maintenanceId": "maint_20240115"
  }
}
```

### Rate Limit Exceeded Event

A user or system has exceeded rate limits

Published when rate limits are exceeded

Content type: `application/json`

Payload: `RateLimitExceededPayload`

Example payload:

```json
{
  "eventId": "evt_1234567903",
  "eventType": "rate_limit.exceeded",
  "timestamp": "2024-01-15T21:30:00Z",
  "rateLimit": {
    "limit": 1000,
    "window": "PT1H",
    "exceededBy": 50
  },
  "user": {
    "id": 1,
    "name": "Marc"
  },
  "request": {
    "endpoint": "/planets",
    "method": "GET",
    "ipAddress": "192.168.1.100"
  },
  "metadata": {
    "source": "rate-limiter",
    "requestId": "req_bcd890"
  }
}
```

### Security Alert Event

A security-related event has been detected

Published for security incidents and suspicious activity

Content type: `application/json`

Payload: `SecurityAlertPayload`

Example payload:

```json
{
  "eventId": "evt_1234567904",
  "eventType": "security.alert",
  "timestamp": "2024-01-15T22:00:00Z",
  "alert": {
    "severity": "medium",
    "type": "suspicious_login_pattern",
    "description": "Multiple failed login attempts from different IP addresses"
  },
  "activity": {
    "attempts": 15,
    "timeWindow": "PT5M",
    "ipAddresses": [
      "192.168.1.100",
      "192.168.1.101",
      "10.0.0.50"
    ]
  },
  "actions": [
    "account_temporarily_locked",
    "admin_notified"
  ],
  "metadata": {
    "source": "security-monitor",
    "alertId": "alert_sec_001"
  }
}
```

### API Version Deprecated Event

An API version has been deprecated

Published when API versions are marked as deprecated

Content type: `application/json`

Payload: `ApiVersionDeprecatedPayload`

Example payload:

```json
{
  "eventId": "evt_1234567905",
  "eventType": "api.version_deprecated",
  "timestamp": "2024-01-15T22:30:00Z",
  "deprecation": {
    "version": "1.0",
    "deprecatedAt": "2024-01-15T22:30:00Z",
    "sunsetAt": "2024-07-15T22:30:00Z",
    "migrationGuide": "https://docs.scalar.com/migration/v1-to-v2"
  },
  "impact": {
    "affectedEndpoints": [
      "/planets",
      "/users"
    ],
    "breakingChanges": true
  },
  "metadata": {
    "source": "api-gateway",
    "deprecationId": "dep_20240115"
  }
}
```

## Types

```ts
export type SubscribeToSystemEventsMessage =
  | SystemMaintenancePayload
  | RateLimitExceededPayload
  | SecurityAlertPayload
  | ApiVersionDeprecatedPayload;

export interface SystemMaintenancePayload {
  eventId: string;
  eventType: "system.maintenance";
  timestamp: string;
  maintenance: {
    type: "scheduled" | "emergency" | "hotfix";
    status: "scheduled" | "starting" | "in_progress" | "completed" | "failed";

    /** Expected duration in ISO 8601 format */
    duration: string;
    description?: string;
    affectedServices?: string[];
  };
  schedule?: {
    startTime?: string;
    endTime?: string;
  };
  metadata?: EventMetadata;
}

export interface RateLimitExceededPayload {
  eventId: string;
  eventType: "rate_limit.exceeded";
  timestamp: string;
  rateLimit: {
    limit: number;
    window: string;
    exceededBy: number;
  };
  user?: User;
  request?: {
    endpoint?: string;
    method?: string;
    ipAddress?: string;
  };
  metadata?: EventMetadata;
}

export interface SecurityAlertPayload {
  eventId: string;
  eventType: "security.alert";
  timestamp: string;
  alert: {
    severity: "low" | "medium" | "high" | "critical";
    type: "suspicious_login_pattern" | "brute_force" | "data_breach" | "malware_detected";
    description: string;
  };
  activity?: {
    attempts?: number;
    timeWindow?: string;
    ipAddresses?: string[];
  };
  actions?: string[];
  metadata?: EventMetadata;
}

export interface ApiVersionDeprecatedPayload {
  eventId: string;
  eventType: "api.version_deprecated";
  timestamp: string;
  deprecation: {
    version: string;
    deprecatedAt: string;
    sunsetAt: string;
    migrationGuide?: string;
  };
  impact?: {
    affectedEndpoints?: string[];
    breakingChanges?: boolean;
  };
  metadata?: EventMetadata;
}

export interface EventMetadata {
  /** Source system that generated the event */
  source?: string;

  /** ID of the user who triggered the event (if applicable) */
  userId?: number;

  /** ID of the request that triggered the event */
  requestId?: string;

  /** Correlation ID for tracing related events */
  correlationId?: string;

  /** Event severity level */
  severity?: "info" | "warning" | "error" | "critical";
}

/** A user */
export interface User {
  readonly id?: number;
  name?: string;
}
```
