# Subscribe to Planet Events

Subscribe to real-time events for a specific planet

Real-time events related to specific planets. Subscribe to get notified about:

* Planet creation and updates
* Atmospheric changes
* Discovery of new satellites
* Cosmic events (explosions, collisions, etc.)
* Image uploads and media updates


Scalar Galaxy Events sends these messages to you.

Address: `planets/{planetId}/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

The operation takes one of:

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

## Address Parameters

- `planetId`: The ID of the planet.

## Query Parameters

The query of connection URL: `SubscribeToPlanetEventsQuery`

## Messages

### Planet Created Event

A new planet has been discovered or created

This event is published when a new planet is added to the Scalar Galaxy.

Event Flow:
1. Planet creation request via REST API
2. Planet validation and storage
3. This event published to subscribers
4. Optional webhook callbacks triggered


Content type: `application/json`

Payload: `PlanetCreatedPayload`

Example payload:

```json
{
  "eventId": "evt_1234567890",
  "eventType": "planet.created",
  "timestamp": "2024-01-15T14:30:00Z",
  "planet": {
    "id": 4,
    "name": "Mars",
    "description": "The red planet",
    "type": "terrestrial",
    "habitabilityIndex": 0.68,
    "physicalProperties": {
      "mass": 0.107,
      "radius": 0.532,
      "gravity": 0.378
    },
    "atmosphere": [
      {
        "compound": "CO2",
        "percentage": 95.3
      }
    ],
    "discoveredAt": "1610-01-07T00:00:00Z",
    "creator": {
      "id": 1,
      "name": "Marc"
    }
  },
  "metadata": {
    "source": "rest-api",
    "userId": 1,
    "requestId": "req_abc123"
  }
}
```

### Planet Updated Event

An existing planet has been modified

Published when planet properties are updated via the REST API

Content type: `application/json`

Payload: `PlanetUpdatedPayload`

Example payload:

```json
{
  "eventId": "evt_1234567891",
  "eventType": "planet.updated",
  "timestamp": "2024-01-15T15:45:00Z",
  "planet": {
    "id": 4,
    "name": "Mars",
    "atmosphere": [
      {
        "compound": "CO2",
        "percentage": 95.3
      },
      {
        "compound": "N2",
        "percentage": 2.7
      }
    ]
  },
  "changes": [
    {
      "field": "atmosphere",
      "oldValue": [
        {
          "compound": "CO2",
          "percentage": 95.3
        }
      ],
      "newValue": [
        {
          "compound": "CO2",
          "percentage": 95.3
        },
        {
          "compound": "N2",
          "percentage": 2.7
        }
      ]
    }
  ],
  "metadata": {
    "source": "rest-api",
    "userId": 1,
    "requestId": "req_def456"
  }
}
```

### Planet Deleted Event

A planet has been removed from the galaxy

⚠️ Warning: This event indicates a planet has been deleted.

This is a destructive operation that may affect related celestial bodies and data.


Content type: `application/json`

Payload: `PlanetDeletedPayload`

Example payload:

```json
{
  "eventId": "evt_1234567892",
  "eventType": "planet.deleted",
  "timestamp": "2024-01-15T16:00:00Z",
  "planetId": 999,
  "planetName": "Test Planet",
  "reason": "Experimental deletion",
  "metadata": {
    "source": "rest-api",
    "userId": 1,
    "requestId": "req_ghi789"
  }
}
```

### Planet Exploded Event

A planet has experienced a cosmic explosion

🚨 Cosmic Event: A planet has exploded due to natural or artificial causes.

This is a rare but dramatic event that may create new celestial bodies or affect nearby objects.


Content type: `application/json`

Payload: `PlanetExplodedPayload`

Example payload:

```json
{
  "eventId": "evt_1234567893",
  "eventType": "planet.exploded",
  "timestamp": "2024-01-15T16:30:00Z",
  "planet": {
    "id": 42,
    "name": "Krypton",
    "type": "super_earth"
  },
  "explosion": {
    "type": "supernova",
    "intensity": 9.8,
    "energyReleased": "1.2e44",
    "newCelestialBodies": [
      {
        "type": "asteroid",
        "name": "Krypton Fragment Alpha"
      },
      {
        "type": "asteroid",
        "name": "Krypton Fragment Beta"
      }
    ]
  },
  "affectedObjects": [
    {
      "planetId": 41,
      "distance": 1500000,
      "impact": "moderate"
    }
  ],
  "metadata": {
    "source": "cosmic-simulator",
    "severity": "critical"
  }
}
```

### Satellite Discovered Event

A new satellite has been discovered orbiting a planet

Published when a new moon, asteroid, or other satellite is found

Content type: `application/json`

Payload: `SatelliteDiscoveredPayload`

Example payload:

```json
{
  "eventId": "evt_1234567894",
  "eventType": "satellite.discovered",
  "timestamp": "2024-01-15T17:00:00Z",
  "satellite": {
    "id": 15,
    "name": "Deimos II",
    "type": "moon",
    "diameter": 12.4,
    "orbit": {
      "planet": {
        "id": 4,
        "name": "Mars"
      },
      "orbitalPeriod": 1.26,
      "distance": 23460
    }
  },
  "discoveryMethod": "telescope_observation",
  "metadata": {
    "source": "astronomical-survey",
    "observerId": 2
  }
}
```

### Atmosphere Changed Event

A planet's atmosphere composition has changed

Published when atmospheric properties are modified

Content type: `application/json`

Payload: `AtmosphereChangedPayload`

Example payload:

```json
{
  "eventId": "evt_1234567895",
  "eventType": "atmosphere.changed",
  "timestamp": "2024-01-15T17:30:00Z",
  "planet": {
    "id": 4,
    "name": "Mars"
  },
  "atmosphere": [
    {
      "compound": "CO2",
      "percentage": 95.3
    },
    {
      "compound": "O2",
      "percentage": 2.1
    },
    {
      "compound": "N2",
      "percentage": 2.6
    }
  ],
  "change": {
    "type": "terraforming",
    "progress": 0.15,
    "estimatedCompletion": "2050-12-31T00:00:00Z"
  },
  "metadata": {
    "source": "terraforming-station",
    "stationId": "mars-alpha-1"
  }
}
```

### Image Uploaded Event

A new image has been uploaded for a planet

Published when a planet image is successfully uploaded

Content type: `application/json`

Payload: `ImageUploadedPayload`

Example payload:

```json
{
  "eventId": "evt_1234567896",
  "eventType": "image.uploaded",
  "timestamp": "2024-01-15T18:00:00Z",
  "planet": {
    "id": 4,
    "name": "Mars"
  },
  "image": {
    "url": "https://cdn.scalar.com/images/mars-latest.jpg",
    "fileSize": 2048576,
    "mimeType": "image/jpeg",
    "uploadedAt": "2024-01-15T18:00:00Z"
  },
  "uploader": {
    "id": 1,
    "name": "Marc"
  },
  "metadata": {
    "source": "rest-api",
    "requestId": "req_jkl012"
  }
}
```

## Types

```ts
export type SubscribeToPlanetEventsMessage =
  | PlanetCreatedPayload
  | PlanetUpdatedPayload
  | PlanetDeletedPayload
  | PlanetExplodedPayload
  | SatelliteDiscoveredPayload
  | AtmosphereChangedPayload
  | ImageUploadedPayload;

