title: Periodically sending messages to clients in Ratchet
body: <p>I'm trying to periodically send a "hello world!" message to all clients connected to the chat-server from the Ratchet tutorial</p>
  
  <p>I will post all of the code here:
  Chat.php:</p>
  
  <pre><code>&lt;?php
  namespace MyApp;
  use Ratchet\MessageComponentInterface;
  use Ratchet\ConnectionInterface;
  
  class Chat implements MessageComponentInterface {
      public $clients;
  
      public function __construct() {
          $this-&gt;clients = new \SplObjectStorage;
              }
  
      public function onOpen(ConnectionInterface $conn) {
          // Store the new connection to send messages to later
          $this-&gt;clients-&gt;attach($conn);
  
          echo "New connection! ({$conn-&gt;resourceId})\n";
      }
  
      //this worked but I don't want this behaviour
      public function onMessage(ConnectionInterface $from, $msg) {
          /*$numRecv = count($this-&gt;clients) - 1;
          echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
              , $from-&gt;resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
  
          foreach ($this-&gt;clients as $client) {
              if ($from !== $client) {
                  // The sender is not the receiver, send to each client connected
                  $client-&gt;send($msg);
              }
          }*/
      }
  
      public function onClose(ConnectionInterface $conn) {
          // The connection is closed, remove it, as we can no longer send it messages
          $this-&gt;clients-&gt;detach($conn);
  
          echo "Connection {$conn-&gt;resourceId} has disconnected\n";
      }
  
      public function onError(ConnectionInterface $conn, \Exception $e) {
          echo "An error has occurred: {$e-&gt;getMessage()}\n";
  
          $conn-&gt;close();
      }
  }
  </code></pre>
  
  <p>chat-server.php:</p>
  
  <pre><code>&lt;?php
  use Ratchet\Server\IoServer;
  use MyApp\Chat;
  
      require dirname(__DIR__) . '/vendor/autoload.php';
  
      $server = IoServer::factory(
          new Chat(),
          8080
      );
  
      $server-&gt;run();
  </code></pre>
  
  <p>To test how much of the docs I understood , I added a timer to the server's loop</p>
  
  <pre><code>    &lt;?php
      use Ratchet\Server\IoServer;
      use MyApp\Chat;
  
          require dirname(__DIR__) . '/vendor/autoload.php';
  
          $server = IoServer::factory(
              new Chat(),
              8080
          );
  
  
          // My code here
          $server-&gt;loop-&gt;addPeriodicTimer(5, function () {
            echo  "custom loop timer working !";        
          });
  
  
          $server-&gt;run();
  </code></pre>
  
  <p>and it worked fine outputting that string every five seconds after starting the server.</p>
  
  <p>Now I tried doing it like so, trying to send a message to clients stored in the MessageComponentInterface called Chat from the tutorial</p>
  
  <pre><code>$server-&gt;loop-&gt;addPeriodicTimer(5, function () {        
      foreach ($server-&gt;app-&gt;clients as $client) {                  
              $client-&gt;send("hello client");          
      }
  });
  </code></pre>
  
  <p>But I'm getting that $server->app is NULL which is probably because I'm now inside the function() block .I'm not an expert when it comes to Object oriented PHP, and this little project will sure help me a lot.
  How can I access the  <code>MessageComponentInterface</code> called <code>app</code> property of the server inside the timer and then send data to the clients stored in there?</p>