title: Push notifications using Socket.IO and PHP
body: <p><strong>The idea/context:</strong></p>
  
  <p>I'm thinking about giving my users a nice <em>little</em> extra feature: I want to add push notifications. This is the use case:</p>
  
  <p>People have a guestbook at their profile page. When someone posts a message in a user's guestbook, that user will receive a push notification (if he's online ofcourse). If he's not online, the next time he comes online, we'll just pull the notifications from the DB.</p>
  
  <p>I was thinking about doing this with Socket.IO running on a Node.JS server. My current application is built with PHP (so the posting etc. is handled by PHP).</p>
  
  <p>All online users will connect using Socket.IO to listen for their own notifications. Their socket will be saved in an array or hash on the server.</p>
  
  <p>This is the flow I have in mind: </p>
  
  <ol>
  <li>UserA posts a message in guestbook of UserB</li>
  <li>Make Socket.IO emit a notification to UserB (if online, so known by Socket.IO)</li>
  <li>Save the message in DB</li>
  </ol>
  
  <p>The issue here is the '<em>make Socket.IO emit a notification</em>'-part. I would need a way to do this from PHP, because I want <strong>the server</strong> to emit this notification and <strong>not the user</strong> that is posting the message. Why you ask? I want to prevent malicious users from creating fake notifications. So in pseudocode the PHP application would look like this:</p>
  
  <pre><code>// do some validations here ...
  
  // This is the message that was posted
  $message = array(
      'from' =&gt; 'UserA',
      'to' =&gt; 'UserB',
      'msg' =&gt; 'Hello you'
  );
  
  // Send a notification to the user by emitting an event
  socketio_emit('notification', json_encode($message));
  
  save_in_db($message);
  </code></pre>
  
  <p><strong>The question(s):</strong></p>
  
  <p>What are your thoughts about this? Are there better ways to implement this seemingly small feature? And also, how would I do the <code>socketio_emit()</code> in PHP, in other words: how do I communicate with a Socket.IO server using PHP?</p>
  
  <p>Thanks a lot!</p>