title: Best way implementing messaging queue
body: <p>I want to implement some kind of messaging queue system in our webproject. I'm just wondering what's the best way to do this. It's written in PHP (Yii Framework) with MySQL as the database system.</p>
  
  <p>The idea:</p>
  
  <ul>
  <li>User 1 sends a message to User 2, but the message will be placed in the future (i.e. 02-11-2013 14:31)</li>
  <li>User 2 receives the message from User 1 in his/her inbox at 02-11-2013 14:31 AND receives an notification email about this new message.</li>
  </ul>
  
  <p>I thought about 2 ways to do this:</p>
  
  <ol>
  <li><p>Insert the message from User 1 in a 'message' table with a date and time the message should be delivered to User 2. User 2 can only read it when this date and time are smaller than the current date and time (<code>WHERE addTime &lt;= NOW()</code>).</p></li>
  <li><p>Insert the message from User 1 in a 'messageQueue' table. This is a table that contains all the messages that needs to be placed in the future. A cronjob (which will run every minute or so) will place all the messages with a date and time smaller than the current time into the 'message' table and deletes it from the 'messageQueue' table. An e-mail can be send directly to User 2.</p></li>
  </ol>
  
  <p>Cons:</p>
  
  <ol>
  <li>Method 1: no way to send a notification email to User 2</li>
  <li>Method 2: you will have to set a cronjob that runs every minute (a message can be placed in the future with a precision of one minute). The notification e-mail can be send directly, but it will be a heavy system with a lot of messages. If the cronjob takes longer than a minute it will be overlapping each other, which can result in a server shutdown eventually.</li>
  </ol>
  
  <p>Does anyone have any idea to do this? I'm actually looking for some push function from the server/database, which tells my script that a new message is delivered to User 2 and that an e-mail needs to be send to User 2.</p>