Advertisement
misaalanshori

laravel controller

Feb 8th, 2025
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.44 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Response;
  7. use Illuminate\Support\Facades\DB;
  8. use Exception;
  9. use Inertia\Inertia;
  10. use App\Models\Document;
  11. use App\Models\ExtractionResults;
  12.  
  13. class ExtractionController extends Controller
  14. {
  15.     public function index(Document $document)
  16.     {
  17.         return Inertia::render('DocumentExtract', compact('document'));
  18.     }
  19.  
  20.     public function extract(Document $document)
  21.     {
  22.         ini_set('output_buffering', 'off');
  23.         ini_set('zlib.output_compression', 'off');
  24.         // Set headers for SSE
  25.         $response = Response::stream(function () use ($document) {
  26.             $counter = 0; // Initialize counter
  27.  
  28.             while ($counter < 20) { // Stop after 10 messages
  29.                 echo "data: " . json_encode([
  30.                     'counter' => $counter,
  31.                     'time' => now()->toDateTimeString(),
  32.                     'document' => $document,
  33.                 ]) . "\n\n";
  34.  
  35.                 ob_flush();
  36.                 flush();
  37.                 sleep(1); // Send updates every 2 seconds
  38.                 $counter++; // Increment counter
  39.             }
  40.         }, 200, [
  41.             'Content-Type'      => 'text/event-stream',
  42.             'Cache-Control'     => 'no-cache',
  43.             'Connection'        => 'keep-alive',
  44.             'X-Accel-Buffering' => 'no',
  45.         ]);
  46.  
  47.         return $response;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement