Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Response;
- use Illuminate\Support\Facades\DB;
- use Exception;
- use Inertia\Inertia;
- use App\Models\Document;
- use App\Models\ExtractionResults;
- class ExtractionController extends Controller
- {
- public function index(Document $document)
- {
- return Inertia::render('DocumentExtract', compact('document'));
- }
- public function extract(Document $document)
- {
- ini_set('output_buffering', 'off');
- ini_set('zlib.output_compression', 'off');
- // Set headers for SSE
- $response = Response::stream(function () use ($document) {
- $counter = 0; // Initialize counter
- while ($counter < 20) { // Stop after 10 messages
- echo "data: " . json_encode([
- 'counter' => $counter,
- 'time' => now()->toDateTimeString(),
- 'document' => $document,
- ]) . "\n\n";
- ob_flush();
- flush();
- sleep(1); // Send updates every 2 seconds
- $counter++; // Increment counter
- }
- }, 200, [
- 'Content-Type' => 'text/event-stream',
- 'Cache-Control' => 'no-cache',
- 'Connection' => 'keep-alive',
- 'X-Accel-Buffering' => 'no',
- ]);
- return $response;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement