title: Implement Server side events with Laravel/php-fpm/nginx
body: <p>I tried to implement server side events with Laravel.<br />
  My implementation of SSE endpoint:</p>
  <pre class="lang-php prettyprint-override"><code>public function stream()
  {
      session_write_close(); 
      session_commit();
      return response()-&gt;stream(function() {
          set_time_limit(300);
          $lastMessage = Message::latest('id')-&gt;first(); 
          $now = Carbon::now()-&gt;format('Y-m-d H:i:s.u');
          $lastMessage = is_null($lastMessage) ? $now : Carbon::parse($lastMessage-&gt;created_at)-&gt;format('Y-m-d H:i:s.u');
          session_write_close(); 
          session_commit();
          ob_start();
          for ($i=0;$i&lt;300;$i++) {
              if (connection_aborted() || connection_status() != CONNECTION_NORMAL) {
                  break;
              }
              $storedMessages = \Cache::rememberForever('stored_messages', function () use ($lastMessage) {
                  return Message::where('created_at', '&gt;', $lastMessage)-&gt;get();
              });
              if (!$storedMessages-&gt;isEmpty()) {
                  \Cache::forget('stored_messages');
                  $lastMessage = Carbon::parse($storedMessages-&gt;last()-&gt;created_at)-&gt;format('Y-m-d H:i:s.u');
                  echo &quot;event: message\n&quot;, &quot;data: &quot; . json_encode($storedMessages), &quot;\n\n&quot;;
              }
              echo &quot;\n\n&quot;;
              ob_flush();
              flush();
              sleep(1);
          }
          ob_end_flush();
      }, 200, [
          'Cache-Control' =&gt; 'no-cache',
          'Content-Type' =&gt; 'text/event-stream',
          'Connection' =&gt; 'keep-alive',
          'X-Accel-Buffering' =&gt; 'no',
      ]);
  }
  </code></pre>
  <p>Front side:</p>
  <pre class="lang-js prettyprint-override"><code>this.evtSource = new EventSource('/api/stream');
  this.evtSource.addEventListener('message', event =&gt; {
      JSON.parse(event.data).forEach(message =&gt; {
          this.receivedMessages.push(message);
      });
  }, false);
  // Reconnection function follows for when the loop ends
  </code></pre>
  <p>This works as long as less than 10 browsers/tabs are viewing the page that opens the connection to the stream endpoint. <strong>When 11th tab is opened, whole website hangs.</strong> (I'm using http2, so I think this not a connection limit)<br />
  I checked what is causing the behavior, and I found out that &quot;max_children&quot; of php-fpm was causing this. But I think this number cannot be increased to a big number.</p>
  <p>I googled SSE examples of php, but many of them had same problem.<br />
  I also found this question: <a href="https://stackoverflow.com/questions/16315137/setting-time-interval-in-html5-server-sent-events/46138860">Setting time interval in HTML5 server sent events</a><br />
  The most upvoted answer says we should remove the infinite loop and &quot;keep retrying the endpoint&quot; until update is detected, but I think this is just a &quot;polling&quot;. Not realtime update and server load not reduced.</p>
  <p>I just want to push notifications to users, so I thought introducing websocket (along with websocket servers etc...) is too much for this purpose. So my question is: is it possible to use Server Side Events with Laravel/php-fpm/nginx? If possible, how?</p>
  <p>Edit:
  My whole implementation for your reference.<br />
  <a href="https://github.com/lechatthecat/laravel-soketi-broadcast-example-by-websocket/tree/http2_server_side_events" rel="nofollow noreferrer">https://github.com/lechatthecat/laravel-soketi-broadcast-example-by-websocket/tree/http2_server_side_events</a></p>