title: How do I access the ratchet php periodic loop and client sending inside app?
body: <p>I have a Ratchet server and chat app class which runs fine.
  My problem is how do I add a periodic loop?</p>
  
  <p>I tried to follow the example in <a href="https://stackoverflow.com/questions/38412951/periodically-sending-mesages-to-clients-in-ratchet">Periodically sending mesages to clients in Ratchet</a> </p>
  
  <p>But I have been getting nowhere.
  My goal like this guy, is for the server to check all clients are still alive.
  Everytime I try to use the addPeriodicTimer, I cant seem to access the $clients public property in chat.php like the guy from the link above could in order to send messages from the timer in server.php.
  The foreach loop in the periodic timer in server.php keeps complaining that it apparently has an "invalid argument".</p>
  
  <p>Can anyone see what im doing wrong?</p>
  
  <p>my server.php code:</p>
  
  <pre><code>&lt;?php
  
  
  require($_SERVER['DOCUMENT_ROOT'].'/var/www/html/vendor/autoload.php');
  require_once($_SERVER['DOCUMENT_ROOT']."/var/www/html/bin/chat.php");
  
  use Ratchet\Server\IoServer;
  use Ratchet\Http\HttpServer;
  use Ratchet\WebSocket\WsServer;
  use Ram\Chat;
  
  
  $server = IoServer::factory(new HttpServer(new WsServer(new Chat())), 8080);
  
   // Server timer &lt;------ having trouble here
  $server-&gt;loop-&gt;addPeriodicTimer(5, function () use ($server) {
  foreach($server-&gt;app-&gt;clients as $client)
  {
      //$client-&gt;send("[helloworld]");    
  }
  });
  
  
  $server-&gt;run();
  ?&gt;
  </code></pre>
  
  <p>and my chat.php:</p>
  
  <pre><code>&lt;?php
  namespace Ram;
  use Ratchet\MessageComponentInterface;
  use Ratchet\ConnectionInterface;
  
  
  error_reporting(E_ALL ^ E_NOTICE);
  session_id($_GET['sessid']);
      if(!session_id)
          session_start();
  
  
      $userid = $_SESSION["userid"];
      $username = $_SESSION["username"];
      $isadmin = $_SESSION["isadmin"];
      $resources = array();
  
  
  
  
  
      class Users
      {
          public $name;
          public $resid;
          public $timestamp;
  
  
  
      }
  
  
  
  class Chat implements MessageComponentInterface
  {
      public $clients;
  
  
  
  
  
  
  
  
      var $users = array();
  
  
  
      /*
      function cmp($a, $b)
      {
          return strcmp($a-&gt;name, $b-&gt;name);
      }
  
  
      function removeObjectById(ConnectionInterface $id , $arr)
      {
           $array = $arr;
  
          foreach ( $array as $key =&gt; $element ) {
              if ( $id-&gt;resourceId == $element-&gt;resid ) 
              {
                  unset($array[$key]);
                  break;
              }
           }
  
           usort($array, "cmp");
  
           return $array;
      }
  
      */
  
  
  
  
      public function __construct()
      {
  
          $this-&gt;clients = new \SplObjectStorage; 
  
  
  
  
  
  
      }
  
      public function onOpen(ConnectionInterface $conn)
      {
          $this-&gt;clients-&gt;attach($conn);  
  
  
  
  
  
      }
  
      public function onClose(ConnectionInterface $conn)
      {
  
          //$users = removeObjectById($conn, $users);
  
          $this-&gt;clients-&gt;detach($conn);
  
  
  
      }
  
      public function onMessage(ConnectionInterface $conn, $msg)
      {
           $msgjson = json_decode($msg);
           $tag = $msgjson-&gt;tag;
  
           if($tag == "[msgsend]")
           {
  
              foreach($this-&gt;clients as $client)
              {
                    $client-&gt;send($msg);    
              }
       }
       else if($tag == "[bye]")
       {
  
           foreach($this-&gt;clients as $client)
           {
                $client-&gt;send($msg);    
           }
  
           $this-&gt;clients-&gt;detach($conn);
       }
       else if($tag == "[connected]")
       {
           //store client information
           $temp = new Users();
           $temp-&gt;name = $msgjson-&gt;uname;
           $temp-&gt;resid = $conn-&gt;resourceId;
           $temp-&gt;timestamp = date('Y-m-d H:i:s');
  
           $users[] = $temp;
  
           //usort($users, "cmp");
  
  
           //send out messages
            foreach($this-&gt;clients as $client)
           {
                $client-&gt;send($msg);    
           }
  
  
  
       }
       else if($tag == "[imalive]")
       {
           //update user timestamp who sent [imalive]
           if (is_array($users) || is_object($users))
           {
               foreach($users as $user)
               {
                  if($msgjson-&gt;uname == $user-&gt;name)
                  {
                          $user-&gt;timestamp = date('Y-m-d H:i:s'); 
                  }
               }
           }
       }
  
  
  
  
  
  
  
  }
  
  public function onError(ConnectionInterface $conn, Exception $e)
  {
      echo "Error: " . $e-&gt;getMessage(); 
      $conn -&gt; close();   
  }
  
  }
  
  ?&gt; 
  </code></pre>