export interface SubscribeToPlanetEventsQuery {
  /**
   * Include historical events
   *
   * @default false
   */
  includeHistory?: boolean;

  /** Filter by specific event types */
  eventTypes?: string[];
}

export interface PlanetCreatedPayload {
  /**
   * Unique identifier for this event
   *
   * @example "evt_1234567890"
   */
  eventId: string;

  /** Type of event */
  eventType: "planet.created";

  /**
   * When the event occurred
   *
   * @example "2024-01-15T14:30:00Z"
   */
  timestamp: string;
  planet: Planet;
  metadata?: EventMetadata;
}

export interface PlanetUpdatedPayload {
  eventId: string;
  eventType: "planet.updated";
  timestamp: string;
  planet: Planet;

  /** List of fields that were modified */
  changes: FieldChange[];
  metadata?: EventMetadata;
}

export interface PlanetDeletedPayload {
  eventId: string;
  eventType: "planet.deleted";
  timestamp: string;
  planetId: number;
  planetName: string;

  /** Reason for deletion */
  reason?: string;
  metadata?: EventMetadata;
}

export interface PlanetExplodedPayload {
  eventId: string;
  eventType: "planet.exploded";
  timestamp: string;
  planet: Planet;
  explosion: {
    type: "supernova" | "nova" | "collision" | "artificial";

    /** Explosion intensity on a scale of 0-10 */
    intensity: number;

    /** Energy released in joules (scientific notation) */
    energyReleased?: string;
    newCelestialBodies?: {
      type?: "asteroid" | "comet" | "planet";
      name?: string;
    }[];
  };
  affectedObjects?: {
    planetId?: number;

    /** Distance in kilometers */
    distance?: number;
    impact?: "none" | "minor" | "moderate" | "severe";
  }[];
  metadata?: EventMetadata;
}

