Berlangganan Topik Pub/Sub
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Menunjukkan cara berlangganan topik dan mengurai peristiwa yang diterima dengan Pub/Sub dari Cloud Functions.
Contoh kode
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page demonstrates how to subscribe to a topic using Pub/Sub from Cloud Functions.\u003c/p\u003e\n"],["\u003cp\u003eThe content provides code samples in Java, Node.js, and Python, illustrating how to parse received events.\u003c/p\u003e\n"],["\u003cp\u003eIt highlights the necessity of setting up Application Default Credentials for authenticating to Cloud Run functions.\u003c/p\u003e\n"],["\u003cp\u003eIt shows that the code will print out the data that comes in from the Pub/Sub.\u003c/p\u003e\n"]]],[],null,["# Pub/Sub Subscribe to a Topic\n\nDemonstrates how to subscribe to a topic and parse the received event with Pub/Sub from Cloud Functions.\n\nCode sample\n-----------\n\n### Java\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.cloud.functions.BackgroundFunction;\n import com.google.cloud.functions.Context;\n import functions.eventpojos.PubsubMessage;\n import java.nio.charset.StandardCharsets;\n import java.util.Base64;\n import java.util.logging.Logger;\n\n public class SubscribeToTopic implements BackgroundFunction\u003cPubsubMessage\u003e {\n private static final Logger logger = Logger.getLogger(SubscribeToTopic.class.getName());\n\n @Override\n public void accept(PubsubMessage message, Context context) {\n if (message.getData() == null) {\n logger.info(\"No message provided\");\n return;\n }\n\n String messageString = new String(\n Base64.getDecoder().decode(message.getData().getBytes(StandardCharsets.UTF_8)),\n StandardCharsets.UTF_8);\n logger.info(messageString);\n }\n }\n\n### Node.js\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n /**\n * Triggered from a message on a Cloud Pub/Sub topic.\n *\n * @param {object} pubsubMessage The Cloud Pub/Sub Message object.\n * @param {string} pubsubMessage.data The \"data\" property of the Cloud Pub/Sub Message.\n */\n exports.subscribe = pubsubMessage =\u003e {\n // Print out the data from Pub/Sub, to prove that it worked\n console.log(Buffer.from(pubsubMessage.data, 'base64').toString());\n };\n\n### Python\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n # Triggered from a message on a Cloud Pub/Sub topic.\n def subscribe(event, context):\n # Print out the data from Pub/Sub, to prove that it worked\n print(base64.b64decode(event[\"data\"]))\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=functions)."]]