با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
با Cloud Functions (نسل دوم)، میتوانید عملکردها را در پاسخ به رویدادهای سفارشی فعال کنید. اینها رویدادهایی هستند که توسط ارائه دهندگان رویدادهای ویژه یا اضافی ارائه می شوند، برخلاف رویدادهای Firebase که به طور بومی توسط Firebase SDK for Cloud Functions پشتیبانی می شوند. از طریق راهاندازهای رویداد سفارشی، برنامه شما میتواند به رویدادهای ارائهشده توسط Firebase Extensions پاسخ دهد، یا میتوانید رویدادهای سفارشی خود را منتشر کنید و عملکردهای راهاندازی را در پاسخ به آنها منتشر کنید.
میتوانید رویدادهای سفارشی را منتشر کنید (یا رویدادها را از برنامههای افزودنی Firebase دریافت کنید) و توابع را در پاسخ به آن رویدادها با اجرای این جریان اصلی راهاندازی کنید:
رویدادهای مورد نظر را در یک کانال Eventarc منتشر کنید یا رویدادهای موجود را که توسط افزونه ای که نصب کرده اید ارائه شده است شناسایی کنید.
در کد تابع خود، با یک کنترل کننده رویداد در رویدادهای کانال Eventarc مشترک شوید.
در تابع، محموله بازگشتی در شی CloudEvent را تجزیه کنید و هر منطق سفارشی مورد نیاز برنامه شما را انجام دهید.
به عنوان مثال، یک برنامه بازی ممکن است بخواهد هنگام ورود یا خروج کاربران به جدول امتیازات ده رقیب برتر، اعلان هایی را برای آنها ارسال کند. این برنامه میتواند رویدادهای تابلوی امتیازات را در کانال پیشفرض منتشر کند و سپس رویداد را در عملکردی مدیریت کند که اعلانهای فشار هدفمند را برای کاربران ارسال میکند.
در مثالی دیگر، افزونه ای که برای کمک به برنامه ها در پردازش تصاویر بزرگ طراحی شده است، ممکن است پس از تکمیل تغییر اندازه تصویر، رویدادی را منتشر کند. برنامههایی که این برنامه افزودنی نصب شده میتوانند با بهروزرسانی پیوندهای موجود در برنامه برای اشاره به نسخههای تغییر اندازه تصویر، رویداد تکمیل را مدیریت کنند.
یک رویداد را در یک کانال منتشر کنید
رویدادهای Eventarc در کانال ها منتشر می شوند. کانال ها راهی برای گروه بندی رویدادهای مرتبط و مدیریت مجوزهای دسترسی هستند. هنگامی که یک برنامه افزودنی را نصب می کنید یا تابعی را مستقر می کنید که رویدادهای سفارشی را مصرف می کند، Firebase به طور خودکار یک کانال پیش فرض به نام firebase در منطقه us-central1 ایجاد می کند. Firebase Admin SDK یک بسته فرعی eventarc برای انتشار در کانال ها فراهم می کند.
برای انتشار یک رویداد از یک سرور قابل اعتماد (یا عملکرد دیگری) با استفاده از کانال پیش فرض:
import{getEventarc}from'firebase-admin/eventarc';getEventarc().channel().publish({type:'achieved-leaderboard',subject:'Welcome to the top 10',data:{message:'You have achieved the nth position in our leaderboard! To see . . .'}});
علاوه بر ایجاد خودکار کانال پیشفرض، Firebase متغیر محیطی EVENTARC_CLOUD_EVENT_SOURCE را تنظیم میکند که منبع رویداد را مشخص میکند. اگر رویدادهایی را خارج از Cloud Functions for Firebase منتشر میکنید، باید صریحاً قسمت source در بار رویداد خود اضافه کنید.
رویدادهای سفارشی را مدیریت کنید
میتوانید همه رویدادهای سفارشی، از جمله رویدادهای برنامههای افزودنی را با کنترلکنندههای onCustomEventPublished یا on_custom_event_published مدیریت کنید. ابتدا این کنترلر را از Eventarc SDK به همراه Firebase Admin SDK وارد کنید:
در کد تابع خود، نام رویداد را همانطور که برای تابع مثال نشان داده شده است، وارد کنید:
Node.js
exports.onimageresized=onCustomEventPublished("firebase.extensions.storage-resize-images.v1.complete",(event)=>{logger.info("Received image resize completed event",event);// For example, write resized image details into Firestore.returngetFirestore().collection("images").doc(event.subject.replace("/","_"))// original file path.set(event.data);// resized images paths and sizes});
@eventarc_fn.on_custom_event_published(event_type="firebase.extensions.storage-resize-images.v1.complete")defonimageresized(event:eventarc_fn.CloudEvent)-> None:print("Received image resize completed event: ",event.type)ifnotisinstance(event.subject,str):print("No 'subject' data.")return# For example, write resized image details into Firestore.firestore_client:google.cloud.firestore.Client=firestore.client()collection=firestore_client.collection("images")doc=collection.document(event.subject.replace("/","_"))# original file pathdoc.set(event.data)# resized images paths and sizes
برای هر برنامه افزودنی خاص، باری که در شی رویداد بازگردانده می شود، داده هایی را ارائه می دهد که می توانید از آنها برای اجرای منطق سفارشی برای جریان برنامه خود استفاده کنید. در این مورد، این تابع از Admin SDK برای کپی ابرداده مربوط به تصویر تغییر اندازه در مجموعه ای در Cloud Firestore ، به دست آوردن نام فایل از subject ارائه شده توسط رویداد، و ذخیره فراداده از data ارائه شده توسط رویداد استفاده می کند.
انتشار و مدیریت رویدادها در کانال های غیر پیش فرض
کانالهای سفارشی میتوانند برای مواردی مفید باشند که نیاز به مجوز خاص یا سایر الزامات دارید و سطح دید و دسترسی یکسانی برای همه رویدادها نمیخواهید. می توانید کانال های خود را با استفاده از کنسول Google Cloud ایجاد کنید. انتشار و عضویت رویدادها باید در همین کانال انجام شود.
در مواردی که یک رویداد سفارشی در یک کانال غیر پیشفرض منتشر میشود، باید کانال را در کد تابع خود مشخص کنید. برای مثال، اگر میخواهید رویدادهایی را که در یک کانال غیر پیشفرض برای مکان us-west1 منتشر میشوند مدیریت کنید، باید کانال را مطابق شکل مشخص کنید:
تاریخ آخرین بهروزرسانی 2025-09-04 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-09-04 بهوقت ساعت هماهنگ جهانی."],[],[],null,["\u003cbr /\u003e\n\nWith Cloud Functions (2nd gen), you can trigger functions in response to *custom\nevents* . These are events provided by special or additional event providers, as\nopposed to the Firebase events natively supported by the Firebase SDK for Cloud Functions.\nVia custom event triggers, your app can respond to events provided by\nFirebase Extensions, or you can publish your own custom\nevents and trigger functions in response to them.\n| **Note:** This feature is a public preview. This means that the functionality might change in backward-incompatible ways. A preview release is not subject to any SLA or deprecation policy and may receive limited or no support.\n\nAll custom events conform to the\n[CloudEvents JSON event format](https://cloud.google.com/eventarc/docs/workflows/cloudevents)\nand are published to [Eventarc](https://cloud.google.com/eventarc/docs/overview).\nEventarc\n[usage fees](https://cloud.google.com/eventarc/pricing) apply.\n\nTrigger functions with custom events\n\nYou can publish custom events (or obtain events from Firebase extensions) and\ntrigger functions in response to those events by implementing this basic flow:\n\n1. Publish the desired events to an Eventarc channel, or identify available events provided by an extension that you have installed.\n2. In your function code, subscribe to events on the Eventarc channel with an event handler.\n3. In the function, parse the payload returned in the CloudEvent object and perform whatever custom logic your app requires.\n\nFor example, a game app might want to send notifications to users as they enter\nor leave the leaderboard of top ten competitors. This app could publish\nleaderboard events to the default channel, and then handle the event in a\nfunction that sends targeted push notifications to users.\n\nIn another\nexample, an extension designed to help apps process large images might emit an\nevent on the completion of image resizing. Apps with this extension installed\ncould handle the completion event by updating links in the app to point to\nresized versions of the image.\n\nPublish an event to a channel\n\nEventarc events are published into\n[channels](https://cloud.google.com/eventarc/docs/third-parties/create-channels).\nChannels are a way to group related events and manage access\npermissions. When you install an extension or deploy a function that consumes\ncustom events, Firebase automatically creates a default channel named\n`firebase` in the `us-central1` region. The Firebase Admin SDK provides\nan `eventarc` subpackage for publishing to channels.\n\nTo publish an event from a trusted server (or another function) using the\ndefault channel: \n\n import {getEventarc} from 'firebase-admin/eventarc';\n\n getEventarc().channel().publish({\n type: 'achieved-leaderboard',\n subject: 'Welcome to the top 10',\n data: {\n message: 'You have achieved the nth position in our leaderboard! To see . . .'\n }\n });\n\nIn addition to automatically creating the default channel, Firebase sets the\nenvironment variable `EVENTARC_CLOUD_EVENT_SOURCE`, which specifies the source\nof the event. If you are publishing events outside of Cloud Functions for Firebase,\nyou'll need to explicitly add the `source` field in your event payload.\n\nHandle custom events\n\nYou can handle all custom events, including extensions events, with the\n[`onCustomEventPublished`](/docs/reference/functions/2nd-gen/node/firebase-functions.eventarc#eventarconcustomeventpublished) or\n[`on_custom_event_published`](/docs/reference/functions/2nd-gen/python/firebase_functions.eventarc_fn#on_custom_event_published)\nhandlers. First, import this handler from the Eventarc SDK along with the\nFirebase Admin SDK: \n\nNode.js \n\n const {onCustomEventPublished} = require(\"firebase-functions/v2/eventarc\");\n const logger = require(\"firebase-functions/logger\");\n const {initializeApp} = require(\"firebase-admin/app\");\n const {getFirestore} = require(\"firebase-admin/firestore\"); \n https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Node/quickstarts/custom-events/functions/index.js#L18-L22\n\nPython \n\n from firebase_admin import firestore, initialize_app\n from firebase_functions import eventarc_fn \n https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Python/quickstarts/custom-events/functions/main.py#L16-L17\n\nIn your function code, pass in the event name as shown for the example function: \n\nNode.js \n\n exports.onimageresized = onCustomEventPublished(\n \"firebase.extensions.storage-resize-images.v1.complete\",\n (event) =\u003e {\n logger.info(\"Received image resize completed event\", event);\n // For example, write resized image details into Firestore.\n return getFirestore()\n .collection(\"images\")\n .doc(event.subject.replace(\"/\", \"_\")) // original file path\n .set(event.data); // resized images paths and sizes\n }); \n https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Node/quickstarts/custom-events/functions/index.js#L28-L37\n\nPython \n\n @eventarc_fn.on_custom_event_published(\n event_type=\"firebase.extensions.storage-resize-images.v1.complete\")\n def onimageresized(event: eventarc_fn.CloudEvent) -\u003e None:\n print(\"Received image resize completed event: \", event.type)\n\n if not isinstance(event.subject, str):\n print(\"No 'subject' data.\")\n return\n\n # For example, write resized image details into Firestore.\n firestore_client: google.cloud.firestore.Client = firestore.client()\n collection = firestore_client.collection(\"images\")\n doc = collection.document(event.subject.replace(\"/\", \"_\")) # original file path\n doc.set(event.data) # resized images paths and sizes \n https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Python/quickstarts/custom-events/functions/main.py#L25-L38\n\nFor each particular extension, the payload returned in the event object provides\ndata you can use to perform custom logic for your application flow. In this\ncase, the function uses the Admin SDK to copy metadata about the resized\nimage to a collection in Cloud Firestore, obtaining the filename from the\n`subject` provided by the event, and saving metadata from the `data` provided\nby the event.\n\nPublish and handle events on non-default channels\n\nCustom channels can be useful for cases where you have special permission needs\nor other requirements, and don't want the same level of visibility and access\nfor all events. You can create your own channels using the\n[Google Cloud console](https://console.cloud.google.com/eventarc/channels). Publishing and subscribing for events must be done on the same channel.\n\nIn cases where a custom event is published on a non-default channel,\nyou'll need to specify the channel in your function code. For example, if you\nwant to handle events that are published in a non-default channel for the\n`us-west1` location, you need to specify the channel as shown: \n\nNode.js \n\n import { onCustomEventPublished } from \"firebase-functions/v2/eventarc\";\n\n export const func = onCustomEventPublished(\n {\n eventType: \"firebase.extensions.storage-resize-images.v1.complete\",\n channel: \"locations/us-west1/channels/firebase\",\n region: \"us-west1\",\n },\n (event) =\u003e { ... });\n\nPython \n\n @eventarc_fn.on_custom_event_published(\n event_type=\"firebase.extensions.storage-resize-images.v1.complete\",\n channel=\"locations/us-west1/channels/firebase\",\n region=\"us-west1\")\n def onimageresizedwest(event: eventarc_fn.CloudEvent) -\u003e None:\n print(\"Received image resize completed event: \", event.type)\n # ... \n https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Python/quickstarts/custom-events/functions/main.py#L43-L59"]]