Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Cloud Functions cho phép nhà phát triển truy cập vào Firebase và các sự kiện Google Cloud, cùng với sức mạnh tính toán có thể mở rộng để chạy mã nhằm phản hồi các sự kiện đó. Mặc dù các ứng dụng Firebase dự kiến sẽ sử dụng Cloud Functions theo những cách riêng để đáp ứng các yêu cầu riêng của mình, nhưng các trường hợp sử dụng điển hình có thể thuộc những lĩnh vực sau:
Xem xét các trường hợp sử dụng và ví dụ cho từng danh mục mà bạn quan tâm, sau đó chuyển sang hướng dẫn Bắt đầu hoặc các hướng dẫn cụ thể về cách thực hiện cho sự kiện xác thực, sự kiện phân tích và nhiều sự kiện khác.
Thông báo cho người dùng khi có sự kiện thú vị
Nhà phát triển có thể sử dụng Cloud Functions để duy trì sự tương tác của người dùng và cung cấp cho họ thông tin mới nhất về một ứng dụng. Ví dụ: hãy xem xét một ứng dụng cho phép người dùng theo dõi hoạt động của nhau trong ứng dụng. Mỗi khi người dùng thêm chính mình làm người theo dõi của một người dùng khác, một thao tác ghi sẽ xảy ra trong Realtime Database. Sau đó, sự kiện ghi này có thể kích hoạt một hàm để tạo thông báo Firebase Cloud Messaging (FCM) nhằm cho người dùng thích hợp biết rằng họ đã có người theo dõi mới.
Hàm này sẽ kích hoạt khi ghi vào đường dẫn Realtime Database nơi người theo dõi được lưu trữ.
Hàm này tạo một thông báo để gửi qua FCM.
FCM gửi thông báo đến thiết bị của người dùng.
Để xem mã đang hoạt động, hãy xem mã mẫu trong GitHub:
Gửi email xác nhận cho người dùng đăng ký nhận bản tin.
Gửi email chào mừng khi người dùng hoàn tất quá trình đăng ký.
Gửi tin nhắn SMS xác nhận khi người dùng tạo tài khoản mới.
Thực hiện việc dọn dẹp và bảo trì cơ sở dữ liệu
Với tính năng xử lý sự kiện cơ sở dữ liệu Cloud Functions, bạn có thể sửa đổi Realtime Database hoặc Cloud Firestore để phản hồi hành vi của người dùng, duy trì hệ thống ở trạng thái mong muốn. Ví dụ: bạn có thể theo dõi các sự kiện ghi và thay đổi định dạng (ví dụ: thay đổi thành tất cả chữ hoa) của một số chuỗi trong tin nhắn của người dùng.
Sau đây là cách thức hoạt động:
Trình xử lý sự kiện cơ sở dữ liệu của hàm này theo dõi các sự kiện ghi trên một đường dẫn cụ thể và truy xuất dữ liệu sự kiện chứa nội dung của thông báo.
Hàm này xử lý văn bản để thay đổi chuỗi thành chữ hoa.
Hàm này ghi văn bản đã cập nhật trở lại cơ sở dữ liệu.
Để xem mã đang hoạt động, hãy xem mã mẫu trong GitHub:
Các trường hợp sử dụng khác về việc dọn dẹp và bảo trì cơ sở dữ liệu
Xoá hoàn toàn nội dung của người dùng đã xoá khỏi Realtime Database.
Giới hạn số lượng nút con trong cơ sở dữ liệu Firebase.
Theo dõi số lượng phần tử trong danh sách Realtime Database.
Sao chép dữ liệu từ Realtime Database sang Google Cloud BigQuery.
Chuyển văn bản thành biểu tượng cảm xúc.
Quản lý siêu dữ liệu được tính toán cho các bản ghi trong cơ sở dữ liệu.
Thực thi các tác vụ chuyên sâu trên đám mây thay vì trong ứng dụng của bạn
Bạn có thể tận dụng Cloud Functions để chuyển những công việc tiêu tốn nhiều tài nguyên (tốn nhiều CPU hoặc mạng) lên đám mây Google thay vì chạy công việc đó trên thiết bị của người dùng, từ đó cải thiện khả năng phản hồi của ứng dụng. Ví dụ: bạn có thể viết một hàm để theo dõi các lượt tải ảnh lên Cloud Storage, tải ảnh xuống phiên bản đang chạy hàm, sửa đổi ảnh rồi tải ảnh đó lên lại Cloud Storage. Bạn có thể sửa đổi bằng cách đổi kích thước, cắt hoặc chuyển đổi hình ảnh bằng các công cụ như sharp hoặc Pillow.
Một hàm sẽ kích hoạt khi một tệp hình ảnh được tải lên Cloud Storage.
Hàm này tải hình ảnh xuống và tạo một phiên bản thu nhỏ của hình ảnh đó.
Hàm này ghi vị trí của hình thu nhỏ vào cơ sở dữ liệu, nhờ đó, ứng dụng khách có thể tìm và sử dụng hình thu nhỏ đó.
Hàm này tải hình thu nhỏ lên lại Cloud Storage ở một vị trí mới.
Ứng dụng sẽ tải đường liên kết đến hình thu nhỏ xuống.
Các ví dụ khác về công việc hàng loạt trên đám mây Firebase
Định kỳ xoá các tài khoản Firebase không dùng đến Node.js | Python.
Tự động sao lưu hình ảnh đã tải lên Node.js | Python.
Gửi email hàng loạt cho người dùng.
Tổng hợp và tóm tắt dữ liệu định kỳ.
Xử lý hàng đợi công việc đang chờ xử lý.
Tích hợp với các dịch vụ và API của bên thứ ba
Cloud Functions có thể giúp ứng dụng của bạn hoạt động hiệu quả hơn với các dịch vụ khác bằng cách gọi và hiển thị các API web. Ví dụ: một ứng dụng được dùng để cộng tác trong quá trình phát triển có thể đăng các cam kết trên GitHub vào phòng trò chuyện của nhóm làm việc.
Người dùng đẩy các cam kết vào một kho lưu trữ trên GitHub.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-09-05 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."]]