1

I want to know if this is possible with PubNub. I want to achieve realtime communication in my app with database processing which is done using PHP and have updates display in an HTML page using JavaScript. This is the scenario:

  1. database changes via insert/update/delete SQL running in a PHP file.
  2. This same PHP file calls something like this:

    $pubnub->publish(array( 'channel' => 'my_test_channel', 'message' => array( 'database_id' => '100' ) ));

  3. A JavaScript browser would then listen for whatever updates posted to it:

    function(message) { if ('database_id' in message) { // do more stuff } } );

Could this method work where a user registers and the steps from above are executed and then an admin can instantly receive updates on his admin panel of the new users without having to use database triggers, proceedures or polling with ajax?

Craig Conover
  • 4,710
  • 34
  • 59

1 Answers1

2

Invoke PubNub PUSH Message via MySQL Trigger on UPDATE, INSERT and DELETE and PHP

There are several ways to invoke updates from your servers to a Web App Admin Interface and to Mobile Apps. A simple way is to just Publish with PHP API when the Registration Occurs, however you could be more low level then that by using MySQL triggers!

MySQL Makes it simple to wrap your coding into easily accessible TRIGGERS via Stored Procedures. You can create something similar with Pusher, though I know how to do it with PubNub; so here is a quick guide with PubNub and MySQL. Simplicity is what you seek and here is your solution! I will walk you through an easy way to bind any UPDATE, INSERT and DELETE action on your table to a stored function that will get invoked each time, sending a push notifications to your mobile and web apps easily with PubNub.

PubNub Push Message

DELIMITER $$
CREATE PROCEDURE push_message
(p1   DOUBLE,
 p2   DOUBLE,
 p3 BIGINT)
BEGIN
 DECLARE cmd CHAR(255);
 DECLARE result CHAR(255);
 SET cmd = CONCAT('curl https://pubsub.pubnub.com/publish/demo/demo/0/mysql_triggers/0/%22',p1, ',' ,p2, ',' ,p3,'%22');
 SET result = sys_eval(cmd);
END$$;

NOTE: Make sure your PROCEDURE types are correct DOUBLE or VARCHAR or TEXT.

MySQL Trigger Code INSERT Example

CREATE TRIGGER push_message_trigger AFTER INSERT ON your_table_name_here
FOR EACH ROW
CALL push_message(NEW.Column1, NEW.Column2, NEW.Column3);

NOTE: Make sure to include the columns you need here in your push message.

MySQL Trigger Code UPDATE Example

CREATE TRIGGER push_message_trigger AFTER UPDATE ON your_table_name_here
FOR EACH ROW
CALL push_message(NEW.Column1, NEW.Column2, NEW.Column3);

Monitor the Push Message via Debug Console

http://www.pubnub.com/console?sub=demo&pub=demo&channel=mysql_triggers - You can watch your triggers being fired via PubNub Dev Console. This way you can understand what paramaters you need to have changed and what data is important for you to include in each push notifications that can be received by PubNub websocket and more on the Mobile and Web device.

Receiving The Push Message in JavaScript

<div id=pubnub ssl=on></div>
<script src=//pubnub.a.ssl.fastly.net/pubnub-3.3.1.min.js></script>
<script>(function(){

    PUBNUB.init({
        subscribe_key : 'demo',
        ssl           : true
    }).subscribe({
        channel  : 'mysql_triggers',
        callback : function(mysql_trigger_details) {
            alert(mysql_trigger_details);
        }
    });

})();</script>

Now you have the steps needed to send and receive change events from MySQL directly via simple procedures. There are ways to optimize this method as well such as issuing a signal to a daemon process that queues and pools HTTPS push notifications. This should be plenty efficient.

Stephen Blum
  • 6,498
  • 2
  • 34
  • 46