Invoke pusher when mysql has changed [closed]

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

⚠️ Warring! this may cause slowdowns on your database. This will introduce relatively poor performance given sys exec’ing curl for every insert. Far better to also have a trigger write the message to another table and have another process checking for new rows in a loop.

MySQL Makes it simple to wrap your coding into easily accessible TRIGGERS via Stored Procedures. You can create something similar with pusher and 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.4.5.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.

Leave a Comment