export interface SatelliteDiscoveredPayload {
  eventId: string;
  eventType: "satellite.discovered";
  timestamp: string;
  satellite: Satellite;
  discoveryMethod?: "telescope_observation" | "space_probe" | "mathematical_prediction" | "collision_remnant";
  metadata?: EventMetadata;
}

export interface AtmosphereChangedPayload {
  eventId: string;
  eventType: "atmosphere.changed";
  timestamp: string;
  planet: {
    id?: number;
    name?: string;
  };
  atmosphere: {
    compound?: string;
    percentage?: number;
  }[];
  change?: {
    type?: "terraforming" | "natural" | "pollution" | "atmospheric_loss";

    /** Progress of the change (0-1) */
    progress?: number;
    estimatedCompletion?: string;
  };
  metadata?: EventMetadata;
}

export interface ImageUploadedPayload {
  eventId: string;
  eventType: "image.uploaded";
  timestamp: string;
  planet: {
    id?: number;
    name?: string;
  };
  image: {
    url: string;

    /** File size in bytes */
    fileSize: number;
    mimeType: string;
    uploadedAt: string;
  };
  uploader?: User;
  metadata?: EventMetadata;
}

/** A planet in the Scalar Galaxy */
export interface Planet {
  readonly id: number;
  name: string;
  description?: string;
  type?: "terrestrial" | "gas_giant" | "ice_giant" | "dwarf" | "super_earth";
  habitabilityIndex?: number;
  physicalProperties?: {
    mass?: number;
    radius?: number;
    gravity?: number;
    temperature?: {
      min?: number;
      max?: number;
      average?: number;
    };
  };
  atmosphere?: {
    compound?: string;
    percentage?: number;
  }[];
  discoveredAt?: string;
  image?: string;
  satellites?: Satellite[];
  creator?: User;
  tags?: string[];
  readonly lastUpdated?: string;
}

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";
}

export interface FieldChange {
  /** Name of the field that changed */
  field: string;

  /** Previous value of the field */
  oldValue: unknown;

  /** New value of the field */
  newValue: unknown;
}

/** Every satellite in the Scalar Galaxy */
export interface Satellite {
  readonly id?: number;
  name: string;
  description?: string;
  diameter?: number;
  type?: "moon" | "asteroid" | "comet";
  orbit?: {
    planet?: Planet;
    orbitalPeriod?: number;
    distance?: number;
  };
}

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