我正在尝试使用Pusher进行实时推送通知。我想在mysql数据发生变化时触发推送器。有人可以帮我吗?我是这个推动者的新手。
答案 0 :(得分:2)
执行此操作的最简单方法是在执行数据库交互后通过Pusher触发事件。
e.g。
// Connect and interact with database
$mysqli = new mysqli("server", "username", "password", "database_name");
$value = $_POST["activity-name"];
$stmt = $mysqli->prepare("INSERT INTO activities (column) VALUES (?)");
$stmt->bind_param("s", $value);
$stmt->execute();
// TODO: check the INSERT was successful.
$stmt->close();
$mysqli->close();
// TODO: Only trigger if the DB interaction was successful
// Create Pusher instance (assuming the Pusher library has been required)
$pusher = new Pusher(APP_KEY, APP_SECRET, APP_ID);
// Trigger the event representing the new data that's been inserted
$pusher->trigger('activities', 'activity-created' ['name' => $value]);
如果数据库与UPDATE
进行交互,那么您可以使用不同的事件名称。
$pusher->trigger('activities', 'activity-updated' ['name' => $value]);
或者,您可以设置数据库触发器,并在发生更改时执行另一个脚本并与Pusher交互。见Invoking a PHP script from a MySQL trigger