คุณใช้ Cloud Functions ทำอะไรได้บ้าง
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
Cloud Functions ช่วยให้นักพัฒนาแอปเข้าถึง Firebase และGoogle Cloud
เหตุการณ์ต่างๆ รวมถึง
พลังการประมวลผลที่ปรับขนาดได้เพื่อเรียกใช้โค้ดเพื่อตอบสนองต่อเหตุการณ์เหล่านั้น แม้ว่าแอป Firebase จะใช้ Cloud Functions ในรูปแบบที่ไม่ซ้ำกันเพื่อตอบสนองความต้องการเฉพาะตัว แต่กรณีการใช้งานทั่วไปอาจอยู่ในขอบเขตต่อไปนี้
ตรวจสอบกรณีการใช้งานและตัวอย่างสำหรับแต่ละหมวดหมู่ที่คุณสนใจ
จากนั้นไปที่บทแนะนำเริ่มต้นใช้งาน
หรือคู่มือวิธีการเฉพาะ
สำหรับเหตุการณ์การตรวจสอบสิทธิ์
เหตุการณ์วิเคราะห์ และอื่นๆ
แจ้งผู้ใช้เมื่อมีสิ่งน่าสนใจเกิดขึ้น
นักพัฒนาแอปสามารถใช้ Cloud Functions เพื่อให้ผู้ใช้มีส่วนร่วมและรับข้อมูลล่าสุด
เกี่ยวกับแอปได้ ลองพิจารณาแอปที่
อนุญาตให้ผู้ใช้ติดตามกิจกรรมของกันและกันในแอป ทุกครั้งที่ผู้ใช้
เพิ่มตัวเองเป็นผู้ติดตามของผู้ใช้รายอื่น จะมีการเขียนใน Realtime Database จากนั้นเหตุการณ์การเขียนนี้จะทริกเกอร์ฟังก์ชัน
เพื่อสร้างการแจ้งเตือน Firebase Cloud Messaging (FCM) เพื่อแจ้งให้ผู้ใช้ที่เหมาะสม
ทราบว่ามีผู้ติดตามใหม่

- ฟังก์ชันจะทริกเกอร์เมื่อมีการเขียนไปยังเส้นทาง Realtime Database ที่เก็บผู้ติดตาม
- ฟังก์ชันนี้จะเขียนข้อความเพื่อส่งผ่าน FCM
- FCM จะส่งข้อความแจ้งเตือนไปยังอุปกรณ์ของผู้ใช้
หากต้องการตรวจสอบโค้ดที่ใช้งานได้ ให้ดูโค้ดตัวอย่างใน GitHub
กรณีการใช้งานการแจ้งเตือนอื่นๆ ที่น่าสนใจ
- ส่งอีเมลยืนยันถึงผู้ใช้ที่สมัครรับจดหมายข่าว
- ส่งอีเมลต้อนรับเมื่อผู้ใช้ลงชื่อสมัครใช้เสร็จสมบูรณ์
- ส่งการยืนยันทาง SMS เมื่อผู้ใช้สร้างบัญชีใหม่
ดำเนินการล้างข้อมูลและบำรุงรักษาฐานข้อมูล
Cloud Functionsการจัดการเหตุการณ์ในฐานข้อมูลช่วยให้คุณแก้ไข Realtime Database หรือ
Cloud Firestore เพื่อตอบสนองต่อพฤติกรรมของผู้ใช้ได้ ซึ่งจะช่วยให้ระบบอยู่ในสถานะที่คุณต้องการ
เช่น คุณสามารถตรวจสอบเหตุการณ์การเขียนและเปลี่ยนรูปแบบ
(เช่น เปลี่ยนเป็นตัวพิมพ์ใหญ่ทั้งหมด) ของสตริงบางรายการในข้อความของผู้ใช้ได้
ตัวอย่างการทำงานมีดังนี้

