Push notifications using Socket.IO and PHP
===

**The idea/context:**

I'm thinking about giving my users a nice *little* extra feature: I want to add push notifications. This is the use case:

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.

I was thinking about doing this with Socket.IO. My current application is built with PHP (so the posting etc. is handled by PHP).

All online users will connect to the Socket.IO server to listen for their own notifications. Their socket will be saved in an array or hash.

This is the flow I have in mind: 

 1. UserA posts a message in guestbook of UserB
 2. Make Socket.IO emit a notification to UserB (if online, so known by Socket.IO)
 3. Save the message in DB

The issue here is the '*make Socket.IO emit a notification*'-part. I would need a way to do this from PHP, because I want **the server** to emit this notification and **not the user** 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:

    // do some validations here ...
    
    // This is the message that was posted
    $message = array(
    	'from' => 'UserA',
    	'to' => 'UserB',
    	'msg' => 'Hello you'
    );
    
    // Send a notification to the user by emitting an event
    socketio_emit('notification', json_encode($message));
    
    save_in_db($message);

**The final question(s):**

What are your thoughts about this? Are there better ways to implement this seemingly small feature? And also, how would I do the `socketio_emit()` in PHP?

Thanks a lot!