PHP Classes

File: docs/REACTHMR_QUICK_SUMMARY.md

Recommend this page to a friend!
  Packages of Adrian M   upMVC   docs/REACTHMR_QUICK_SUMMARY.md   Download  
File: docs/REACTHMR_QUICK_SUMMARY.md
Role: Example script
Content type: text/markdown
Description: Example script
Class: upMVC
Pure PHP web development without other frameworks
Author: By
Last change: Update of docs/REACTHMR_QUICK_SUMMARY.md
Date: 2 months ago
Size: 7,197 bytes
 

Contents

Class file image Download

? ReactHMR Module - Quick Summary

What We Built

Hot Module Reload for ES Modules - Edit files, save, watch browser auto-reload in ~1.5s. No webpack, no Vite, just PHP + SSE.

? The Innovation

Traditional HMR:        ReactHMR:
- Webpack config        - Drop in module
- npm dev server        - Pure PHP
- Complex setup         - SSE + file watching
- ~500ms reload         - ~1.5s reload

Trade-off: Simplicity over speed

? Core Features

  1. PHP File Watching - Monitors files via `filemtime()` + hashing
  2. SSE Streaming - Server-Sent Events push to browser
  3. Auto Browser Reload - EventSource triggers refresh
  4. Status Indicator - Real-time connection status
  5. Auto-Reconnect - Handles disconnects gracefully

? What Was Created

modules/reacthmr/
??? Controller.php          ? HMR logic (~280 lines)
??? Model.php              ? Sample data (~60 lines)
??? View.php               ? UI + components (~580 lines)
??? README.md              ? Documentation (~550 lines)
??? routes/Routes.php      ? Route registration (~30 lines)
??? components/
    ??? TodoApp.js         ? External component (~90 lines)

docs/
??? REACT_INTEGRATION_PATTERNS.md  ? Updated with Pattern 5
??? REACTHMR_IMPLEMENTATION.md     ? Implementation summary
??? MODULE_PHILOSOPHY.md           ? Updated React modules section

Total: ~1,590 lines of code + comprehensive docs

? Quick Demo

# 1. Visit
http://localhost/reacthmr

# 2. Open editor
modules/reacthmr/View.php

# 3. Change text
<h1>? Hot Module Reload - TESTING!</h1>

# 4. Save (Ctrl+S)

# 5. Watch browser auto-reload! ?
# Time: ~1.5 seconds

? What It Shows

1. Five Components Included

  • Counter (Preact + hooks)
  • User Table (PHP data ? Preact)
  • Stats Dashboard (PHP stats)
  • Todo App (external file)
  • Vue.js example

2. HMR Status Indicator

  • ? Connected
  • ? Reconnecting
  • ? Reloading

3. PHP ? JS Data Flow

<!-- PHP serializes data -->
<script type="application/json" id="php-data">
    <?php echo json_encode($data); ?>
</script>

<!-- JS hydrates component -->
<script type="module">
    const phpData = JSON.parse(
        document.getElementById('php-data').textContent
    );
</script>

? Performance

| Event | Time | |-------|------| | File saved | 0ms | | PHP detects | ~1000ms | | SSE sent | ~50ms | | Browser fade | 300ms | | Page reload | ~200ms | | Total | ~1.5s |

vs Webpack HMR: ~500ms (hot swap) vs Manual F5: ~5s (context switch)

? How It Works

Browser                    PHP Controller
  ?                            ?
  ???? EventSource('/hmr') ?????
  ?                            ?
  ?                         while(true) {
  ?                            getFileHashes()
  ?                            if (changed) {
  ????? SSE 'reload' event ?????
  ?                            }
  ?                            sleep(1)
  ?                         }
  ?
 reload()

? Key Innovations

1. SSE Instead of WebSockets

  • Simpler (one-way communication)
  • Built-in EventSource API
  • Auto-reconnect
  • Lower overhead

2. Hash-Based Change Detection

