title: Ratchet client event loop wait for finish
body: <p>I have the following test php code:</p>
  <pre class="lang-php prettyprint-override"><code>use React\EventLoop\Loop;
  $loop = Loop::get();
  $reason = &quot;unknown&quot;;
  
  \Ratchet\Client\connect('wss://demo.piesocket.com/v3/channel_1?api_key=VCXCEuvhGcBDP7XhiJJUDvR1e1D3eiVjgZ9VRiaV&amp;notify_self', [], [], $loop)-&gt;then(function($conn) use ($loop, &amp;$reason){
      $conn-&gt;on('message', function($msg) use ($loop, $conn, &amp;$reason){
          echo $msg.&quot;\n&quot;;
          if ($msg == &quot;quit&quot;) {
              $conn-&gt;close();
              $loop-&gt;stop();
              $reason = &quot;quit&quot;;
          }
      });
  }, function ($e) {
      echo &quot;Could not connect: {$e-&gt;getMessage()}\n&quot;;
  });
  
  echo &quot;the echo $reason\n&quot;;
  </code></pre>
  <p>The code mostly works, it connects to the websocket and prints messages on the command line that are sent through the websocket. Also when I send quit it stops the connection and the script ends.</p>
  <p>However the last echo in the script, the &quot;the echo $reason&quot; echo, is printed first thing the script starts. I would like to somehow wait for the websocket to finish and then run the echo statement. How do I do this?</p>
  <p>In the end the websocket code is going to be a small subroutine of a bigger script. I want the bigger script to wait for the subroutine to finish.</p>