קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
Cloud Functions נותן למפתחים גישה ל-Firebase ולאירועים Google Cloud
בנוסף לכוח מחשוב ניתן להרחבה להפעלת קוד בתגובה לאירועים האלה. אפליקציות Firebase צפויות להשתמש ב-Cloud Functions בדרכים ייחודיות כדי לעמוד בדרישות הייחודיות שלהן, אבל תרחישים אופייניים לשימוש יכולים להיות בתחומים הבאים:
מפתחים יכולים להשתמש ב-Cloud Functions כדי לשמור על מעורבות המשתמשים ולעדכן אותם במידע רלוונטי על האפליקציה. לדוגמה, באפליקציה שמאפשרת למשתמשים לעקוב אחרי הפעילויות של משתמשים אחרים באפליקציה. בכל פעם שמשתמש מוסיף את עצמו כעוקב של משתמש אחר, מתבצעת פעולת כתיבה ב-Cloud Functions.Realtime Database לאחר מכן, אירוע הכתיבה הזה יכול להפעיל פונקציה ליצירת התראות Firebase Cloud Messaging (FCM) כדי ליידע את המשתמשים הרלוונטיים על כך שהצטרפו אליהם עוקבים חדשים.
הפונקציה מופעלת כשמתבצעות פעולות כתיבה בנתיב Realtime Database שבו מאוחסנים העוקבים.
הפונקציה כותבת הודעה לשליחה באמצעות FCM.
FCM שולח את הודעת ההתראה למכשיר של המשתמש.
כדי לבדוק קוד עובד, אפשר לעיין בקוד לדוגמה ב-GitHub:
באמצעות Cloud Functions טיפול באירועים במסד נתונים, אתם יכולים לשנות את Realtime Database או את Cloud Firestore בתגובה להתנהגות המשתמשים, וכך לשמור על המערכת במצב הרצוי. לדוגמה, אפשר לעקוב אחרי אירועי כתיבה ולשנות את הפורמט (למשל, להפוך את כל האותיות לרישיות) של מחרוזות מסוימות בהודעות של המשתמשים.
כך זה יכול לעבוד:
פונקציית ה-event handler של מסד הנתונים מקשיבה לאירועי כתיבה בנתיב ספציפי, ומאחזרת נתוני אירועים שמכילים את הטקסט של הודעות.
הפונקציה מעבדת את הטקסט כדי לשנות מחרוזות לאותיות רישיות.
הפונקציה כותבת את הטקסט המעודכן בחזרה למסד הנתונים.
כדי לבדוק קוד עובד, אפשר לעיין בקוד לדוגמה ב-GitHub:
מחיקה סופית של התוכן של משתמש שנמחק מ-Realtime Database.
הגבלת מספר צמתי הצאצא במסד נתונים של Firebase.
מעקב אחרי מספר הפריטים ברשימה Realtime Database.
העתקת נתונים מ-Realtime Database אל Google Cloud BigQuery.
המרת טקסט לאמוג'י.
ניהול מטא-נתונים מחושבים של רשומות במסד נתונים.
ביצוע משימות אינטנסיביות בענן במקום באפליקציה
אתם יכולים להשתמש ב-Cloud Functions כדי להעביר לענן של Google עבודות שדורשות הרבה משאבים (עומס גבוה על המעבד או על הרשת) במקום להריץ אותן במכשיר של המשתמש, וכך לשפר את מהירות התגובה של האפליקציה. לדוגמה, אתם יכולים לכתוב פונקציה שתאזין להעלאות של תמונות ל-Cloud Storage, תוריד את התמונה למופע שבו הפונקציה פועלת, תשנה אותה ותעלה אותה בחזרה ל-Cloud Storage. השינויים יכולים לכלול שינוי גודל, חיתוך או המרה של תמונות באמצעות כלים כמו sharp או Pillow.
פונקציה מופעלת כשקובץ תמונה מועלה אל Cloud Storage.
הפונקציה מורידה את התמונה ויוצרת גרסה ממוזערת שלה.
הפונקציה כותבת את המיקום של התמונה הממוזערת למסד הנתונים, כדי שאפליקציית לקוח תוכל למצוא אותה ולהשתמש בה.
הפונקציה מעלה את התמונה הממוזערת בחזרה אל Cloud Storage במיקום חדש.
Cloud Functions יכול לעזור לאפליקציה שלכם לפעול בצורה טובה יותר עם שירותים אחרים על ידי קריאה לממשקי API של אינטרנט וחשיפה שלהם. לדוגמה, אפליקציה שמשמשת לשיתוף פעולה בפיתוח יכולה לפרסם בחדר צ'אט של קבוצת עבודה התחייבויות (commits) ב-GitHub.
[[["התוכן קל להבנה","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-03 (שעון UTC)."],[],[],null,["\u003cbr /\u003e\n\nCloud Functions gives developers access to Firebase and Google Cloud\nevents, along with\nscalable computing power to run code in response to those events. While it's\nexpected that Firebase apps will use Cloud Functions in unique ways to meet\ntheir unique requirements, typical use cases might fall into these areas:\n\n- [Notify users when something interesting happens](#notify_users_when_something_interesting_happens).\n- [Perform database sanitization and maintenance](#perform_database_sanitization_and_maintenance).\n- [Execute intensive tasks in the cloud instead of in your app](#execute_intensive_tasks_in_the_cloud_instead_of_in_your_app).\n- [Integrate with third-party services and APIs](#integrate_with_third-party_services_and_apis).\n\nReview the use cases and examples for each category that interests\nyou, and then proceed to our [Get Started](/docs/functions/get-started) tutorial\nor to specific how-to guides\nfor [authentication events](/docs/functions/auth-events),\n[analytics events](/docs/functions/analytics-events), and more.\n\nNotify users when something interesting happens\n\nDevelopers can use Cloud Functions to keep users engaged and up to date\nwith relevant information about an app. Consider, for example, an app that\nallows users to follow one another's activities in the app. Each time a user\nadds themselves as a follower of another user, a write occurs in the\nRealtime Database. Then this write event could trigger a function\nto create Firebase Cloud Messaging (FCM) notifications to let the appropriate\nusers know that they have gained new followers.\n\n1. The function triggers on writes to the Realtime Database path where followers are stored.\n2. The function composes a message to send via .\n3. sends the notification message to the user's device.\n\nTo review working code, see the sample code in GitHub:\n\n- Node.js: [fcm-notifications](https://github.com/firebase/functions-samples/tree/main/Node/fcm-notifications)\n- Python: [fcm-notifications](https://github.com/firebase/functions-samples/tree/main/Python/fcm-notifications)\n\nOther interesting notification use cases\n\n- Send confirmation emails to users subscribing to a newsletter.\n- Send a welcome email when a user completes signup.\n- Send an SMS confirmation when a user creates a new account.\n\nPerform database sanitization and maintenance\n\nWith Cloud Functions database event handling, you can modify Realtime Database or\nCloud Firestore in response to user behavior, keeping the system in your desired\nstate. For example, you could monitor write events and change the format\n(for example, change to all uppercase) of certain strings in users' messages.\nHere's how that could work:\n\n1. The function's database event handler listens for write events on a specific path, and retrieves event data containing the text of a messages.\n2. The function processes the text to change strings to uppercase.\n3. The function writes the updated text back to the database.\n\nTo review working code, see the sample code in GitHub:\n\n- Node.js: [uppercase-rtdb](https://github.com/firebase/functions-samples/tree/main/Node/quickstarts/uppercase-rtdb)\n- Python: [uppercase-rtdb](https://github.com/firebase/functions-samples/tree/main/Python/quickstarts/uppercase-rtdb)\n\nOther database sanitization and maintenance use cases\n\n- Purge a deleted user's content from Realtime Database.\n- Limit the number of child nodes in a Firebase database.\n- Track the number of elements in a Realtime Database list.\n- Copy data from Realtime Database to Google Cloud BigQuery.\n- Convert text to emoji.\n- Manage computed metadata for database records.\n\nExecute intensive tasks in the cloud instead of in your app\n\nYou can take advantage of Cloud Functions to offload to the Google\ncloud resource-intensive work (heavy CPU or networking) instead of running it\non a user's device, improving the responsiveness of your app.\nFor instance, you could write a function\nto listen for image uploads to Cloud Storage, download the image to the instance\nrunning the function, modify it, and upload it back to Cloud Storage. Your\nmodifications could include resizing, cropping, or converting images with tools\nlike [sharp](https://www.npmjs.com/package/sharp) or\n[Pillow](https://pillow.readthedocs.io/en/stable/).\n\n1. A function triggers when an image file is uploaded to Cloud Storage.\n2. The function downloads the image and creates a thumbnail version of it.\n3. The function writes that thumbnail location to the database, so a client app can find and use it.\n4. The function uploads the thumbnail back to Cloud Storage in a new location.\n5. The app downloads the thumbnail link.\n\nFor a walkthrough of an image processing example, see the guide to\n[handling Cloud Storage events](https://firebase.google.com/docs/functions/gcp-storage-events?gen=2nd).\n\nOther examples of batch jobs in the Firebase cloud\n\n- Periodically delete unused Firebase accounts [Node.js](https://github.com/firebase/functions-samples/tree/main/Node/delete-unused-accounts-cron) \\| [Python](https://github.com/firebase/functions-samples/tree/main/Python/delete-unused-accounts-cron).\n- Automatically back up uploaded images [Node.js](https://github.com/firebase/functions-samples/tree/main/Node/taskqueues-backup-images) \\| [Python](https://github.com/firebase/functions-samples/tree/main/Python/taskqueues-backup-images).\n- Send bulk email to users.\n- Aggregate and summarize data periodically.\n- Process a queue of pending work.\n\nIntegrate with third-party services and APIs\n\nCloud Functions can help your app work better with other services by\ncalling and exposing web APIs. For instance, an app used for collaboration on\ndevelopment could post GitHub commits to a workgroup chat room.\n\n1. A user pushes commits to a GitHub repo.\n2. An HTTPS function triggers via the [GitHub webhook API](https://developer.github.com/webhooks/).\n3. The function sends a notification of the commit to a team Slack channel.\n\nOther ways to integrate with third-party services and APIs\n\n- Use Google [Cloud Vision API](https://cloud.google.com/vision/) to analyze and tag uploaded images.\n- Translate messages using Google Translate.\n- Use [custom auth](https://firebase.google.com/docs/auth/where-to-start#custom-auth) to sign in users.\n- Send a request to a webhook on Realtime Database writes.\n- Enable full-text search on Realtime Database elements.\n- Process payments from users.\n- Create auto-responses to phone calls and SMS messages.\n- Create a chatbot using Google Assistant."]]