<?php
// stream.php

header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
header("Access-Control-Allow-Origin: *");

$siteId = file_get_contents("current_site.txt");

for ($i = 1; $i <= 5; $i++) {
    echo "data: " . json_encode([
        "type" => "update",
        "message" => "Crawling page $i..."
    ]) . "\n\n";
    ob_flush();
    flush();
    sleep(1);
}

echo "data: " . json_encode([
    "type" => "complete",
    "site_id" => trim($siteId)
]) . "\n\n";

ob_flush();
flush();