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


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

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

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

Node.js

const {onMessagePublished} = require("firebase-functions/v2/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 สำหรับ Cloud Functions จะมีพร็อพเพอร์ตี้ตัวช่วยในการถอดรหัสข้อความFirebase ตัวอย่างเช่น ข้อความที่เผยแพร่ด้วยเพย์โหลด 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 สามารถส่งได้โดยใช้แอตทริบิวต์ข้อมูลที่ตั้งค่าไว้ในคำสั่งเผยแพร่ ตัวอย่างเช่น คุณสามารถเผยแพร่ข้อความที่มีแอตทริบิวต์ 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"]