当mysql改变时调用pusher [英] Invoke pusher when mysql has changed

查看:107
本文介绍了当mysql改变时调用pusher的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不使用pusher进行管理员交互的情况下,如何将mysql数据库更改推送到管理屏幕?

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

pusher可以运行某种侦听器来更改mysql上的更改吗?或者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.

推荐答案

通过MySQL在UPDATE,INSERT和DELETE上触发来调用PubNub PUSH消息

⚠️战争!,这可能会导致数据库运行速度变慢.给定每个插入的sys exec'curl,这将引入相对较差的性能.最好还有一个触发器将消息写入另一个表,并让另一个进程检查循环中的新行.

⚠️ 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使通过存储过程将代码包装到易于访问的 TRIGGERS 中变得简单.您可以使用pusher创建类似的内容,而我知道如何使用PubNub进行创建.因此,这里是PubNub和MySQL的快速指南.您追求的就是简单,这就是您的解决方案!我将引导您通过一种简单的方法将表上的任何 UPDATE INSERT DELETE 操作绑定到存储的函数,该函数将分别被调用时间,使用PubNub轻松向您的移动和Web应用发送推送通知.

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.

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$$;

注意:确保您的过程类型正确 DOUBLE VARCHAR TEXT .

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

注意:请确保在您的推送消息中包括此处所需的列.

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

通过调试控制台监控推送消息

http://www.pubnub.com/console? sub = demo& pub = demo& channel = mysql_triggers -您可以查看通过PubNub Dev Console触发的触发器.这样,您就可以了解需要更改哪些参数,以及哪些数据对您很重要,以便将它们包含在PubNub websocket可以接收的每个推送通知中,以及在移动和Web设备上可以接收的更多信息.

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.

<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>

现在,您已具有通过简单的过程直接从MySQL发送和接收变更事件所需的步骤.还有一些方法可以优化此方法,例如向守护程序进程发出信号,该进程将HTTPS推送通知排队并入池.这应该足够高效.

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.

这篇关于当mysql改变时调用pusher的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