ทริกเกอร์ Pub/Sub

Pub/Sub ของ Google Cloud เป็นบัสข้อความที่กระจายอยู่ทั่วโลกซึ่งจะปรับขนาดโดยอัตโนมัติตามที่คุณต้องการ คุณสามารถเรียกใช้ฟังก์ชันเมื่อใดก็ตามที่มีการส่งข้อความ Pub/Sub ใหม่ไปยังหัวข้อที่เฉพาะเจาะจง

นำเข้าโมดูลที่จำเป็น

ในการเริ่มต้นใช้งาน ให้นำเข้าโมดูลที่จำเป็นสำหรับการจัดการเหตุการณ์ Pub/Sub ดังนี้

Node.js

const {onMessagePublished} = require("firebase-functions/pubsub");
const logger = require("firebase-functions/logger");

Python

from firebase_functions import pubsub_fn

เรียกใช้ฟังก์ชัน

คุณต้องระบุชื่อหัวข้อ Pub/Sub ที่ต้องการให้ทริกเกอร์ฟังก์ชัน และตั้งค่าเหตุการณ์ภายในเครื่องจัดการเหตุการณ์ ดังนี้

Node.js

exports.hellopubsub = onMessagePublished("topic-name", (event) => {

Python

@pubsub_fn.on_message_published(topic="topic-name")
def hellopubsub(event: pubsub_fn.CloudEvent[pubsub_fn.MessagePublishedData]) -> None:
    """Log a message using data published to a Pub/Sub topic."""

เข้าถึงเพย์โหลดข้อความ Pub/Sub

คุณเข้าถึงเพย์โหลดสำหรับข้อความ Pub/Sub ได้จากออบเจ็กต์ข้อความที่แสดงผลไปยังฟังก์ชันของคุณ สำหรับข้อความที่มี JSON ในเนื้อหาข้อความ Pub/Sub SDK Firebase สำหรับ Cloud Functions จะมีพร็อพเพอร์ตี้ตัวช่วยในการถอดรหัสข้อความ ตัวอย่างเช่น ข้อความที่เผยแพร่พร้อมเพย์โหลด JSON แบบง่ายมีดังนี้

gcloud pubsub topics publish topic-name --message '{"name":"Xenia"}'

คุณเข้าถึงเพย์โหลดข้อมูล JSON เช่นนี้ได้ผ่านพร็อพเพอร์ตี้ json

Node.js

  // Get the `name` attribute of the PubSub message JSON body.
  let name = null;
  try {
    name = event.data.message.json.name;
  } catch (e) {
    logger.error("PubSub message was not JSON", e);
  }

Python

# Get the `name` attribute of the PubSub message JSON body.
try:
    data = event.data.message.json
except ValueError:
    print("PubSub message was not JSON")
    return
if data is None:
    return
if "name" not in data:
    print("No 'name' key")
    return
name = data["name"]

เพย์โหลดอื่นๆ ที่ไม่ใช่ JSON จะอยู่ในข้อความ Pub/Sub เป็นสตริงที่เข้ารหัส base64 ในออบเจ็กต์ข้อความ หากต้องการอ่านข้อความดังต่อไปนี้ คุณต้องถอดรหัสสตริงที่เข้ารหัส base64 ดังที่แสดง

gcloud pubsub topics publish topic-name --message 'MyMessage'

Node.js

// Decode the PubSub Message body.
const message = event.data.message;
const messageBody = message.data ?
      Buffer.from(message.data, "base64").toString() :
      null;

Python

# Decode the PubSub message body.
message_body = base64.b64decode(event.data.message.data)

เข้าถึงแอตทริบิวต์ของข้อความ

คุณสามารถส่งข้อความ Pub/Sub พร้อมกับแอตทริบิวต์ข้อมูลที่ตั้งค่าไว้ในคำสั่ง publish ตัวอย่างเช่น คุณสามารถเผยแพร่ข้อความที่มีแอตทริบิวต์ name ดังนี้

gcloud pubsub topics publish topic-name --attribute name=Xenia

คุณสามารถอ่านแอตทริบิวต์ดังกล่าวได้จากพร็อพเพอร์ตี้ที่เกี่ยวข้องของออบเจ็กต์ข้อความ ดังนี้

Node.js

// Get the `name` attribute of the message.
const name = event.data.message.attributes.name;

Python

# Get the `name` attribute of the message.
if "name" not in event.data.message.attributes:
    print("No 'name' attribute")
    return
name = event.data.message.attributes["name"]