CodeIgniter Forums
[SOLVED] Chunked / Streamed response - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: [SOLVED] Chunked / Streamed response (/thread-77201.html)



[SOLVED] Chunked / Streamed response - marios88 - 08-01-2020

Hello

Is there a recommended way to send chunked or streaming responses from the controller?

Thanks


RE: Chunked / Streamed response - marios88 - 08-06-2020

That was actually easy, problem was with apache and php-fpm, make sure you have flushpackets=auto in your virtualhost conf

Code:
<FilesMatch \.php$>
  SetHandler proxy:fcgi://localhost:9000
  # for Unix sockets, Apache 2.4.10 or higher
  # SetHandler proxy:unix:/path/to/fpm.sock|fcgi://dummy
</FilesMatch>

<Proxy "fcgi://localhost:9000" flushpackets=auto flushwait=10>
</Proxy>

-----
example controller
-----

PHP Code:
public function test(){
      $this->session->regenerate(true); // test cookies are sent
      $i 0;
      $this->response->setHeader('header-forced','1'); //example add header
      $this->response->setBody(''); // clear body
      $this->response->send(); // sends headers and cookies
      $this->session->close(); //session_write_close()
      
      
//long running task
      do{
          echo $i++.'<br>'.PHP_EOL;
          usleep(50000);
          ob_end_flush();
          if(ob_get_level() > 0) {
              @ob_flush();
          }
          flush();
          ob_start();
      }while($i 20);