$hash = md5(/all file mtimes/);
if ($hash !== $lastHash) {
    echo "event: reload\n\n";
}

3. Zero Build Tools

  • No webpack
  • No Vite
  • No npm dev server
  • Just pure PHP

? Use Cases

? Perfect For:

  • Component development
  • Rapid prototyping
  • UI/style iteration
  • Learning ES modules

? Not For:

  • Production (must disable)
  • Shared hosting
  • Very large projects

? Production Safety

Auto-disable:

<?php if (ENVIRONMENT === 'development'): ?>
    <?php $this->hmrClient(); ?>
<?php endif; ?>

Route protection:

if (ENVIRONMENT !== 'development') {
    http_response_code(404);
    exit;
}

? Documentation Quality

Module README (~4,000 words)

  • ? Quick start
  • ? Architecture diagrams
  • ? Complete code examples
  • ? Configuration options
  • ? Troubleshooting
  • ? Pro tips

Updated Main Docs

  • ? REACT_INTEGRATION_PATTERNS.md (+300 lines)
  • ? MODULE_PHILOSOPHY.md (updated)
  • ? Pattern comparison tables
  • ? Decision matrices

? What Makes It Special

1. Educational

Shows how HMR actually works: - File watching - SSE streaming - Browser communication - State management

2. Simple

No complex config: - Drop in module - Register routes - Start coding

3. Transparent

All code visible: - ~280 line Controller - Clear implementation - Easy to modify

4. Framework Agnostic

Works with any ES module framework: - Preact ? - React ? - Vue ? - Svelte ?

? Development Impact

Before (Pattern 4):

Edit ? Save ? Switch to Browser ? F5 ? See Changes
Time: ~5 seconds (manual)

After (Pattern 5):

Edit ? Save ? See Changes (auto)
Time: ~1.5 seconds (automatic)

Result: 3x faster + no context switching

? Learning Outcomes

Developers learn: 1. How HMR works (not magic!) 2. Server-Sent Events (SSE) 3. PHP file watching 4. ES modules + Import Maps 5. Component communication 6. Real-time browser updates

? Pattern 5 vs Others

| Feature | P1<br>CDN | P2/P3<br>Build | P4<br>ES Mod | P5<br>HMR | |---------|-----------|----------------|--------------|-----------| | Build | ? | ? | ? | ? | | JSX | ? | ? | HTM | HTM | | Auto-reload | ? | ?? | ? | ? | | Setup | 5min | 30min | 5min | 10min | | Dev Speed | Slow | Medium | Medium | Fast |

? Key Quotes

> "You don't need webpack for Hot Module Reload"

> "Edit ? Save ? See Changes (1.5s). That's it."

> "Simple tools can deliver great experiences"

? Implementation Checklist

  • [x] File watching implementation
  • [x] SSE streaming
  • [x] Client EventSource connection
  • [x] Status indicator UI
  • [x] Auto-reconnect logic
  • [x] Multiple component examples
  • [x] External component (TodoApp.js)
  • [x] PHP ? JS data flow
  • [x] Production safety
  • [x] Comprehensive README
  • [x] Updated main documentation
  • [x] Performance analysis
  • [x] Configuration options
  • [x] Troubleshooting guide

? Bottom Line

ReactHMR proves that modern development tools don't have to be complex.

Achievements:

  1. ? HMR without build tools
  2. ? ~1.5s feedback loop
  3. ? Simple PHP implementation
  4. ? Educational reference
  5. ? Production-safe
  6. ? Framework agnostic

Philosophy:

Show developers they can build powerful tools with simple code.

That's the upMVC way. ?

? Quick Links

  • Module: `modules/reacthmr/`
  • Demo: `/reacthmr`
  • Docs: `modules/reacthmr/README.md`
  • Implementation: `docs/REACTHMR_IMPLEMENTATION.md`
  • Patterns Guide: `docs/REACT_INTEGRATION_PATTERNS.md`

Created: October 17, 2025 Pattern 5: ES Modules + Hot Module Reload Status: Production Ready ?