Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke pusher when mysql has changed [closed]

Tags:

mysql

pusher

How possible is it to get mysql database changes to be pushed to a an admin screen without admin interaction using pusher?

Can pusher run some kind of listener for changes on the mysql or will pusher simple do some kind of fancy polling?

All in all how would such a setup be coded?

A simple example or a link would be very much appreciated.

like image 700
Tennesha Rigg Stewart Avatar asked Nov 20 '12 13:11

Tennesha Rigg Stewart


3 Answers

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.

like image 75
Stephen Blum Avatar answered Nov 12 '22 21:11

Stephen Blum


In MySQL, this takes an administration application with a polling loop.

If you design the MySQL schema for it, the polling loop can be cheap. It can query the interesting tables for either a latest time stamp or a latest row id.

But it still has to poll.

like image 20
O. Jones Avatar answered Nov 12 '22 21:11

O. Jones


You have several options. The most common would be to handle this at your application layer when you change the database. This of course means you need a singular point in your code where the database is updated and this isn't always possible. However, if possible, I would recommend this solution.

Polling is another option, although obviously less real-time. You would simply write a script that checks those conditions and calls the pusher API when things change.

One other possibility is to use MySQL triggers with a custom plugin so you can call the Pusher API immediately when the database changes. This is by far the most complicated solution and I wouldn't really recommended it. See the first answer of this question for some of the reasons why.

like image 1
Michael Mior Avatar answered Nov 12 '22 22:11

Michael Mior