- ตัวแฮนเดิลเหตุการณ์ฐานข้อมูลของฟังก์ชันจะรอรับเหตุการณ์การเขียนในเส้นทางที่เฉพาะเจาะจง
และเรียกข้อมูลเหตุการณ์ที่มีข้อความของข้อความ
- ฟังก์ชันจะประมวลผลข้อความเพื่อเปลี่ยนสตริงเป็นตัวพิมพ์ใหญ่
- ฟังก์ชันจะเขียนข้อความที่อัปเดตกลับไปยังฐานข้อมูล
หากต้องการตรวจสอบโค้ดที่ใช้งานได้ ให้ดูโค้ดตัวอย่างใน GitHub
กรณีการใช้งานอื่นๆ ในการล้างข้อมูลและบำรุงรักษาฐานข้อมูล
- ลบเนื้อหาของผู้ใช้ที่ถูกลบออกจาก Realtime Database
- จำกัดจำนวนโหนดลูกในฐานข้อมูล Firebase
- ติดตามจำนวนองค์ประกอบในRealtime Databaseรายการ
- คัดลอกข้อมูลจาก Realtime Database ไปยัง Google Cloud BigQuery
- แปลงข้อความเป็นอีโมจิ
- จัดการข้อมูลเมตาที่คำนวณแล้วสำหรับระเบียนฐานข้อมูล
เรียกใช้งานที่ต้องใช้ทรัพยากรมากในระบบคลาวด์แทนที่จะเรียกใช้ในแอป
คุณสามารถใช้ประโยชน์จาก Cloud Functions เพื่อส่งงานที่ใช้ทรัพยากรมาก (CPU หรือเครือข่ายหนัก) ไปยังระบบคลาวด์ของ Google แทนที่จะเรียกใช้ในอุปกรณ์ของผู้ใช้ ซึ่งจะช่วยปรับปรุงการตอบสนองของแอปได้
ตัวอย่างเช่น คุณอาจเขียนฟังก์ชันเพื่อรอการอัปโหลดรูปภาพไปยัง Cloud Storage ดาวน์โหลดรูปภาพไปยังอินสแตนซ์ที่เรียกใช้ฟังก์ชัน แก้ไข และอัปโหลดกลับไปยัง Cloud Storage
การแก้ไขอาจรวมถึงการปรับขนาด การครอบตัด หรือการแปลงรูปภาพด้วยเครื่องมือ
อย่าง sharp หรือ
Pillow

- ฟังก์ชันจะทริกเกอร์เมื่อมีการอัปโหลดไฟล์รูปภาพไปยัง Cloud Storage
- ฟังก์ชันนี้จะดาวน์โหลดรูปภาพและสร้างเวอร์ชันภาพขนาดย่อของรูปภาพ
- ฟังก์ชันจะเขียนตำแหน่งภาพขนาดย่อลงในฐานข้อมูลเพื่อให้แอปไคลเอ็นต์ค้นหาและใช้งานได้
- ฟังก์ชันนี้จะอัปโหลดภาพปกกลับไปยัง Cloud Storage ในตำแหน่งใหม่
- แอปจะดาวน์โหลดลิงก์ภาพปก
ดูคำแนะนำเกี่ยวกับการจัดการเหตุการณ์ Cloud Storageเพื่อดูตัวอย่างการประมวลผลรูปภาพแบบทีละขั้นตอน
ตัวอย่างอื่นๆ ของงานแบบกลุ่มในระบบคลาวด์ของ Firebase
- ลบบัญชี Firebase ที่ไม่ได้ใช้เป็นระยะๆ Node.js | Python
- สำรองข้อมูลรูปภาพที่อัปโหลดโดยอัตโนมัติ Node.js | Python
- ส่งอีเมลจำนวนมากถึงผู้ใช้
- รวบรวมและสรุปข้อมูลเป็นระยะๆ
- ประมวลผลคิวของงานที่รอดำเนินการ
ผสานรวมกับบริการและ API ของบุคคลที่สาม
Cloud Functions ช่วยให้แอปทำงานร่วมกับบริการอื่นๆ ได้ดียิ่งขึ้นโดย
การเรียกและแสดง Web API เช่น แอปที่ใช้สำหรับการทำงานร่วมกันในการพัฒนาอาจโพสต์การคอมมิตใน GitHub ไปยังแชทรูมของเวิร์กกรุ๊ปได้

- ผู้ใช้พุชคอมมิตไปยังที่เก็บ GitHub
- ฟังก์ชัน HTTPS จะทริกเกอร์ผ่าน GitHub Webhook API
- ฟังก์ชันนี้จะส่งการแจ้งเตือนการคอมมิตไปยังช่อง Slack ของทีม
วิธีอื่นๆ ในการผสานรวมกับบริการและ API ของบุคคลที่สาม
- ใช้ Cloud Vision API ของ Google เพื่อวิเคราะห์และติดแท็กรูปภาพที่อัปโหลด
- แปลข้อความโดยใช้ Google แปลภาษา
- ใช้การตรวจสอบสิทธิ์ที่กำหนดเองเพื่อลงชื่อเข้าใช้ผู้ใช้
- ส่งคำขอไปยัง Webhook ใน Realtime Database writes
- เปิดใช้การค้นหาข้อความแบบเต็มในองค์ประกอบ Realtime Database
- ประมวลผลการชำระเงินจากผู้ใช้
- สร้างการตอบกลับอัตโนมัติสำหรับการโทรและข้อความ SMS
- สร้างแชทบอทโดยใช้ Google Assistant
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-17 UTC
[null,null,["อัปเดตล่าสุด 2025-08-17 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."]]