{
  "id": "php",
  "title": "Rolling sensor graph demo with Redis and PHP",
  "url": "https://redis.io/docs/latest/develop/use-cases/time-series-dashboard/php/",
  "summary": "Build a Redis-backed rolling sensor graph demo in PHP with Predis",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc"
  ],
  "last_updated": "2026-05-12T09:07:59-04:00",
  "children": [],
  "page_type": "content",
  "content_hash": "2b753935c2d63a6a5707b7059338a6c0f7930a45dfa36cd53988ab033508afb8",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "This guide shows you how to build a compact rolling sensor graph demo in PHP with [Predis](https://redis.io/docs/latest/develop/clients/php) and Redis time series support. The example simulates three power sensors, ingests readings into Redis, and serves a local browser dashboard that updates in real time."
    },
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Time series are a natural fit for telemetry, monitoring, and IoT-style workloads. In this example, Redis stores a stream of timestamped readings from three simulated power sensors.\n\nThe demo is designed to make a few ideas easy to see:\n\n* Raw readings arrive every 500ms\n* Each graph shows only the most recent 12 seconds\n* Older samples disappear because the time series retention is short\n* 3-second buckets summarize the same readings with minimum, maximum, and average values\n* The bucket summaries are drawn under the same moving time scale as the graph"
    },
    {
      "id": "how-it-works",
      "title": "How it works",
      "role": "content",
      "text": "The example has three main parts:\n\n1. A `SensorSimulator` generates realistic-looking power readings with drift and occasional spikes\n2. A `RedisTimeSeriesStore` creates the time series keys and issues Redis TimeSeries queries\n3. A small local router script for PHP's built-in server renders three stacked combined graph-and-bucket views and serves a JSON snapshot endpoint\n\nEach sensor is stored in its own time series with labels such as `sensor_type`, `sensor_id`, `zone`, and `unit`. The dashboard then uses [`TS.MADD`](https://redis.io/docs/latest/commands/ts.madd) to ingest new readings and [`TS.RANGE`](https://redis.io/docs/latest/commands/ts.range) to query both raw samples and aggregated bucket summaries. The aggregate queries use aligned buckets so the bucket boundaries stay stable as the visible window moves.\n\nBecause PHP's built-in development server is request-driven, this version advances the simulator on each `/api/snapshot` request and persists the simulator state in Redis. That keeps the demo simple to run while still producing the same rolling dashboard behavior as the long-lived server examples."
    },
    {
      "id": "the-php-files",
      "title": "The PHP files",
      "role": "content",
      "text": "The implementation is split across three small files:\n\n* [`SensorSimulator.php`](SensorSimulator.php) - Sensor definitions and sample generation\n* [`RedisTimeSeriesStore.php`](RedisTimeSeriesStore.php) - Redis TimeSeries command helpers\n* [`demo_server.php`](demo_server.php) - PHP router, simulator advancement, and inline dashboard UI\n\nThe Redis helper issues time series commands through Predis' `executeRaw()` method, which keeps the example small while still making the Redis command flow explicit."
    },
    {
      "id": "data-model",
      "title": "Data model",
      "role": "content",
      "text": "Series keys use this pattern:\n\n[code example]\n\nFor example:\n\n[code example]\n\nEach time series has labels similar to:\n\n[code example]\n\nThe demo uses a 12-second retention period so the graphs visibly slide forward as old samples expire. The setup is also idempotent, so you can stop and restart the demo without first cleaning up the time series keys."
    },
    {
      "id": "redis-commands-used",
      "title": "Redis commands used",
      "role": "content",
      "text": "The implementation uses these time series commands directly through Predis:\n\n* [`TS.CREATE`](https://redis.io/docs/latest/commands/ts.create) - Create one time series per sensor with retention and labels\n* [`TS.MADD`](https://redis.io/docs/latest/commands/ts.madd) - Batch-ingest readings from all three sensors every 500ms\n* [`TS.GET`](https://redis.io/docs/latest/commands/ts.get) - Fetch the latest reading for a sensor\n* [`TS.RANGE`](https://redis.io/docs/latest/commands/ts.range) - Read raw recent samples and aggregated 3-second buckets\n* `ALIGN 0` with `TS.RANGE ... AGGREGATION` - Keep bucket boundaries stable as the visible window moves"
    },
    {
      "id": "prerequisites",
      "title": "Prerequisites",
      "role": "content",
      "text": "Before running the demo, make sure that:\n\n* Redis is running and accessible. By default, the demo connects to `localhost:6379`.\n* Your Redis deployment includes time series support.\n* Predis is installed:\n\n[code example]"
    },
    {
      "id": "running-the-demo",
      "title": "Running the demo",
      "role": "content",
      "text": "Start the dashboard server:\n\n[code example]\n\nIf your Redis instance is not on the default host and port, pass them as environment variables:\n\n[code example]\n\nAfter starting the server, visit `http://localhost:8080`.\n\nThe dashboard polls a JSON snapshot endpoint several times per second to show:\n\n* Three stacked rolling graphs of raw sensor readings\n* A bucket summary strip aligned to the same time axis as each graph\n* Minimum, maximum, and average values for each visible bucket\n* A moving 12-second window where old samples disappear as retention expires them\n\nBecause the graph and the bucket summary share the same moving time scale, you can see how raw samples relate to their aggregate bucket without switching views or interpreting a separate table."
    },
    {
      "id": "what-to-look-for",
      "title": "What to look for",
      "role": "content",
      "text": "As you watch the dashboard, pay attention to how the Redis query patterns map to the UI:\n\n* New points arrive every 500ms through `TS.MADD`\n* The graphs show raw values returned by `TS.RANGE`\n* The bucket summaries use `TS.RANGE ... ALIGN 0 AGGREGATION min|max|avg 3000`\n* The left edge of the graph keeps advancing because the time series retention is short\n* The bucket boundaries stay fixed even while the visible window moves\n* The dashboard remains safe to rerun because series creation is idempotent"
    },
    {
      "id": "why-this-shape-works-well",
      "title": "Why this shape works well",
      "role": "content",
      "text": "This demo intentionally uses only three sensors and a very short time horizon. That keeps the visualization small enough to understand at a glance while still demonstrating:\n\n* Repeated high-frequency ingest\n* Querying recent raw history\n* Aggregating into fixed time buckets\n* Short retention and visible expiration\n\nFor a first time series example, this is often easier to understand than a larger dashboard with many metrics, filters, or tables."
    },
    {
      "id": "production-considerations",
      "title": "Production considerations",
      "role": "content",
      "text": "This example intentionally keeps the server and UI small so the Redis behavior is easy to follow. In production, you would usually want to add:\n\n* Authentication and authorization\n* Separate static assets instead of inline HTML\n* Better error reporting and health checks\n* A long-lived worker if you want ingest to continue independently of dashboard requests\n* Deployment-specific retention, window sizes, and aggregation intervals"
    },
    {
      "id": "learn-more",
      "title": "Learn more",
      "role": "related",
      "text": "* [PHP client guide](https://redis.io/docs/latest/develop/clients/php) - Install and use the PHP client\n* [Time series overview](https://redis.io/docs/latest/develop/data-types/timeseries) - Time series concepts and commands\n* [TS.RANGE command](https://redis.io/docs/latest/commands/ts.range) - Query raw and aggregated ranges from a time series\n* [TS.MADD command](https://redis.io/docs/latest/commands/ts.madd) - Add multiple samples in one call\n* [TS.CREATE command](https://redis.io/docs/latest/commands/ts.create) - Create a time series with labels and retention"
    }
  ],
  "examples": [
    {
      "id": "data-model-ex0",
      "language": "text",
      "code": "ts:sensor:power_consumption:{sensor_id}",
      "section_id": "data-model"
    },
    {
      "id": "data-model-ex1",
      "language": "text",
      "code": "ts:sensor:power_consumption:power-1\nts:sensor:power_consumption:power-2\nts:sensor:power_consumption:power-3",
      "section_id": "data-model"
    },
    {
      "id": "data-model-ex2",
      "language": "text",
      "code": "site = demo\nsensor_type = power_consumption\nsensor_id = power-1\nzone = north\nunit = watts",
      "section_id": "data-model"
    },
    {
      "id": "prerequisites-ex0",
      "language": "bash",
      "code": "composer require predis/predis",
      "section_id": "prerequisites"
    },
    {
      "id": "running-the-demo-ex0",
      "language": "bash",
      "code": "composer require predis/predis\nphp -S localhost:8080 demo_server.php",
      "section_id": "running-the-demo"
    },
    {
      "id": "running-the-demo-ex1",
      "language": "bash",
      "code": "REDIS_HOST=127.0.0.1 REDIS_PORT=6379 php -S localhost:8080 demo_server.php",
      "section_id": "running-the-demo"
    }
  ]
}
