如何订阅 firebase 函数以便它通过 pub/sub 触发器执行
How to subscribe a firebase function so that it executes via a pub/sub trigger
我被要求创建一个触发 onUpdate 的 firebase (fb) 函数。然后我必须从fb数据库收集一堆信息并发布一条消息,以便在那个时候触发另一个功能。
fb更新触发函数A
functionA 发布消息
functionB 是该主题的订阅者,并在消息发布后触发。
下面是 onUpdate 触发器的基础知识:
const functions = require("firebase-functions"),
Promise = require("promise"),
PubSub = require(`@google-cloud/pubsub`),
admin = require("firebase-admin");
const pubsub = new PubSub();
exports.checkInOrder = functions.database
.ref("/orders/{id}")
.onUpdate((change, context) => {
const after = change.after.val();
// check the status: "pending-pickup" or "fulfilled" TODO
if (after.status === "new") {
console.log("ended because package is new.");
return null;
}
let dsObj = {};
const orderId = context.params.id;
const topicName = 'check-in-order';
const subscriptionName = 'check-in-order';
return // how would I send the message to the pubsub here?
});
总结一下:
- 如何向 pubsub 发送消息
- 如何订阅 firebase 函数以在主题收到消息时触发?
如果这听起来很混乱,我很抱歉 - 我完全迷失在这里。谢谢!
所以我终于想通了。非常直接,只是在匆忙的时间框架内一次学习所有这些东西而感到不知所措。下面是我的代码。我包含了整个页面,以防将来可能对其他人有所帮助。
const functions = require("firebase-functions"),
Promise = require("promise"),
PubSub = require(`@google-cloud/pubsub`),
admin = require("firebase-admin");
const init = () => {
const topicName = "check-in-order";
pubsub
.createTopic(topicName)
.then(results => {
const topic = results[0];
console.log(`Topic ${topicName} created.`);
return;
})
.catch(err => {
console.error("ERROR on init:", err);
return;
});
};
const pubsub = new PubSub();
exports.orderCreated = functions.database
.ref("/orders/{id}")
.onUpdate((change, context) => {
console.log("it's happening!");
const after = change.after.val();
console.log("after::::>", after)
if (after.status === "new") {
console.log('stopped because status is new');
return null;
}
if (after.status === "pending-pickup" || after.status === "fulfilled") {
const orderId = context.params.id;
const topicName = "check-in-order";
let { assignedSlot, userId, parcelLockerId, carrier, trackingNumber, orderInDate, pickUpCode, status, } = after;
const dsObj = {
order: {
orderId,
userId,
parcelLockerId,
carrier,
trackingNumber,
orderInDate,
pickUpCode,
status,
}
};
const dataBuffer = new Buffer.from(dsObj.toString());
// publish to trigger check in function
return pubsub
.topic(topicName)
.publisher()
.publish(dataBuffer)
.then(messageId => {
console.log(`:::::::: Message ${messageId} has now published. :::::::::::`);
return true;
})
.catch(err => {
console.error("ERROR:", err);
throw err;
});
}
return false;
});
exports.checkInOrder = () => {
}
exports.checkIn = functions.pubsub.topic('check-in-order').onPublish((message) => {
console.log("everything is running now", message);
return true;
});
init();
我被要求创建一个触发 onUpdate 的 firebase (fb) 函数。然后我必须从fb数据库收集一堆信息并发布一条消息,以便在那个时候触发另一个功能。
fb更新触发函数A functionA 发布消息 functionB 是该主题的订阅者,并在消息发布后触发。
下面是 onUpdate 触发器的基础知识:
const functions = require("firebase-functions"),
Promise = require("promise"),
PubSub = require(`@google-cloud/pubsub`),
admin = require("firebase-admin");
const pubsub = new PubSub();
exports.checkInOrder = functions.database
.ref("/orders/{id}")
.onUpdate((change, context) => {
const after = change.after.val();
// check the status: "pending-pickup" or "fulfilled" TODO
if (after.status === "new") {
console.log("ended because package is new.");
return null;
}
let dsObj = {};
const orderId = context.params.id;
const topicName = 'check-in-order';
const subscriptionName = 'check-in-order';
return // how would I send the message to the pubsub here?
});
总结一下:
- 如何向 pubsub 发送消息
- 如何订阅 firebase 函数以在主题收到消息时触发?
如果这听起来很混乱,我很抱歉 - 我完全迷失在这里。谢谢!
所以我终于想通了。非常直接,只是在匆忙的时间框架内一次学习所有这些东西而感到不知所措。下面是我的代码。我包含了整个页面,以防将来可能对其他人有所帮助。
const functions = require("firebase-functions"),
Promise = require("promise"),
PubSub = require(`@google-cloud/pubsub`),
admin = require("firebase-admin");
const init = () => {
const topicName = "check-in-order";
pubsub
.createTopic(topicName)
.then(results => {
const topic = results[0];
console.log(`Topic ${topicName} created.`);
return;
})
.catch(err => {
console.error("ERROR on init:", err);
return;
});
};
const pubsub = new PubSub();
exports.orderCreated = functions.database
.ref("/orders/{id}")
.onUpdate((change, context) => {
console.log("it's happening!");
const after = change.after.val();
console.log("after::::>", after)
if (after.status === "new") {
console.log('stopped because status is new');
return null;
}
if (after.status === "pending-pickup" || after.status === "fulfilled") {
const orderId = context.params.id;
const topicName = "check-in-order";
let { assignedSlot, userId, parcelLockerId, carrier, trackingNumber, orderInDate, pickUpCode, status, } = after;
const dsObj = {
order: {
orderId,
userId,
parcelLockerId,
carrier,
trackingNumber,
orderInDate,
pickUpCode,
status,
}
};
const dataBuffer = new Buffer.from(dsObj.toString());
// publish to trigger check in function
return pubsub
.topic(topicName)
.publisher()
.publish(dataBuffer)
.then(messageId => {
console.log(`:::::::: Message ${messageId} has now published. :::::::::::`);
return true;
})
.catch(err => {
console.error("ERROR:", err);
throw err;
});
}
return false;
});
exports.checkInOrder = () => {
}
exports.checkIn = functions.pubsub.topic('check-in-order').onPublish((message) => {
console.log("everything is running now", message);
return true;
});
init();