CINXE.COM
Pull subscriptions | Pub/Sub Documentation | Google Cloud
<!doctype html> <html lang="en" dir="ltr"> <head> <meta name="google-signin-client-id" content="721724668570-nbkv1cfusk7kk4eni4pjvepaus73b13t.apps.googleusercontent.com"> <meta name="google-signin-scope" content="profile email https://www.googleapis.com/auth/developerprofiles https://www.googleapis.com/auth/developerprofiles.award https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/webhistory"> <meta property="og:site_name" content="Google Cloud"> <meta property="og:type" content="website"><meta name="theme-color" content="#039be5"><meta charset="utf-8"> <meta content="IE=Edge" http-equiv="X-UA-Compatible"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="manifest" href="/_pwa/cloud/manifest.json" crossorigin="use-credentials"> <link rel="preconnect" href="//www.gstatic.com" crossorigin> <link rel="preconnect" href="//fonts.gstatic.com" crossorigin> <link rel="preconnect" href="//fonts.googleapis.com" crossorigin> <link rel="preconnect" href="//apis.google.com" crossorigin> <link rel="preconnect" href="//www.google-analytics.com" crossorigin><link rel="stylesheet" href="//fonts.googleapis.com/css?family=Google+Sans:400,500,700|Google+Sans+Text:400,400italic,500,500italic,700,700italic|Roboto:400,400italic,500,500italic,700,700italic|Roboto+Mono:400,500,700&display=swap"> <link rel="stylesheet" href="//fonts.googleapis.com/css2?family=Material+Icons&family=Material+Symbols+Outlined&display=block"><link rel="stylesheet" href="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/cloud/css/app.css"> <link rel="shortcut icon" href="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/cloud/images/favicons/onecloud/favicon.ico"> <link rel="apple-touch-icon" href="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/cloud/images/favicons/onecloud/super_cloud.png"><link rel="canonical" href="https://cloud.google.com/pubsub/docs/pull"><link rel="search" type="application/opensearchdescription+xml" title="Google Cloud" href="https://cloud.google.com/s/opensearch.xml"> <link rel="alternate" hreflang="en" href="https://cloud.google.com/pubsub/docs/pull" /><link rel="alternate" hreflang="x-default" href="https://cloud.google.com/pubsub/docs/pull" /><link rel="alternate" hreflang="zh-Hans" href="https://cloud.google.com/pubsub/docs/pull?hl=zh-cn" /><link rel="alternate" hreflang="fr" href="https://cloud.google.com/pubsub/docs/pull?hl=fr" /><link rel="alternate" hreflang="de" href="https://cloud.google.com/pubsub/docs/pull?hl=de" /><link rel="alternate" hreflang="id" href="https://cloud.google.com/pubsub/docs/pull?hl=id" /><link rel="alternate" hreflang="it" href="https://cloud.google.com/pubsub/docs/pull?hl=it" /><link rel="alternate" hreflang="ja" href="https://cloud.google.com/pubsub/docs/pull?hl=ja" /><link rel="alternate" hreflang="ko" href="https://cloud.google.com/pubsub/docs/pull?hl=ko" /><link rel="alternate" hreflang="pt-BR" href="https://cloud.google.com/pubsub/docs/pull?hl=pt-br" /><link rel="alternate" hreflang="es-419" href="https://cloud.google.com/pubsub/docs/pull?hl=es-419" /><title>Pull subscriptions | Pub/Sub Documentation | Google Cloud</title> <meta property="og:title" content="Pull subscriptions | Pub/Sub Documentation | Google Cloud"><meta name="description" content="Understand how a pull subscription works. Also, explore the differences between the Pull API and the StreamingPull API"> <meta property="og:description" content="Understand how a pull subscription works. Also, explore the differences between the Pull API and the StreamingPull API"><meta property="og:url" content="https://cloud.google.com/pubsub/docs/pull"><meta property="og:image" content="https://cloud.google.com/_static/cloud/images/social-icon-google-cloud-1200-630.png"> <meta property="og:image:width" content="1200"> <meta property="og:image:height" content="630"><meta property="og:locale" content="en"><meta name="twitter:card" content="summary_large_image"><script type="application/ld+json"> { "@context": "https://schema.org", "@type": "Article", "headline": "Pull subscriptions" } </script><script type="application/ld+json"> { "@context": "https://schema.org", "@type": "BreadcrumbList", "itemListElement": [{ "@type": "ListItem", "position": 1, "name": "Pub/Sub", "item": "https://cloud.google.com/pubsub" },{ "@type": "ListItem", "position": 2, "name": "Documentation", "item": "https://cloud.google.com/pubsub/docs" },{ "@type": "ListItem", "position": 3, "name": "Pull subscriptions", "item": "https://cloud.google.com/pubsub/docs/pull" }] } </script> <link rel="stylesheet" href="/extras.css"></head> <body class="" template="page" theme="cloud-theme" type="article" layout="docs" free-trial display-toc pending> <devsite-progress type="indeterminate" id="app-progress"></devsite-progress> <section class="devsite-wrapper"> <devsite-cookie-notification-bar></devsite-cookie-notification-bar><cloudx-track userCountry="SG"></cloudx-track> <cloudx-utils-init></cloudx-utils-init> <devsite-header keep-tabs-visible> <div class="devsite-header--inner nocontent"> <div class="devsite-top-logo-row-wrapper-wrapper"> <div class="devsite-top-logo-row-wrapper"> <div class="devsite-top-logo-row"> <button type="button" id="devsite-hamburger-menu" class="devsite-header-icon-button button-flat material-icons gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Navigation menu button" visually-hidden aria-label="Open menu"> </button> <div class="devsite-product-name-wrapper"> <a href="/" class="devsite-site-logo-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Site logo" track-type="globalNav" track-name="googleCloud" track-metadata-position="nav" track-metadata-eventDetail="nav"> <picture> <img src="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/cloud/images/cloud-logo.svg" class="devsite-site-logo" alt="Google Cloud"> </picture> </a> <span class="devsite-product-name"> <ul class="devsite-breadcrumb-list" > <li class="devsite-breadcrumb-item devsite-has-google-wordmark"> </li> </ul> </span> </div> <div class="devsite-top-logo-row-middle"> <div class="devsite-header-upper-tabs"> <cloudx-tabs-nav class="upper-tabs"> <nav class="devsite-tabs-wrapper" aria-label="Upper tabs"> <tab class="devsite-active"> <a href="https://cloud.google.com/docs" track-metadata-eventdetail="https://cloud.google.com/docs" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - docs-home" track-metadata-module="primary nav" aria-label="Documentation, selected" data-category="Site-Wide Custom Events" data-label="Tab: Documentation" track-name="docs-home" track-link-column-type="single-column" > Documentation </a> </tab> <tab class="devsite-dropdown devsite-clickable "> <a href="https://cloud.google.com/docs/tech-area-overviews" track-metadata-eventdetail="https://cloud.google.com/docs/tech-area-overviews" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - technology-areas" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Technology areas" track-name="technology-areas" track-link-column-type="single-column" > Technology areas </a> <a href="#" role="button" aria-haspopup="true" aria-expanded="false" aria-label="Dropdown menu for Technology areas" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/tech-area-overviews" track-metadata-position="nav - technology-areas" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Technology areas" track-name="technology-areas" track-link-column-type="single-column" class="devsite-tabs-dropdown-toggle devsite-icon devsite-icon-arrow-drop-down"></a> <div class="devsite-tabs-dropdown" aria-label="submenu" hidden> <button class="devsite-tabs-close-button material-icons button-flat gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Close dropdown menu" aria-label="Close dropdown menu" track-type="nav" track-name="close" track-metadata-eventdetail="#" track-metadata-position="nav - technology-areas" track-metadata-module="tertiary nav">close</button> <div class="devsite-tabs-dropdown-content"> <div class="devsite-tabs-dropdown-column "> <ul class="devsite-tabs-dropdown-section "> <li class="devsite-nav-item"> <a href="https://cloud.google.com/docs/ai-ml" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/ai-ml" track-metadata-position="nav - technology-areas" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> AI and ML </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/docs/application-development" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/application-development" track-metadata-position="nav - technology-areas" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Application development </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/docs/application-hosting" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/application-hosting" track-metadata-position="nav - technology-areas" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Application hosting </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/docs/compute-area" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/compute-area" track-metadata-position="nav - technology-areas" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Compute </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/docs/data" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/data" track-metadata-position="nav - technology-areas" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Data analytics and pipelines </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/docs/databases" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/databases" track-metadata-position="nav - technology-areas" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Databases </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/docs/dhm-cloud" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/dhm-cloud" track-metadata-position="nav - technology-areas" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Distributed, hybrid, and multicloud </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/docs/generative-ai" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/generative-ai" track-metadata-position="nav - technology-areas" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Generative AI </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/docs/industry" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/industry" track-metadata-position="nav - technology-areas" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Industry solutions </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/docs/networking" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/networking" track-metadata-position="nav - technology-areas" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Networking </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/docs/observability" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/observability" track-metadata-position="nav - technology-areas" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Observability and monitoring </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/docs/security" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/security" track-metadata-position="nav - technology-areas" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Security </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/docs/storage" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/storage" track-metadata-position="nav - technology-areas" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Storage </div> </a> </li> </ul> </div> </div> </div> </tab> <tab class="devsite-dropdown devsite-clickable "> <a href="https://cloud.google.com/docs/cross-product-overviews" track-metadata-eventdetail="https://cloud.google.com/docs/cross-product-overviews" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - crossproduct" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Cross-product tools" track-name="crossproduct" track-link-column-type="single-column" > Cross-product tools </a> <a href="#" role="button" aria-haspopup="true" aria-expanded="false" aria-label="Dropdown menu for Cross-product tools" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/cross-product-overviews" track-metadata-position="nav - crossproduct" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Cross-product tools" track-name="crossproduct" track-link-column-type="single-column" class="devsite-tabs-dropdown-toggle devsite-icon devsite-icon-arrow-drop-down"></a> <div class="devsite-tabs-dropdown" aria-label="submenu" hidden> <button class="devsite-tabs-close-button material-icons button-flat gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Close dropdown menu" aria-label="Close dropdown menu" track-type="nav" track-name="close" track-metadata-eventdetail="#" track-metadata-position="nav - crossproduct" track-metadata-module="tertiary nav">close</button> <div class="devsite-tabs-dropdown-content"> <div class="devsite-tabs-dropdown-column "> <ul class="devsite-tabs-dropdown-section "> <li class="devsite-nav-item"> <a href="https://cloud.google.com/docs/access-resources" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/access-resources" track-metadata-position="nav - crossproduct" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Access and resources management </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/docs/costs-usage" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/costs-usage" track-metadata-position="nav - crossproduct" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Costs and usage management </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/docs/devtools" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/devtools" track-metadata-position="nav - crossproduct" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Google Cloud SDK, languages, frameworks, and tools </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/docs/iac" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/iac" track-metadata-position="nav - crossproduct" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Infrastructure as code </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/docs/migration" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/docs/migration" track-metadata-position="nav - crossproduct" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Migration </div> </a> </li> </ul> </div> </div> </div> </tab> <tab class="devsite-dropdown devsite-clickable "> <a href="https://cloud.google.com/" track-metadata-eventdetail="https://cloud.google.com/" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - related-sites" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Related sites" track-name="related-sites" track-link-column-type="single-column" > Related sites </a> <a href="#" role="button" aria-haspopup="true" aria-expanded="false" aria-label="Dropdown menu for Related sites" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/" track-metadata-position="nav - related-sites" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Related sites" track-name="related-sites" track-link-column-type="single-column" class="devsite-tabs-dropdown-toggle devsite-icon devsite-icon-arrow-drop-down"></a> <div class="devsite-tabs-dropdown" aria-label="submenu" hidden> <button class="devsite-tabs-close-button material-icons button-flat gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Close dropdown menu" aria-label="Close dropdown menu" track-type="nav" track-name="close" track-metadata-eventdetail="#" track-metadata-position="nav - related-sites" track-metadata-module="tertiary nav">close</button> <div class="devsite-tabs-dropdown-content"> <div class="devsite-tabs-dropdown-column "> <ul class="devsite-tabs-dropdown-section "> <li class="devsite-nav-item"> <a href="https://cloud.google.com/" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/" track-metadata-position="nav - related-sites" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Google Cloud Home </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/free" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/free" track-metadata-position="nav - related-sites" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Free Trial and Free Tier </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/architecture" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/architecture" track-metadata-position="nav - related-sites" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Architecture Center </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/blog" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/blog" track-metadata-position="nav - related-sites" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Blog </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/contact" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/contact" track-metadata-position="nav - related-sites" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Contact Sales </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/developers" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/developers" track-metadata-position="nav - related-sites" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Google Cloud Developer Center </div> </a> </li> <li class="devsite-nav-item"> <a href="https://developers.google.com/" track-type="nav" track-metadata-eventdetail="https://developers.google.com/" track-metadata-position="nav - related-sites" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Google Developer Center </div> </a> </li> <li class="devsite-nav-item"> <a href="https://console.cloud.google.com/marketplace" track-type="nav" track-metadata-eventdetail="https://console.cloud.google.com/marketplace" track-metadata-position="nav - related-sites" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Google Cloud Marketplace </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/marketplace/docs" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/marketplace/docs" track-metadata-position="nav - related-sites" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Google Cloud Marketplace Documentation </div> </a> </li> <li class="devsite-nav-item"> <a href="https://www.cloudskillsboost.google/paths" track-type="nav" track-metadata-eventdetail="https://www.cloudskillsboost.google/paths" track-metadata-position="nav - related-sites" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Google Cloud Skills Boost </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/solutions" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/solutions" track-metadata-position="nav - related-sites" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Google Cloud Solution Center </div> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/support-hub" track-type="nav" track-metadata-eventdetail="https://cloud.google.com/support-hub" track-metadata-position="nav - related-sites" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Google Cloud Support </div> </a> </li> <li class="devsite-nav-item"> <a href="https://www.youtube.com/@googlecloudtech" track-type="nav" track-metadata-eventdetail="https://www.youtube.com/@googlecloudtech" track-metadata-position="nav - related-sites" track-metadata-module="tertiary nav" tooltip > <div class="devsite-nav-item-title"> Google Cloud Tech Youtube Channel </div> </a> </li> </ul> </div> </div> </div> </tab> </nav> </cloudx-tabs-nav> </div> <devsite-search enable-signin enable-search enable-suggestions project-name="Pub/Sub Documentation" tenant-name="Google Cloud" project-scope="/pubsub/docs" url-scoped="https://cloud.google.com/s/results/pubsub/docs" > <form class="devsite-search-form" action="https://cloud.google.com/s/results" method="GET"> <div class="devsite-search-container"> <button type="button" search-open class="devsite-search-button devsite-header-icon-button button-flat material-icons" aria-label="Open search"></button> <div class="devsite-searchbox"> <input aria-activedescendant="" aria-autocomplete="list" aria-label="Search" aria-expanded="false" aria-haspopup="listbox" autocomplete="off" class="devsite-search-field devsite-search-query" name="q" placeholder="Search" role="combobox" type="text" value="" > <div class="devsite-search-image material-icons" aria-hidden="true"> </div> <div class="devsite-search-shortcut-icon-container" aria-hidden="true"> <kbd class="devsite-search-shortcut-icon">/</kbd> </div> </div> </div> </form> <button type="button" search-close class="devsite-search-button devsite-header-icon-button button-flat material-icons" aria-label="Close search"></button> </devsite-search> </div> <devsite-language-selector> <ul role="presentation"> <li role="presentation"> <a role="menuitem" lang="en" >English</a> </li> <li role="presentation"> <a role="menuitem" lang="de" >Deutsch</a> </li> <li role="presentation"> <a role="menuitem" lang="es_419" >Español – América Latina</a> </li> <li role="presentation"> <a role="menuitem" lang="fr" >Français</a> </li> <li role="presentation"> <a role="menuitem" lang="id" >Indonesia</a> </li> <li role="presentation"> <a role="menuitem" lang="it" >Italiano</a> </li> <li role="presentation"> <a role="menuitem" lang="pt_br" >Português – Brasil</a> </li> <li role="presentation"> <a role="menuitem" lang="zh_cn" >中文 – 简体</a> </li> <li role="presentation"> <a role="menuitem" lang="ja" >日本語</a> </li> <li role="presentation"> <a role="menuitem" lang="ko" >한국어</a> </li> </ul> </devsite-language-selector> <devsite-user enable-profiles fp-auth id="devsite-user"> <span class="button devsite-top-button" aria-hidden="true" visually-hidden>Sign in</span> </devsite-user> </div> </div> </div> <div class="devsite-collapsible-section "> <div class="devsite-header-background"> <div class="devsite-product-id-row" hidden> <div class="devsite-product-description-row"> </div> </div> <div class="devsite-doc-set-nav-row"> <ul class="devsite-breadcrumb-list" > <li class="devsite-breadcrumb-item "> <a href="https://cloud.google.com/pubsub" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Lower Header" data-value="1" track-type="globalNav" track-name="breadcrumb" track-metadata-position="1" track-metadata-eventdetail="Pub/Sub" > Pub/Sub </a> </li> </ul> <cloudx-tabs-nav class="lower-tabs"> <nav class="devsite-tabs-wrapper" aria-label="Lower tabs"> <tab > <a href="https://cloud.google.com/pubsub/docs" track-metadata-eventdetail="https://cloud.google.com/pubsub/docs" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - overview" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Overview" track-name="overview" > Overview </a> </tab> <tab class="devsite-active"> <a href="https://cloud.google.com/pubsub/docs/overview" track-metadata-eventdetail="https://cloud.google.com/pubsub/docs/overview" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - guides" track-metadata-module="primary nav" aria-label="Guides, selected" data-category="Site-Wide Custom Events" data-label="Tab: Guides" track-name="guides" > Guides </a> </tab> <tab > <a href="https://cloud.google.com/pubsub/docs/apis" track-metadata-eventdetail="https://cloud.google.com/pubsub/docs/apis" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - reference" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Reference" track-name="reference" > Reference </a> </tab> <tab > <a href="https://cloud.google.com/pubsub/docs/samples" track-metadata-eventdetail="https://cloud.google.com/pubsub/docs/samples" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - samples" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Samples" track-name="samples" > Samples </a> </tab> <tab > <a href="https://cloud.google.com/pubsub/docs/media" track-metadata-eventdetail="https://cloud.google.com/pubsub/docs/media" class="devsite-tabs-content gc-analytics-event " track-type="nav" track-metadata-position="nav - resources" track-metadata-module="primary nav" data-category="Site-Wide Custom Events" data-label="Tab: Resources" track-name="resources" > Resources </a> </tab> </nav> </cloudx-tabs-nav> <div class="devsite-product-button-row"> <a href="https://cloud.google.com/contact" class="cta-button-secondary button " track-name="sales" track-metadata-eventDetail="nav" data-overflow-container="left" data-overflow="devsite-tabs-wrapper" data-overflow-wrapper="tab" track-metadata-position="nav" track-type="contact" >Contact Us</a> <a href="//console.cloud.google.com/freetrial" class="cloud-free-trial-button cta-button-primary button-primary button cloud-button cloud-button--primary " track-name="gcpCta" referrerpolicy="no-referrer-when-downgrade" track-type="freeTrial" track-metadata-position="nav" track-metadata-eventDetail="nav" data-overflow-class="devsite-header-link devsite-top-button button cloud-free-trial-button cloud-free-trial-enabled cloud-button cloud-button--primary" data-overflow-container="right" data-overflow="devsite-top-logo-row" >Start free</a> </div> </div> </div> </div> </div> </devsite-header> <devsite-book-nav scrollbars > <div class="devsite-book-nav-filter" > <span class="filter-list-icon material-icons" aria-hidden="true"></span> <input type="text" placeholder="Filter" aria-label="Type to filter" role="searchbox"> <span class="filter-clear-button hidden" data-title="Clear filter" aria-label="Clear filter" role="button" tabindex="0"></span> </div> <nav class="devsite-book-nav devsite-nav nocontent" aria-label="Side menu"> <div class="devsite-mobile-header"> <button type="button" id="devsite-close-nav" class="devsite-header-icon-button button-flat material-icons gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Close navigation" aria-label="Close navigation"> </button> <div class="devsite-product-name-wrapper"> <a href="/" class="devsite-site-logo-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Site logo" track-type="globalNav" track-name="googleCloud" track-metadata-position="nav" track-metadata-eventDetail="nav"> <picture> <img src="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/cloud/images/cloud-logo.svg" class="devsite-site-logo" alt="Google Cloud"> </picture> </a> <span class="devsite-product-name"> <ul class="devsite-breadcrumb-list" > <li class="devsite-breadcrumb-item devsite-has-google-wordmark"> </li> </ul> </span> </div> </div> <div class="devsite-book-nav-wrapper"> <div class="devsite-mobile-nav-top"> <ul class="devsite-nav-list"> <li class="devsite-nav-item"> <a href="/docs" class="devsite-nav-title gc-analytics-event devsite-nav-active" data-category="Site-Wide Custom Events" data-label="Tab: Documentation" track-name="docs-home" track-link-column-type="single-column" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Documentation" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Documentation </span> </a> <ul class="devsite-nav-responsive-tabs"> <li class="devsite-nav-item"> <a href="/pubsub/docs" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Tab: Overview" track-name="overview" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Overview" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Overview </span> </a> </li> <li class="devsite-nav-item"> <a href="/pubsub/docs/overview" class="devsite-nav-title gc-analytics-event devsite-nav-has-children devsite-nav-active" data-category="Site-Wide Custom Events" data-label="Tab: Guides" track-name="guides" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Guides" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip menu="_book"> Guides </span> <span class="devsite-nav-icon material-icons" data-icon="forward" menu="_book"> </span> </a> </li> <li class="devsite-nav-item"> <a href="/pubsub/docs/apis" class="devsite-nav-title gc-analytics-event devsite-nav-has-children " data-category="Site-Wide Custom Events" data-label="Tab: Reference" track-name="reference" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Reference" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Reference </span> <span class="devsite-nav-icon material-icons" data-icon="forward" > </span> </a> </li> <li class="devsite-nav-item"> <a href="/pubsub/docs/samples" class="devsite-nav-title gc-analytics-event devsite-nav-has-children " data-category="Site-Wide Custom Events" data-label="Tab: Samples" track-name="samples" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Samples" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Samples </span> <span class="devsite-nav-icon material-icons" data-icon="forward" > </span> </a> </li> <li class="devsite-nav-item"> <a href="/pubsub/docs/media" class="devsite-nav-title gc-analytics-event devsite-nav-has-children " data-category="Site-Wide Custom Events" data-label="Tab: Resources" track-name="resources" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Resources" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Resources </span> <span class="devsite-nav-icon material-icons" data-icon="forward" > </span> </a> </li> </ul> </li> <li class="devsite-nav-item"> <a href="/docs/tech-area-overviews" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Tab: Technology areas" track-name="technology-areas" track-link-column-type="single-column" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Technology areas" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Technology areas </span> </a> <ul class="devsite-nav-responsive-tabs devsite-nav-has-menu "> <li class="devsite-nav-item"> <span class="devsite-nav-title" tooltip data-category="Site-Wide Custom Events" data-label="Tab: Technology areas" track-name="technology-areas" track-link-column-type="single-column" > <span class="devsite-nav-text" tooltip menu="Technology areas"> More </span> <span class="devsite-nav-icon material-icons" data-icon="forward" menu="Technology areas"> </span> </span> </li> </ul> </li> <li class="devsite-nav-item"> <a href="/docs/cross-product-overviews" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Tab: Cross-product tools" track-name="crossproduct" track-link-column-type="single-column" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Cross-product tools" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Cross-product tools </span> </a> <ul class="devsite-nav-responsive-tabs devsite-nav-has-menu "> <li class="devsite-nav-item"> <span class="devsite-nav-title" tooltip data-category="Site-Wide Custom Events" data-label="Tab: Cross-product tools" track-name="crossproduct" track-link-column-type="single-column" > <span class="devsite-nav-text" tooltip menu="Cross-product tools"> More </span> <span class="devsite-nav-icon material-icons" data-icon="forward" menu="Cross-product tools"> </span> </span> </li> </ul> </li> <li class="devsite-nav-item"> <a href="/" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Tab: Related sites" track-name="related-sites" track-link-column-type="single-column" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Related sites" track-type="globalNav" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Related sites </span> </a> <ul class="devsite-nav-responsive-tabs devsite-nav-has-menu "> <li class="devsite-nav-item"> <span class="devsite-nav-title" tooltip data-category="Site-Wide Custom Events" data-label="Tab: Related sites" track-name="related-sites" track-link-column-type="single-column" > <span class="devsite-nav-text" tooltip menu="Related sites"> More </span> <span class="devsite-nav-icon material-icons" data-icon="forward" menu="Related sites"> </span> </span> </li> </ul> </li> <li class="devsite-nav-item"> <a href="//console.cloud.google.com/" class="devsite-nav-title gc-analytics-event " track-metadata-eventDetail="nav" track-metadata-position="nav" track-name="console" track-type="globalNav" referrerpolicy="no-referrer-when-downgrade" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Console" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Console </span> </a> </li> <li class="devsite-nav-item"> <a href="/contact" class="cta-button-secondary button" track-name="sales" track-metadata-eventDetail="nav" data-overflow-container="left" data-overflow="devsite-tabs-wrapper" data-overflow-wrapper="tab" track-metadata-position="nav" track-type="contact" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Contact Us" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Contact Us </span> </a> </li> <li class="devsite-nav-item"> <a href="//console.cloud.google.com/freetrial" class="cloud-free-trial-button cta-button-primary button-primary button cloud-button cloud-button--primary" track-name="gcpCta" referrerpolicy="no-referrer-when-downgrade" track-type="freeTrial" track-metadata-position="nav" track-metadata-eventDetail="nav" data-overflow-class="devsite-header-link devsite-top-button button cloud-free-trial-button cloud-free-trial-enabled cloud-button cloud-button--primary" data-overflow-container="right" data-overflow="devsite-top-logo-row" data-category="Site-Wide Custom Events" data-label="Responsive Tab: Start free" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Start free </span> </a> </li> </ul> </div> <div class="devsite-mobile-nav-bottom"> <ul class="devsite-nav-list" menu="_book"> <li class="devsite-nav-item devsite-nav-heading"><div class="devsite-nav-title devsite-nav-title-no-path"> <span class="devsite-nav-text" tooltip>Discover</span> </div></li> <li class="devsite-nav-item"><a href="/pubsub/docs/overview" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/overview" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/overview" ><span class="devsite-nav-text" tooltip>Product overview</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/choosing-pubsub-or-lite" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/choosing-pubsub-or-lite" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/choosing-pubsub-or-lite" ><span class="devsite-nav-text" tooltip>Choose Pub/Sub or Pub/Sub Lite</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/choosing-pubsub-or-cloud-tasks" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/choosing-pubsub-or-cloud-tasks" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/choosing-pubsub-or-cloud-tasks" ><span class="devsite-nav-text" tooltip>Choose Pub/Sub or Cloud Tasks</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/architecture" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/architecture" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/architecture" ><span class="devsite-nav-text" tooltip>Pub/Sub architectural overview</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/reliability-intro" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/reliability-intro" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/reliability-intro" ><span class="devsite-nav-text" tooltip>Pub/Sub reliability</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/pubsub-lite-documentation" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/pubsub-lite-documentation" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/pubsub-lite-documentation" ><span class="devsite-nav-text" tooltip>Pub/Sub Lite documentation</span></a></li> <li class="devsite-nav-item devsite-nav-heading"><div class="devsite-nav-title devsite-nav-title-no-path"> <span class="devsite-nav-text" tooltip>Get started</span> </div></li> <li class="devsite-nav-item"><a href="/pubsub/docs/pubsub-basics" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/pubsub-basics" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/pubsub-basics" ><span class="devsite-nav-text" tooltip>Pub/Sub service overview</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/publish-receive-messages-client-library" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/publish-receive-messages-client-library" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/publish-receive-messages-client-library" ><span class="devsite-nav-text" tooltip>Publish and subscribe with client libraries</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/publish-receive-messages-gcloud" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/publish-receive-messages-gcloud" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/publish-receive-messages-gcloud" ><span class="devsite-nav-text" tooltip>Publish and subscribe with the gcloud CLI</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/publish-receive-messages-console" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/publish-receive-messages-console" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/publish-receive-messages-console" ><span class="devsite-nav-text" tooltip>Publish and subscribe with the console</span></a></li> <li class="devsite-nav-item devsite-nav-heading"><div class="devsite-nav-title devsite-nav-title-no-path"> <span class="devsite-nav-text" tooltip>Secure and control access</span> </div></li> <li class="devsite-nav-item"><a href="/pubsub/docs/authentication" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/authentication" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/authentication" ><span class="devsite-nav-text" tooltip>Authenticate to Pub/Sub</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/access-control" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/access-control" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/access-control" ><span class="devsite-nav-text" tooltip>Access control for Pub/Sub</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/encryption" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/encryption" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/encryption" ><span class="devsite-nav-text" tooltip>Configure message encryption</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/resource-location-restriction" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/resource-location-restriction" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/resource-location-restriction" ><span class="devsite-nav-text" tooltip>Configure message storage policies</span></a></li> <li class="devsite-nav-item devsite-nav-heading"><div class="devsite-nav-title devsite-nav-title-no-path"> <span class="devsite-nav-text" tooltip>Publish messages</span> </div></li> <li class="devsite-nav-item"><a href="/pubsub/docs/publish-message-overview" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/publish-message-overview" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/publish-message-overview" ><span class="devsite-nav-text" tooltip>Publish message overview</span></a></li> <li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Create a topic</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/pubsub/docs/create-topic" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/create-topic" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/create-topic" ><span class="devsite-nav-text" tooltip>Standard topic</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/create-aws-kinesis-import-topic" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/create-aws-kinesis-import-topic" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/create-aws-kinesis-import-topic" ><span class="devsite-nav-text" tooltip>Amazon Kinesis Data Streams import topic</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/create-cloud-storage-import-topic" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/create-cloud-storage-import-topic" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/create-cloud-storage-import-topic" ><span class="devsite-nav-text" tooltip>Cloud Storage import topic</span></a></li></ul></div></li> <li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Manage topics</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/pubsub/docs/change-topic-type" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/change-topic-type" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/change-topic-type" ><span class="devsite-nav-text" tooltip>Change topic type</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/delete-topic" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/delete-topic" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/delete-topic" ><span class="devsite-nav-text" tooltip>Delete topics</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/list-topics" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/list-topics" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/list-topics" ><span class="devsite-nav-text" tooltip>List topics</span></a></li></ul></div></li> <li class="devsite-nav-item"><a href="/pubsub/docs/publisher" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/publisher" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/publisher" ><span class="devsite-nav-text" tooltip>Publish messages</span></a></li> <li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Configure publishing options</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/pubsub/docs/compress-messages" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/compress-messages" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/compress-messages" ><span class="devsite-nav-text" tooltip>Compress messages</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/retry-requests" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/retry-requests" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/retry-requests" ><span class="devsite-nav-text" tooltip>Retry requests</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/flow-control-messages" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/flow-control-messages" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/flow-control-messages" ><span class="devsite-nav-text" tooltip>Flow control</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/concurrency-control-messages" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/concurrency-control-messages" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/concurrency-control-messages" ><span class="devsite-nav-text" tooltip>Concurrency control</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/batch-messaging" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/batch-messaging" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/batch-messaging" ><span class="devsite-nav-text" tooltip>Batch messaging</span></a></li></ul></div></li> <li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Use schemas</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/pubsub/docs/schemas" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/schemas" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/schemas" ><span class="devsite-nav-text" tooltip>Schema overview</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/create-schemas" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/create-schemas" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/create-schemas" ><span class="devsite-nav-text" tooltip>Create a schema</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/associate-schema-topic" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/associate-schema-topic" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/associate-schema-topic" ><span class="devsite-nav-text" tooltip>Associate a schema with a topic</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/publish-topics-schema" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/publish-topics-schema" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/publish-topics-schema" ><span class="devsite-nav-text" tooltip>Publish messages to a topic with a schema</span></a></li><li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Manage schemas</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/pubsub/docs/commit-schema-revision" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/commit-schema-revision" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/commit-schema-revision" ><span class="devsite-nav-text" tooltip>Commit a schema revision</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/delete-schema" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/delete-schema" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/delete-schema" ><span class="devsite-nav-text" tooltip>Delete a schema</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/delete-schema-revision" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/delete-schema-revision" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/delete-schema-revision" ><span class="devsite-nav-text" tooltip>Delete a schema revision</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/list-schemas" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/list-schemas" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/list-schemas" ><span class="devsite-nav-text" tooltip>List schemas</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/list-schema-revisions" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/list-schema-revisions" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/list-schema-revisions" ><span class="devsite-nav-text" tooltip>List revisions of a schema</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/roll-back-schemas" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/roll-back-schemas" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/roll-back-schemas" ><span class="devsite-nav-text" tooltip>Roll back a schema revision</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/view-schema-details" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/view-schema-details" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/view-schema-details" ><span class="devsite-nav-text" tooltip>View schema details</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/validate-schema-definition" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/validate-schema-definition" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/validate-schema-definition" ><span class="devsite-nav-text" tooltip>Validate a schema definition</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/validate-schema-message" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/validate-schema-message" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/validate-schema-message" ><span class="devsite-nav-text" tooltip>Validate a message for a schema</span></a></li></ul></div></li></ul></div></li> <li class="devsite-nav-item"><a href="/pubsub/docs/publish-best-practices" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/publish-best-practices" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/publish-best-practices" ><span class="devsite-nav-text" tooltip>Best practices for publishing</span></a></li> <li class="devsite-nav-item devsite-nav-heading"><div class="devsite-nav-title devsite-nav-title-no-path"> <span class="devsite-nav-text" tooltip>Receive messages</span> </div></li> <li class="devsite-nav-item"><a href="/pubsub/docs/subscription-overview" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/subscription-overview" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/subscription-overview" ><span class="devsite-nav-text" tooltip>Subscription overview</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/subscriber" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/subscriber" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/subscriber" ><span class="devsite-nav-text" tooltip>Choose a subscription type</span></a></li> <li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>How subscriptions work</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/pubsub/docs/pull" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/pull" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/pull" ><span class="devsite-nav-text" tooltip>Pull subscriptions</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/push" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/push" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/push" ><span class="devsite-nav-text" tooltip>Push subscriptions</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/bigquery" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/bigquery" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/bigquery" ><span class="devsite-nav-text" tooltip>BigQuery subscriptions</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/cloudstorage" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/cloudstorage" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/cloudstorage" ><span class="devsite-nav-text" tooltip>Cloud Storage subscriptions</span></a></li></ul></div></li> <li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Create a subscription</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/pubsub/docs/create-subscription" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/create-subscription" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/create-subscription" ><span class="devsite-nav-text" tooltip>Pull subscription</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/create-push-subscription" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/create-push-subscription" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/create-push-subscription" ><span class="devsite-nav-text" tooltip>Push subscription</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/create-bigquery-subscription" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/create-bigquery-subscription" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/create-bigquery-subscription" ><span class="devsite-nav-text" tooltip>BigQuery subscription</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/create-cloudstorage-subscription" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/create-cloudstorage-subscription" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/create-cloudstorage-subscription" ><span class="devsite-nav-text" tooltip>Cloud Storage subscription</span></a></li></ul></div></li> <li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Manage subscriptions</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/pubsub/docs/manage-subscriptions" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/manage-subscriptions" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/manage-subscriptions" ><span class="devsite-nav-text" tooltip>Change subscription type</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/list-subscriptions" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/list-subscriptions" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/list-subscriptions" ><span class="devsite-nav-text" tooltip>List subscriptions</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/detach-subscriptions" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/detach-subscriptions" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/detach-subscriptions" ><span class="devsite-nav-text" tooltip>Detatch a subscription from a topic</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/delete-subscriptions" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/delete-subscriptions" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/delete-subscriptions" ><span class="devsite-nav-text" tooltip>Delete subscriptions</span></a></li></ul></div></li> <li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Configure delivery options</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/pubsub/docs/flow-control" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/flow-control" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/flow-control" ><span class="devsite-nav-text" tooltip>Flow control</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/handling-failures" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/handling-failures" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/handling-failures" ><span class="devsite-nav-text" tooltip>Handle message failures</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/replay-overview" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/replay-overview" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/replay-overview" ><span class="devsite-nav-text" tooltip>Replay and purge messages</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/lease-management" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/lease-management" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/lease-management" ><span class="devsite-nav-text" tooltip>Lease management</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/ordering" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/ordering" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/ordering" ><span class="devsite-nav-text" tooltip>Order messages</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/subscription-message-filter" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/subscription-message-filter" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/subscription-message-filter" ><span class="devsite-nav-text" tooltip>Filter messages</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/schemas-valid" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/schemas-valid" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/schemas-valid" ><span class="devsite-nav-text" tooltip>Validate schemas</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/exactly-once-delivery" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/exactly-once-delivery" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/exactly-once-delivery" ><span class="devsite-nav-text" tooltip>Exactly-once delivery</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/concurrency-control" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/concurrency-control" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/concurrency-control" ><span class="devsite-nav-text" tooltip>Concurrency control</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/authenticate-push-subscriptions" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/authenticate-push-subscriptions" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/authenticate-push-subscriptions" ><span class="devsite-nav-text" tooltip>Authentication for push subscriptions</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/payload-unwrapping" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/payload-unwrapping" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/payload-unwrapping" ><span class="devsite-nav-text" tooltip>Payload unwrapping</span></a></li></ul></div></li> <li class="devsite-nav-item"><a href="/pubsub/docs/subscribe-best-practices" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/subscribe-best-practices" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/subscribe-best-practices" ><span class="devsite-nav-text" tooltip>Best practices for subscribing</span></a></li> <li class="devsite-nav-item devsite-nav-heading"><div class="devsite-nav-title devsite-nav-title-no-path"> <span class="devsite-nav-text" tooltip>Monitor and audit</span> </div></li> <li class="devsite-nav-item"><a href="/pubsub/docs/monitoring" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/monitoring" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/monitoring" ><span class="devsite-nav-text" tooltip>Monitor Pub/Sub in Cloud Monitoring</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/monitor-topic" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/monitor-topic" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/monitor-topic" ><span class="devsite-nav-text" tooltip>Monitor topics within Pub/Sub</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/monitor-subscription" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/monitor-subscription" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/monitor-subscription" ><span class="devsite-nav-text" tooltip>Monitor subscriptions within Pub/Sub</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/audit-logging" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/audit-logging" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/audit-logging" ><span class="devsite-nav-text" tooltip>View Pub/Sub audit logs</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/labels" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/labels" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/labels" ><span class="devsite-nav-text" tooltip>View Pub/Sub reports with labels</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/open-telemetry-tracing" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/open-telemetry-tracing" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/open-telemetry-tracing" ><span class="devsite-nav-text" tooltip>Use OpenTelemetry tracing</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/metrics-autoscaling-best-practices" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/metrics-autoscaling-best-practices" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/metrics-autoscaling-best-practices" ><span class="devsite-nav-text" tooltip>Best practices for metrics in scaling</span></a></li> <li class="devsite-nav-item devsite-nav-heading"><div class="devsite-nav-title devsite-nav-title-no-path"> <span class="devsite-nav-text" tooltip>Build Pub/Sub systems</span> </div></li> <li class="devsite-nav-item"><a href="/pubsub/docs/building-pubsub-messaging-system" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/building-pubsub-messaging-system" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/building-pubsub-messaging-system" ><span class="devsite-nav-text" tooltip>Build a one-to-many Pub/Sub system</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/replay-message" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/replay-message" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/replay-message" ><span class="devsite-nav-text" tooltip>Replay messages with seek</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/migrating-from-kafka-to-pubsub" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/migrating-from-kafka-to-pubsub" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/migrating-from-kafka-to-pubsub" ><span class="devsite-nav-text" tooltip>Migrate from Kafka to Pub/Sub</span></a></li> <li class="devsite-nav-item devsite-nav-heading"><div class="devsite-nav-title devsite-nav-title-no-path"> <span class="devsite-nav-text" tooltip>Test and integrate</span> </div></li> <li class="devsite-nav-item"><a href="/pubsub/docs/emulator" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/emulator" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/emulator" ><span class="devsite-nav-text" tooltip>Test locally with Pub/Sub emulator</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/connect_kafka" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/connect_kafka" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/connect_kafka" ><span class="devsite-nav-text" tooltip>Connect Pub/Sub to Apache Kafka</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/spring" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/spring" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/spring" ><span class="devsite-nav-text" tooltip>Pub/Sub in Spring applications</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/use-with-cloud-run" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/use-with-cloud-run" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/use-with-cloud-run" ><span class="devsite-nav-text" tooltip>Pub/Sub with Cloud Run</span></a></li> <li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Integrate Pub/Sub with Cloud Functions</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/pubsub/docs/use-with-cloud-function-triggers" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/use-with-cloud-function-triggers" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/use-with-cloud-function-triggers" ><span class="devsite-nav-text" tooltip>Pub/Sub triggers in Cloud Functions</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/optical-character-recognition" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/optical-character-recognition" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/optical-character-recognition" ><span class="devsite-nav-text" tooltip>Optical Character Recognition (OCR)</span></a></li></ul></div></li> <li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Integrate Pub/Sub with Dataflow</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/pubsub/docs/stream-messages-dataflow" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/stream-messages-dataflow" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/stream-messages-dataflow" ><span class="devsite-nav-text" tooltip>Stream from Pub/Sub to Cloud Storage</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/stream-pubsub-bigquery" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/stream-pubsub-bigquery" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/stream-pubsub-bigquery" ><span class="devsite-nav-text" tooltip>Stream from Pub/Sub to BigQuery</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/stream-processing-pubsub-dataflow" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/stream-processing-pubsub-dataflow" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/stream-processing-pubsub-dataflow" ><span class="devsite-nav-text" tooltip>Stream with Pub/Sub and Dataflow</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/pubsub-logs-to-splunk" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/pubsub-logs-to-splunk" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/pubsub-logs-to-splunk" ><span class="devsite-nav-text" tooltip>Stream logs to Splunk using Pub/Sub and Dataflow</span></a></li></ul></div></li> <li class="devsite-nav-item"><a href="/pubsub/docs/app-engine-writing-and-responding-to-pub-sub-messages" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/app-engine-writing-and-responding-to-pub-sub-messages" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/app-engine-writing-and-responding-to-pub-sub-messages" ><span class="devsite-nav-text" tooltip>Work with messages in App Engine</span></a></li> <li class="devsite-nav-item"><a href="/pubsub/docs/streaming-cloud-pub-sub-messages-over-websockets" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/streaming-cloud-pub-sub-messages-over-websockets" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/streaming-cloud-pub-sub-messages-over-websockets" ><span class="devsite-nav-text" tooltip>Stream Pub/Sub messages over WebSockets</span></a></li> <li class="devsite-nav-item devsite-nav-heading"><div class="devsite-nav-title devsite-nav-title-no-path"> <span class="devsite-nav-text" tooltip>Troubleshoot</span> </div></li> <li class="devsite-nav-item"><a href="/pubsub/docs/troubleshooting" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/troubleshooting" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/troubleshooting" ><span class="devsite-nav-text" tooltip>General troubleshooting</span></a></li> <li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Troubleshoot topics</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/pubsub/docs/topic-troubleshooting" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/topic-troubleshooting" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/topic-troubleshooting" ><span class="devsite-nav-text" tooltip>Troubleshoot standard topics</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/aws-kinesis-import-topic-troubleshooting" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/aws-kinesis-import-topic-troubleshooting" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/aws-kinesis-import-topic-troubleshooting" ><span class="devsite-nav-text" tooltip>Troubleshoot Amazon Kinesis Data Streams import topics</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/cloud-storage-import-topic-troubleshooting" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/cloud-storage-import-topic-troubleshooting" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/cloud-storage-import-topic-troubleshooting" ><span class="devsite-nav-text" tooltip>Troubleshoot Cloud Storage import topics</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/platform-logs-pubsub" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/platform-logs-pubsub" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/platform-logs-pubsub" ><span class="devsite-nav-text" tooltip>Use platform logs to troubleshoot Cloud Storage import topics</span></a></li></ul></div></li> <li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Troubleshoot subscriptions</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/pubsub/docs/pull-troubleshooting" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/pull-troubleshooting" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/pull-troubleshooting" ><span class="devsite-nav-text" tooltip>Troubleshoot pull subscriptions</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/push-troubleshooting" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/push-troubleshooting" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/push-troubleshooting" ><span class="devsite-nav-text" tooltip>Troubleshoot push subscriptions</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/payload-unwrapping-troubleshooting" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/payload-unwrapping-troubleshooting" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/payload-unwrapping-troubleshooting" ><span class="devsite-nav-text" tooltip>Troubleshoot push payload unwrapping</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/bigquery-troubleshooting" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/bigquery-troubleshooting" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/bigquery-troubleshooting" ><span class="devsite-nav-text" tooltip>Troubleshoot BigQuery subscriptions</span></a></li><li class="devsite-nav-item"><a href="/pubsub/docs/cloudstorage-troubleshooting" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/cloudstorage-troubleshooting" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/cloudstorage-troubleshooting" ><span class="devsite-nav-text" tooltip>Troubleshoot Cloud Storage subscriptions</span></a></li></ul></div></li> <li class="devsite-nav-item devsite-nav-expandable"><div class="devsite-expandable-nav"> <a class="devsite-nav-toggle" aria-hidden="true"></a><div class="devsite-nav-title devsite-nav-title-no-path" tabindex="0" role="button"> <span class="devsite-nav-text" tooltip>Troubleshoot with GCPDiag</span> </div><ul class="devsite-nav-section"><li class="devsite-nav-item"><a href="/pubsub/docs/gcpdiag-pull-troubleshooting" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/gcpdiag-pull-troubleshooting" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/gcpdiag-pull-troubleshooting" ><span class="devsite-nav-text" tooltip>Troubleshoot pull subscriptions with GCPDiag</span></a></li></ul></div></li> <li class="devsite-nav-item devsite-nav-heading"><div class="devsite-nav-title devsite-nav-title-no-path"> <span class="devsite-nav-text" tooltip>Code samples</span> </div></li> <li class="devsite-nav-item"><a href="/pubsub/docs/samples" class="devsite-nav-title gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Book nav link, pathname: /pubsub/docs/samples" track-type="bookNav" track-name="click" track-metadata-eventdetail="/pubsub/docs/samples" ><span class="devsite-nav-text" tooltip>Pub/Sub code samples</span></a></li> </ul> <ul class="devsite-nav-list" menu="Technology areas" aria-label="Side menu" hidden> <li class="devsite-nav-item"> <a href="/docs/ai-ml" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: AI and ML" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > AI and ML </span> </a> </li> <li class="devsite-nav-item"> <a href="/docs/application-development" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Application development" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Application development </span> </a> </li> <li class="devsite-nav-item"> <a href="/docs/application-hosting" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Application hosting" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Application hosting </span> </a> </li> <li class="devsite-nav-item"> <a href="/docs/compute-area" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Compute" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Compute </span> </a> </li> <li class="devsite-nav-item"> <a href="/docs/data" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Data analytics and pipelines" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Data analytics and pipelines </span> </a> </li> <li class="devsite-nav-item"> <a href="/docs/databases" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Databases" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Databases </span> </a> </li> <li class="devsite-nav-item"> <a href="/docs/dhm-cloud" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Distributed, hybrid, and multicloud" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Distributed, hybrid, and multicloud </span> </a> </li> <li class="devsite-nav-item"> <a href="/docs/generative-ai" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Generative AI" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Generative AI </span> </a> </li> <li class="devsite-nav-item"> <a href="/docs/industry" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Industry solutions" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Industry solutions </span> </a> </li> <li class="devsite-nav-item"> <a href="/docs/networking" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Networking" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Networking </span> </a> </li> <li class="devsite-nav-item"> <a href="/docs/observability" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Observability and monitoring" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Observability and monitoring </span> </a> </li> <li class="devsite-nav-item"> <a href="/docs/security" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Security" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Security </span> </a> </li> <li class="devsite-nav-item"> <a href="/docs/storage" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Storage" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Storage </span> </a> </li> </ul> <ul class="devsite-nav-list" menu="Cross-product tools" aria-label="Side menu" hidden> <li class="devsite-nav-item"> <a href="/docs/access-resources" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Access and resources management" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Access and resources management </span> </a> </li> <li class="devsite-nav-item"> <a href="/docs/costs-usage" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Costs and usage management" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Costs and usage management </span> </a> </li> <li class="devsite-nav-item"> <a href="/docs/devtools" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Google Cloud SDK, languages, frameworks, and tools" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Google Cloud SDK, languages, frameworks, and tools </span> </a> </li> <li class="devsite-nav-item"> <a href="/docs/iac" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Infrastructure as code" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Infrastructure as code </span> </a> </li> <li class="devsite-nav-item"> <a href="/docs/migration" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Migration" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Migration </span> </a> </li> </ul> <ul class="devsite-nav-list" menu="Related sites" aria-label="Side menu" hidden> <li class="devsite-nav-item"> <a href="/" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Google Cloud Home" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Google Cloud Home </span> </a> </li> <li class="devsite-nav-item"> <a href="/free" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Free Trial and Free Tier" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Free Trial and Free Tier </span> </a> </li> <li class="devsite-nav-item"> <a href="/architecture" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Architecture Center" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Architecture Center </span> </a> </li> <li class="devsite-nav-item"> <a href="https://cloud.google.com/blog" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Blog" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Blog </span> </a> </li> <li class="devsite-nav-item"> <a href="/contact" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Contact Sales" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Contact Sales </span> </a> </li> <li class="devsite-nav-item"> <a href="/developers" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Google Cloud Developer Center" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Google Cloud Developer Center </span> </a> </li> <li class="devsite-nav-item"> <a href="https://developers.google.com/" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Google Developer Center" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Google Developer Center </span> </a> </li> <li class="devsite-nav-item"> <a href="https://console.cloud.google.com/marketplace" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Google Cloud Marketplace" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Google Cloud Marketplace </span> </a> </li> <li class="devsite-nav-item"> <a href="/marketplace/docs" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Google Cloud Marketplace Documentation" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Google Cloud Marketplace Documentation </span> </a> </li> <li class="devsite-nav-item"> <a href="https://www.cloudskillsboost.google/paths" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Google Cloud Skills Boost" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Google Cloud Skills Boost </span> </a> </li> <li class="devsite-nav-item"> <a href="/solutions" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Google Cloud Solution Center" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Google Cloud Solution Center </span> </a> </li> <li class="devsite-nav-item"> <a href="/support-hub" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Google Cloud Support" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Google Cloud Support </span> </a> </li> <li class="devsite-nav-item"> <a href="https://www.youtube.com/@googlecloudtech" class="devsite-nav-title gc-analytics-event " data-category="Site-Wide Custom Events" data-label="Responsive Tab: Google Cloud Tech Youtube Channel" track-type="navMenu" track-metadata-eventDetail="globalMenu" track-metadata-position="nav"> <span class="devsite-nav-text" tooltip > Google Cloud Tech Youtube Channel </span> </a> </li> </ul> </div> </div> </nav> </devsite-book-nav> <section id="gc-wrapper"> <main role="main" class="devsite-main-content" has-book-nav has-sidebar > <div class="devsite-sidebar"> <div class="devsite-sidebar-content"> <devsite-toc class="devsite-nav" role="navigation" aria-label="On this page" depth="2" scrollbars ></devsite-toc> <devsite-recommendations-sidebar class="nocontent devsite-nav"> </devsite-recommendations-sidebar> </div> </div> <devsite-content> <article class="devsite-article"> <div class="devsite-article-meta nocontent" role="navigation"> <ul class="devsite-breadcrumb-list" aria-label="Breadcrumb"> <li class="devsite-breadcrumb-item "> <a href="https://cloud.google.com/" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="1" track-type="globalNav" track-name="breadcrumb" track-metadata-position="1" track-metadata-eventdetail="Google Cloud" > Home </a> </li> <li class="devsite-breadcrumb-item "> <div class="devsite-breadcrumb-guillemet material-icons" aria-hidden="true"></div> <a href="https://cloud.google.com/pubsub" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="2" track-type="globalNav" track-name="breadcrumb" track-metadata-position="2" track-metadata-eventdetail="Pub/Sub" > Pub/Sub </a> </li> <li class="devsite-breadcrumb-item "> <div class="devsite-breadcrumb-guillemet material-icons" aria-hidden="true"></div> <a href="https://cloud.google.com/pubsub/docs" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="3" track-type="globalNav" track-name="breadcrumb" track-metadata-position="3" track-metadata-eventdetail="Pub/Sub Documentation" > Documentation </a> </li> <li class="devsite-breadcrumb-item "> <div class="devsite-breadcrumb-guillemet material-icons" aria-hidden="true"></div> <a href="https://cloud.google.com/pubsub/docs/overview" class="devsite-breadcrumb-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Breadcrumbs" data-value="4" track-type="globalNav" track-name="breadcrumb" track-metadata-position="4" track-metadata-eventdetail="" > Guides </a> </li> </ul> <devsite-thumb-rating position="header"> </devsite-thumb-rating> </div> <devsite-feedback position="header" project-name="Pub/Sub Documentation" product-id="96707" bucket="documentation" context="" version="t-devsite-webserver-20241114-r00-rc02.464922260396498922" data-label="Send Feedback Button" track-type="feedback" track-name="sendFeedbackLink" track-metadata-position="header" class="nocontent" project-feedback-url="https://issuetracker.google.com/issues/new?component=187173&template=0" project-icon="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/cloud/images/favicons/onecloud/super_cloud.png" > <button> Send feedback </button> </devsite-feedback> <h1 class="devsite-page-title" tabindex="-1"> Pull subscriptions </h1> <devsite-feature-tooltip ack-key="AckCollectionsBookmarkTooltipDismiss" analytics-category="Site-Wide Custom Events" analytics-action-show="Callout Profile displayed" analytics-action-close="Callout Profile dismissed" analytics-label="Create Collection Callout" class="devsite-page-bookmark-tooltip nocontent" dismiss-button="true" id="devsite-collections-dropdown" dismiss-button-text="Dismiss" close-button-text="Got it"> <devsite-bookmark></devsite-bookmark> <span slot="popout-heading"> Stay organized with collections </span> <span slot="popout-contents"> Save and categorize content based on your preferences. </span> </devsite-feature-tooltip> <div class="devsite-page-title-meta"><devsite-view-release-notes></devsite-view-release-notes></div> <devsite-toc class="devsite-nav" depth="2" devsite-toc-embedded > </devsite-toc> <div class="devsite-article-body clearfix "> <div class="video-wrapper"> <iframe class="devsite-embedded-youtube-video" data-video-id="cCkwo6eEHIg" data-autohide="1" data-showinfo="0" frameborder="0" allowfullscreen> </iframe> </div> <p>This document provides an overview of a pull subscription, its workflow, and associated properties.</p> <p>In a pull subscription, a subscriber client requests messages from the Pub/Sub server.</p> <p>The pull mode can use one of the two service APIs, Pull or StreamingPull. To run the chosen API, you can select a Google-provided high-level client library, or a low-level auto-generated client library. You can also choose between asynchronous and synchronous message processing.</p> <aside class="note"><strong>Note:</strong><span> For most use cases, we recommend the Google-provided high-level client library with the StreamingPull API and asynchronous message processing.</span></aside> <h2 id="before-you-begin" data-text="Before you begin" tabindex="-1">Before you begin</h2> <p>Before reading this document, ensure that you're familiar with the following:</p> <ul> <li><p>How <a href="/pubsub/docs/overview">Pub/Sub works</a> and the different Pub/Sub terms.</p></li> <li><p>The different <a href="/pubsub/docs/subscription-overview">kinds of subscriptions</a> that Pub/Sub supports and why you might want to use a pull subscription.</p></li> </ul> <h2 id="pull-workflow" data-text="Pull subscription workflow" tabindex="-1">Pull subscription workflow</h2> <p>For a pull subscription, your subscriber client initiates requests to a Pub/Sub server to retrieve messages. The subscriber client uses one of the following APIs:</p> <ul> <li><a href="/pubsub/docs/reference/rest/v1/projects.subscriptions/pull">REST <code translate="no" dir="ltr">Pull</code>API</a></li> <li><a href="/pubsub/docs/reference/rpc/google.pubsub.v1#pullrequest">RPC <code translate="no" dir="ltr">PullRequest</code> API</a></li> <li><a href="/pubsub/docs/reference/rpc/google.pubsub.v1#streamingpullrequest">RPC <code translate="no" dir="ltr">StreamingPullRequest</code> API</a></li> </ul> <p>Most subscriber clients don't make these requests directly. Instead, the clients rely on the Google Cloud-provided high-level client library that performs streaming pull requests internally and delivers messages asynchronously. For a subscriber client that needs greater control over how messages are pulled, Pub/Sub uses a low-level and automatically generated gRPC library. This library makes pull or streaming pull requests directly. These requests can be synchronous or asynchronous.</p> <p>The following two images show the workflow between a subscriber client and a pull subscription.</p> <figure id="pull subscriber"> <img src="/static/pubsub/images/subscriber_pull.png" alt="Flow of messages for a pull subscription"> <figcaption><b>Figure 1.</b> Workflow for a pull subscription</figcaption> </figure> <p><br> <br> <figure id="streamingPull subscriber"> <img src="/static/pubsub/images/subscriber_streamingpull.png" alt="Flow of messages for a streamingPull subscription"/> <figcaption><b>Figure 2.</b> Workflow for a streaming pull subscription</figcaption> </figure></p> <h4 id="pull_workflow" data-text="Pull workflow" tabindex="-1">Pull workflow</h4> <p>The pull workflow is as follows and references Figure 1:</p> <ol> <li>The subscriber client explicitly calls the <code translate="no" dir="ltr">pull</code> method, which requests messages for delivery. This request is the <code translate="no" dir="ltr">PullRequest</code> as shown in the image.</li> <li><p>The Pub/Sub server responds with zero or more messages and acknowledgment IDs. A response with zero messages or with an error does not necessarily indicate that there are no messages available to receive. This response is the <code translate="no" dir="ltr">PullResponse</code> as shown in the image.</p></li> <li><p>The subscriber client explicitly calls the <code translate="no" dir="ltr">acknowledge</code> method. The client uses the returned acknowledgment ID to acknowledge that the message is processed and need not be delivered again.</p></li> </ol> <p>For a single streaming pull request, a subscriber client can have multiple responses returned due to the open connection. In contrast, only one response is returned for each pull request.</p> <h2 id="pull-properties" data-text="Properties of a pull subscription" tabindex="-1">Properties of a pull subscription</h2> <p>The properties that you configure for a pull subscription determines how you write messages to your subscription. For more information, see <a href="/pubsub/docs/subscription-properties">subscription properties</a>.</p> <h2 id="service_apis" data-text="Pub/Sub service APIs" tabindex="-1">Pub/Sub service APIs</h2> <p>The Pub/Sub pull subscription can use one of the following two APIs for retrieving messages:</p> <ul> <li>Pull</li> <li>StreamingPull</li> </ul> <p>Use unary Acknowledge and ModifyAckDeadline RPCs when you receive messages using these APIs. The two Pub/Sub APIs are described in the following sections.</p> <h3 id="streamingpull_api" data-text="StreamingPull API" tabindex="-1">StreamingPull API</h3> <p>Where possible, the Pub/Sub client libraries use <a href="https://grpc.io/docs/what-is-grpc/core-concepts/#bidirectional-streaming-rpc" target="external" track-type="article" track-name="externalLink" track-metadata-position="body" class="external">StreamingPull</a> for maximum throughput and lowest latency. Although you might never use the StreamingPull API directly, it's important to know how it differs from the Pull API.</p> <p>The StreamingPull API relies on a persistent bidirectional connection to receive multiple messages as they become available. The following is the workflow:</p> <ol> <li><p>The client sends a request to the server to establish a connection. If the connection quota is exceeded, the server returns a resource exhausted error. The client library retries the out-of-quota errors automatically.</p></li> <li><p>If there is no error or the connection quota is available again, the server continuously sends messages to the connected client.</p></li> <li><p>If or when the throughput quota is exceeded, the server stops sending messages. However, the connection is not broken. Whenever there's sufficient throughput quota available again, the stream resumes.</p></li> <li><p>The client or the server eventually closes the connection.</p></li> </ol> <p>The StreamingPull API keeps an open connection. The Pub/Sub servers recurrently close the connection after a time period to avoid a long-running sticky connection. The client library automatically reopens a StreamingPull connection.</p> <p>Messages are sent to the connection when they are available. The StreamingPull API thus minimizes latency and maximizes throughput for messages.</p> <aside class="note"><strong>Note:</strong><span> The PHP client library does not support the StreamingPull API.</span></aside> <p>Read more about the StreamingPull RPC methods: <a href="/pubsub/docs/reference/rpc/google.pubsub.v1#google.pubsub.v1.StreamingPullRequest">StreamingPullRequest</a> and <a href="/pubsub/docs/reference/rpc/google.pubsub.v1#google.pubsub.v1.StreamingPullResponse">StreamingPullResponse</a>.</p> <h3 id="pull_api" data-text="Pull API" tabindex="-1">Pull API</h3> <p>This API is a traditional unary RPC that is based on a request and response model. A single pull response corresponds to a single pull request. The following is the workflow:</p> <ol> <li><p>The client sends a request to the server for messages. If the throughput quota is exceeded, the server returns a resource exhausted error.</p></li> <li><p>If there is no error or the throughput quota is available again, the server replies with zero or more messages and acknowledgment IDs.</p></li> </ol> <p><strong>When using the unary Pull API, a response with zero messages or with an error does not necessarily indicate that there are no messages available to receive.</strong></p> <p>Using the Pull API does not guarantee low latency and a high throughput of messages. To achieve high throughput and low latency with the Pull API, you must have multiple simultaneous outstanding requests. New requests are created when old requests receive a response. Architecting such a solution is error-prone and hard to maintain. We recommend that you use the StreamingPull API for such use cases.</p> <p>Use the Pull API instead of the StreamingPull API only if you require strict control over the following:</p> <ul> <li>The number of messages that the subscriber client can process</li> <li>The client memory and resources</li> </ul> <p>You can also use this API when your subscriber is a proxy between Pub/Sub and another service that operates in a more pull-oriented way.</p> <p>Read more about the Pull REST methods: <a href="/pubsub/docs/reference/rest/v1/projects.subscriptions/pull">Method: projects.subscriptions.pull</a>.</p> <p>Read more about the Pull RPC methods: <a href="/pubsub/docs/reference/rpc/google.pubsub.v1#google.pubsub.v1.PullRequest">PullRequest</a> and <a href="/pubsub/docs/reference/rpc/google.pubsub.v1#pullresponse">PullResponse</a>.</p> <h2 id="types_of_message_processing_modes" data-text="Types of message processing modes" tabindex="-1">Types of message processing modes</h2> <p>Choose one of the following pull modes for your subscriber clients.</p> <h3 id="asynchronous_pull_mode" data-text="Asynchronous pull mode" tabindex="-1">Asynchronous pull mode</h3> <p>Asynchronous pull mode decouples the receiving of messages from the processing of messages in a subscriber client. This mode is the default for most subscriber clients. Asynchronous pull mode can use the StreamingPull API or unary Pull API. Asynchronous pull can also use the high-level client library or low-level auto-generated client library.</p> <p>You can learn more about client libraries later in this document.</p> <h3 id="synchronous_pull_mode" data-text="Synchronous pull mode" tabindex="-1">Synchronous pull mode</h3> <p>In synchronous pull mode, the receiving and processing of messages occur in sequence and are not decoupled from each other. Hence, similar to StreamingPull versus unary Pull APIs, asynchronous processing offers lower latency and higher throughput than synchronous processing.</p> <p>Use synchronous pull mode only for applications where low latency and high throughput are not the most important factors as compared to some other requirements. For example, an application might be limited to using only the synchronous programming model. Or, an application with resource constraints might require more exact control over memory, network, or CPU. In such cases, use synchronous mode with the unary Pull API.</p> <h2 id="client_libraries" data-text="Pub/Sub client libraries" tabindex="-1">Pub/Sub client libraries</h2> <p>Pub/Sub offers a high-level and a low-level auto-generated client library.</p> <h3 id="high_client_library" data-text="High-level Pub/Sub client library" tabindex="-1">High-level Pub/Sub client library</h3> <p>The high-level client library provides options for controlling the acknowledgment deadlines by using lease management. These options are more granular than when you configure the acknowledgment deadlines by using the console or the CLI at the subscription level. The high-level client library also implements support for features such as ordered delivery, exactly-once delivery, and flow control.</p> <p>We recommend using asynchronous pull and the StreamingPull API with the high-level client library. Not all languages that are supported for Google Cloud also support the Pull API in the high-level client library.</p> <p>To use the high-level client libraries, see <a href="/pubsub/docs/reference/libraries">Pub/Sub client libraries</a>.</p> <h3 id="low_client_library" data-text="Low-level auto-generated Pub/Sub client library" tabindex="-1">Low-level auto-generated Pub/Sub client library</h3> <p>A low-level client library is available for cases where you must use the Pull API directly. You can use synchronous or asynchronous processing with the low-level auto-generated client library. You must manually code features such as ordered delivery, exactly-once delivery, flow control, and lease management when you use the low-level auto-generated client library.</p> <p>You can use the synchronous processing model when you use the low-level auto-generated client library for all supported languages. You might use the low-level auto-generated client library and synchronous pull in cases where using the Pull API directly makes sense. For example, you might have existing application logic that relies on this model.</p> <p>To use the low-level auto-generated client libraries directly, see <a href="/pubsub/docs/reference/service_apis_overview">Pub/Sub APIs overview</a>.</p> <h2 id="client_library_code_samples" data-text="Client library code samples" tabindex="-1">Client library code samples</h2> <h3 id="streamingpull_and_high-level_client_library_code_samples" data-text="StreamingPull and high-level client library code samples" tabindex="-1">StreamingPull and high-level client library code samples</h3> <div class="ds-selector-tabs" data-ds-scope="code-sample"> <section><h3 id="c++" data-text="C++" tabindex="-1">C++</h3> <p>Before trying this sample, follow the C++ setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://googleapis.dev/cpp/google-cloud-pubsub/latest/" class="external">Pub/Sub C++ API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/google-cloud-cpp/google/cloud/pubsub/samples/samples.cc/HEAD/pubsub_subscriber_async_pull" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/google-cloud-cpp/blob/HEAD/google/cloud/pubsub/samples/samples.cc" feedback-context="{"language": "cpp", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/google-cloud-cpp/blob/HEAD/google/cloud/pubsub/samples/samples.cc"}" feedback-product="1634365" feedback-bucket="data-analytics" language="cpp" data-github-path="googleapis/google-cloud-cpp/google/cloud/pubsub/samples/samples.cc" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_async_pull" dir="ltr" is-upgraded syntax="C++"><code translate="no" dir="ltr"><span class="devsite-syntax-k">namespace</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">pubsub</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-nn">google</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-nn">cloud</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-nn">pubsub</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">auto</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">sample</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[](</span><span class="devsite-syntax-n">pubsub</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">Subscriber</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Subscribe</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[&](</span><span class="devsite-syntax-n">pubsub</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">Message</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">const</span>&<span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">m</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">pubsub</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">AckHandler</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">h</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">std</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">cout</span><span class="devsite-syntax-w"> << </span><span class="devsite-syntax-s">"Received message "</span><span class="devsite-syntax-w"> << </span><span class="devsite-syntax-n">m</span><span class="devsite-syntax-w"> << </span><span class="devsite-syntax-s">"</span><span class="devsite-syntax-se">\n</span><span class="devsite-syntax-s">"</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">std</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">move</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">h</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-n">ack</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">PleaseIgnoreThisSimplifiesTestingTheSamples</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">});</span> <span class="devsite-syntax-p">};</span></code></pre></devsite-code> </div> </section> <section><h3 id="c" data-text="C#" tabindex="-1">C#</h3> <p>Before trying this sample, follow the C# setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.PubSub.V1/" class="external">Pub/Sub C# API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="GoogleCloudPlatform/dotnet-docs-samples/pubsub/api/Pubsub.Samples/PullMessagesAsync.cs/HEAD/pubsub_subscriber_async_pull" data-code-snippet="true" data-github-includecode-link="https://github.com/GoogleCloudPlatform/dotnet-docs-samples/blob/HEAD/pubsub/api/Pubsub.Samples/PullMessagesAsync.cs" feedback-context="{"language": "csharp", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/GoogleCloudPlatform/dotnet-docs-samples/blob/HEAD/pubsub/api/Pubsub.Samples/PullMessagesAsync.cs"}" feedback-product="1634365" feedback-bucket="data-analytics" language="csharp" data-github-path="GoogleCloudPlatform/dotnet-docs-samples/pubsub/api/Pubsub.Samples/PullMessagesAsync.cs" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_async_pull" dir="ltr" is-upgraded syntax="C#"><code translate="no" dir="ltr"> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">Google.Cloud.PubSub.V1</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">System</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">System.Threading</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">System.Threading.Tasks</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">class</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nc">PullMessagesAsyncSample</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">async</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Task<int></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">PullMessagesAsync</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-kt">string</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">string</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">bool</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">acknowledge</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SubscriptionName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SubscriptionName</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">FromProjectSubscription</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SubscriberClient</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SubscriberClient</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">CreateAsync</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">subscriptionName</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// SubscriberClient runs your message handle function on multiple</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// threads to maximize throughput.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">int</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">messageCount</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">0</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Task</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">startTask</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">StartAsync</span><span class="devsite-syntax-p">((</span><span class="devsite-syntax-n">PubsubMessage</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">CancellationToken</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">cancel</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span>> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">string</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Data</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">ToStringUtf8</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">WriteLine</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">$"Message {message.MessageId}: {text}"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Interlocked</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Increment</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-k">ref</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">messageCount</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Task</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">FromResult</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">acknowledge</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">?</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SubscriberClient</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Reply</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Ack</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SubscriberClient</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Reply</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Nack</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">});</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Run for 5 seconds.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Task</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Delay</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-m">5000</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">StopAsync</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">CancellationToken</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">None</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Lets make sure that the start task finished successfully after the call to stop.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">startTask</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">messageCount</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-p">}</span></code></pre></devsite-code> </div> </section> <section><h3 id="go" data-text="Go" tabindex="-1">Go</h3> <p>Before trying this sample, follow the Go setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://godoc.org/cloud.google.com/go/pubsub" class="external">Pub/Sub Go API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="GoogleCloudPlatform/golang-samples/pubsub/subscriptions/async_pull.go/HEAD/pubsub_subscriber_async_pull" data-code-snippet="true" data-github-includecode-link="https://github.com/GoogleCloudPlatform/golang-samples/blob/HEAD/pubsub/subscriptions/async_pull.go" feedback-context="{"language": "go", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/GoogleCloudPlatform/golang-samples/blob/HEAD/pubsub/subscriptions/async_pull.go"}" feedback-product="1634365" feedback-bucket="data-analytics" language="go" data-github-path="GoogleCloudPlatform/golang-samples/pubsub/subscriptions/async_pull.go" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_async_pull" dir="ltr" is-upgraded syntax="Go"><code translate="no" dir="ltr"><span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"context"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"fmt"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"io"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"sync/atomic"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"time"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"cloud.google.com/go/pubsub"</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">func</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">pullMsgs</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">w</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">io</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Writer</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">projectID</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subID</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">string</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">error</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// projectID := "my-project-id"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// subID := "my-sub"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">context</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Background</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">pubsub</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">NewClient</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">projectID</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fmt</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Errorf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"pubsub.NewClient: %w"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">defer</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Close</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">sub</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Subscription</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">subID</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Receive messages for 10 seconds, which simplifies testing.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Comment this out in production, since `Receive` should</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// be used as a long running operation.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">cancel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">context</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">WithTimeout</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mi">10</span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-nx">time</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Second</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">defer</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">cancel</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">var</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">received</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">int32</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">sub</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Receive</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">func</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">_</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">context</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Context</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">msg</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-nx">pubsub</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Message</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fmt</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fprintf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">w</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"Got message: %q\n"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">string</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">msg</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Data</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">atomic</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">AddInt32</span><span class="devsite-syntax-p">(</span>&<span class="devsite-syntax-nx">received</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mi">1</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">msg</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Ack</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">})</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fmt</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Errorf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"sub.Receive: %w"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fmt</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fprintf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">w</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"Received %d messages\n"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">received</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span> <span class="devsite-syntax-p">}</span> </code></pre></devsite-code> </div> </section> <section><h3 id="java" data-text="Java" tabindex="-1">Java</h3> <p>Before trying this sample, follow the Java setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://cloud.google.com/java/docs/reference/google-cloud-pubsub/latest/overview" class="external">Pub/Sub Java API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/java-pubsub/samples/snippets/src/main/java/pubsub/SubscribeAsyncExample.java/HEAD/pubsub_subscriber_async_pull" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/java-pubsub/blob/HEAD/samples/snippets/src/main/java/pubsub/SubscribeAsyncExample.java" feedback-context="{"language": "java", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/java-pubsub/blob/HEAD/samples/snippets/src/main/java/pubsub/SubscribeAsyncExample.java"}" feedback-product="1634365" feedback-bucket="data-analytics" language="java" data-github-path="googleapis/java-pubsub/samples/snippets/src/main/java/pubsub/SubscribeAsyncExample.java" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_async_pull" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.cloud.pubsub.v1.AckReplyConsumer</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.cloud.pubsub.v1.MessageReceiver</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.cloud.pubsub.v1.Subscriber</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.pubsub.v1.ProjectSubscriptionName</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.pubsub.v1.PubsubMessage</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.util.concurrent.TimeUnit</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.util.concurrent.TimeoutException</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">class</span> <span class="devsite-syntax-nc">SubscribeAsyncExample</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">main</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-p">...</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">args</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">throws</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Exception</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// TODO(developer): Replace these variables before running the sample.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"your-project-id"</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"your-subscription-id"</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscribeAsyncExample</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">subscribeAsyncExample</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ProjectSubscriptionName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ProjectSubscriptionName</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">of</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Instantiate an asynchronous message receiver.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">MessageReceiver</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">receiver</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">PubsubMessage</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">AckReplyConsumer</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">consumer</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">-</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Handle incoming message, then ack the received message.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Id: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getMessageId</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Data: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getData</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">toStringUtf8</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">consumer</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">ack</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">};</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Subscriber</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">try</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">newBuilder</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">subscriptionName</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">receiver</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Start the subscriber.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">startAsync</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">awaitRunning</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">printf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Listening for messages on %s:\n"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionName</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">toString</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Allow the subscriber to run for 30s unless an unrecoverable error occurs.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">awaitTerminated</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-mi">30</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">TimeUnit</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">SECONDS</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">catch</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">TimeoutException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">timeoutException</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Shut down the subscriber after 30s. Stop receiving messages.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">stopAsync</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-p">}</span></code></pre></devsite-code> </div> </section> <section><h3 id="node.js" data-text="Node.js" tabindex="-1"><span class="two-line-tab notranslate"><span tab-label="JavaScript"></span>Node.js</span></h3><p> <p>Before trying this sample, follow the Node.js setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://googleapis.dev/nodejs/pubsub/latest" class="external">Pub/Sub Node.js API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/nodejs-pubsub/samples/listenForMessages.js/HEAD/pubsub_subscriber_async_pull" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/nodejs-pubsub/blob/HEAD/samples/listenForMessages.js" feedback-context="{"language": "nodejs", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/nodejs-pubsub/blob/HEAD/samples/listenForMessages.js"}" feedback-product="1634365" feedback-bucket="data-analytics" language="nodejs" data-github-path="googleapis/nodejs-pubsub/samples/listenForMessages.js" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_async_pull" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * TODO(developer): Uncomment these variables before running the sample.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-c1">// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID';</span> <span class="devsite-syntax-c1">// const timeout = 60;</span> <span class="devsite-syntax-c1">// Imports the Google Cloud client library</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-nx">PubSub</span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">require</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'@google-cloud/pubsub'</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-c1">// Creates a client; cache this for further use</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">pubSubClient</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">PubSub</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-kd">function</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">listenForMessages</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">subscriptionNameOrId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">timeout</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// References an existing subscription; if you are unsure if the</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// subscription will exist, try the optimisticSubscribe sample.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">pubSubClient</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">subscriptionNameOrId</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Create an event handler to handle messages</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">let</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">messageCount</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">0</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">messageHandler</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sb">`Received message </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">id</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb">:`</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sb">`\tData: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">data</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb">`</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sb">`\tAttributes: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">attributes</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb">`</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">messageCount</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">1</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// "Ack" (acknowledge receipt of) the message</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">ack</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">};</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Listen for new messages until timeout is hit</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">on</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'message'</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">messageHandler</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Wait a while for the subscription to run. (Part of the sample only.)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">setTimeout</span><span class="devsite-syntax-p">(()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">removeListener</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'message'</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">messageHandler</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sb">`</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nx">messageCount</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb"> message(s) received.`</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">timeout</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">1000</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span></code></pre></devsite-code> </div> </p></section> <section><h3 id="node.js_1" data-text="Node.js" tabindex="-1"><span class="two-line-tab notranslate"><span tab-label="TypeScript"></span>Node.js</span></h3><p> <p>Before trying this sample, follow the Node.js setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://googleapis.dev/nodejs/pubsub/latest" class="external">Pub/Sub Node.js API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/nodejs-pubsub/samples/typescript/listenForMessages.ts/HEAD/pubsub_subscriber_async_pull" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/nodejs-pubsub/blob/HEAD/samples/typescript/listenForMessages.ts" feedback-context="{"language": "nodejs", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/nodejs-pubsub/blob/HEAD/samples/typescript/listenForMessages.ts"}" feedback-product="1634365" feedback-bucket="data-analytics" language="nodejs" data-github-path="googleapis/nodejs-pubsub/samples/typescript/listenForMessages.ts" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_async_pull" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * TODO(developer): Uncomment these variables before running the sample.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-c1">// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID';</span> <span class="devsite-syntax-c1">// const timeout = 60;</span> <span class="devsite-syntax-c1">// Imports the Google Cloud client library</span> <span class="devsite-syntax-k">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-nx">PubSub</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">Message</span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kr">from</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s1">'@google-cloud/pubsub'</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-c1">// Creates a client; cache this for further use</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">pubSubClient</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">PubSub</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-kd">function</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">listenForMessages</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">subscriptionNameOrId</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">string</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">timeout</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">number</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// References an existing subscription; if you are unsure if the</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// subscription will exist, try the optimisticSubscribe sample.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">pubSubClient</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">subscriptionNameOrId</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Create an event handler to handle messages</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">let</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">messageCount</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">0</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">messageHandler</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">Message</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sb">`Received message </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">id</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb">:`</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sb">`\tData: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">data</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb">`</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sb">`\tAttributes: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">attributes</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb">`</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">messageCount</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">1</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// "Ack" (acknowledge receipt of) the message</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">ack</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">};</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Listen for new messages until timeout is hit</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">on</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'message'</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">messageHandler</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Wait a while for the subscription to run. (Part of the sample only.)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">setTimeout</span><span class="devsite-syntax-p">(()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">removeListener</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'message'</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">messageHandler</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sb">`</span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nx">messageCount</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb"> message(s) received.`</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">timeout</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">1000</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span></code></pre></devsite-code> </div> </p></section> <section><h3 class="selected" id="python" data-text="Python" tabindex="-1">Python</h3> <p>Before trying this sample, follow the Python setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="/python/docs/reference/pubsub/latest">Pub/Sub Python API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/python-pubsub/samples/snippets/subscriber.py/HEAD/pubsub_subscriber_async_pull" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/python-pubsub/blob/HEAD/samples/snippets/subscriber.py" feedback-context="{"language": "python", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/python-pubsub/blob/HEAD/samples/snippets/subscriber.py"}" feedback-product="1634365" feedback-bucket="data-analytics" language="python" data-github-path="googleapis/python-pubsub/samples/snippets/subscriber.py" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_async_pull" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-kn">from</span> <span class="devsite-syntax-nn">concurrent.futures</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-ne">TimeoutError</span> <span class="devsite-syntax-kn">from</span> <span class="devsite-syntax-nn">google.cloud</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-n">pubsub_v1</span> <span class="devsite-syntax-c1"># TODO(developer)</span> <span class="devsite-syntax-c1"># project_id = "your-project-id"</span> <span class="devsite-syntax-c1"># subscription_id = "your-subscription-id"</span> <span class="devsite-syntax-c1"># Number of seconds the subscriber should listen for messages</span> <span class="devsite-syntax-c1"># timeout = 5.0</span> <span class="devsite-syntax-n">subscriber</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">pubsub_v1</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">SubscriberClient</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-c1"># The `subscription_path` method creates a fully qualified identifier</span> <span class="devsite-syntax-c1"># in the form `projects/{project_id}/subscriptions/{subscription_id}`</span> <span class="devsite-syntax-n">subscription_path</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">subscription_path</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">project_id</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">subscription_id</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">callback</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">pubsub_v1</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">Message</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-o">-</span>> <span class="devsite-syntax-kc">None</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sa">f</span><span class="devsite-syntax-s2">"Received </span><span class="devsite-syntax-si">{</span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">."</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">ack</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-n">streaming_pull_future</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">subscribe</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">subscription_path</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">callback</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">callback</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sa">f</span><span class="devsite-syntax-s2">"Listening for messages on </span><span class="devsite-syntax-si">{</span><span class="devsite-syntax-n">subscription_path</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">..</span><span class="devsite-syntax-se">\n</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1"># Wrap subscriber in a 'with' block to automatically call close() when done.</span> <span class="devsite-syntax-k">with</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-k">try</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-c1"># When `timeout` is not set, result() will block indefinitely,</span> <span class="devsite-syntax-c1"># unless an exception is encountered first.</span> <span class="devsite-syntax-n">streaming_pull_future</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">timeout</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">timeout</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">except</span> <span class="devsite-syntax-ne">TimeoutError</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">streaming_pull_future</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">cancel</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-c1"># Trigger the shutdown.</span> <span class="devsite-syntax-n">streaming_pull_future</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-c1"># Block until the shutdown is complete.</span></code></pre></devsite-code> </div> </section> <section><h3 id="ruby" data-text="Ruby" tabindex="-1">Ruby</h3> <p>Before trying this sample, follow the Ruby setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://googleapis.dev/ruby/google-cloud-pubsub/latest/Google/Cloud/PubSub.html" class="external">Pub/Sub Ruby API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/google-cloud-ruby/google-cloud-pubsub/samples/pubsub_subscriber_async_pull.rb/HEAD/pubsub_subscriber_async_pull" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/google-cloud-ruby/blob/HEAD/google-cloud-pubsub/samples/pubsub_subscriber_async_pull.rb" feedback-context="{"language": "ruby", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/google-cloud-ruby/blob/HEAD/google-cloud-pubsub/samples/pubsub_subscriber_async_pull.rb"}" feedback-product="1634365" feedback-bucket="data-analytics" language="ruby" data-github-path="googleapis/google-cloud-ruby/google-cloud-pubsub/samples/pubsub_subscriber_async_pull.rb" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_async_pull" dir="ltr" is-upgraded syntax="Ruby"><code translate="no" dir="ltr"><span class="devsite-syntax-c1"># subscription_id = "your-subscription-id"</span> <span class="devsite-syntax-n">pubsub</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">Google</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">Cloud</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">Pubsub</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">new</span> <span class="devsite-syntax-n">subscription</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">pubsub</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">subscription</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscription_id</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscription</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">listen</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">do</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">|</span><span class="devsite-syntax-n">received_message</span><span class="devsite-syntax-o">|</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">puts</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Received message: </span><span class="devsite-syntax-si">#{</span><span class="devsite-syntax-n">received_message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">data</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">received_message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">acknowledge!</span> <span class="devsite-syntax-k">end</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">start</span> <span class="devsite-syntax-c1"># Let the main thread sleep for 60 seconds so the thread for listening</span> <span class="devsite-syntax-c1"># messages does not quit</span> <span class="devsite-syntax-nb">sleep</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mi">60</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">stop</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">wait!</span></code></pre></devsite-code> </div> </section> </div> <h3 id="retrieve_custom_attributes_using_the_high-level_client_library" data-text="Retrieve custom attributes using the high-level client library" tabindex="-1">Retrieve custom attributes using the high-level client library</h3> <p>The following samples show how to pull messages asynchronously and retrieve the custom attributes from the metadata.</p> <div class="ds-selector-tabs" data-ds-scope="code-sample"> <section><h3 id="c++_1" data-text="C++" tabindex="-1">C++</h3> <p>Before trying this sample, follow the C++ setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://googleapis.dev/cpp/google-cloud-pubsub/latest/" class="external">Pub/Sub C++ API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/google-cloud-cpp/google/cloud/pubsub/samples/samples.cc/HEAD/pubsub_subscriber_async_pull_custom_attributes" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/google-cloud-cpp/blob/HEAD/google/cloud/pubsub/samples/samples.cc" feedback-context="{"language": "cpp", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/google-cloud-cpp/blob/HEAD/google/cloud/pubsub/samples/samples.cc"}" feedback-product="1634365" feedback-bucket="data-analytics" language="cpp" data-github-path="googleapis/google-cloud-cpp/google/cloud/pubsub/samples/samples.cc" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_async_pull_custom_attributes" dir="ltr" is-upgraded syntax="C++"><code translate="no" dir="ltr"><span class="devsite-syntax-k">namespace</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">pubsub</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-nn">google</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-nn">cloud</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-nn">pubsub</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">auto</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">sample</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[](</span><span class="devsite-syntax-n">pubsub</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">Subscriber</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Subscribe</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[&](</span><span class="devsite-syntax-n">pubsub</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">Message</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">const</span>&<span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">m</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">pubsub</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">AckHandler</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">h</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">std</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">cout</span><span class="devsite-syntax-w"> << </span><span class="devsite-syntax-s">"Received message with attributes:</span><span class="devsite-syntax-se">\n</span><span class="devsite-syntax-s">"</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-k">auto</span>&<span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">kv</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">m</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">attributes</span><span class="devsite-syntax-p">())</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">std</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">cout</span><span class="devsite-syntax-w"> << </span><span class="devsite-syntax-s">" "</span><span class="devsite-syntax-w"> << </span><span class="devsite-syntax-n">kv</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">first</span><span class="devsite-syntax-w"> << </span><span class="devsite-syntax-s">": "</span><span class="devsite-syntax-w"> << </span><span class="devsite-syntax-n">kv</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">second</span><span class="devsite-syntax-w"> << </span><span class="devsite-syntax-s">"</span><span class="devsite-syntax-se">\n</span><span class="devsite-syntax-s">"</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">std</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">move</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">h</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-n">ack</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">PleaseIgnoreThisSimplifiesTestingTheSamples</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">});</span> <span class="devsite-syntax-p">};</span></code></pre></devsite-code> </div> </section> <section><h3 id="c_1" data-text="C#" tabindex="-1">C#</h3> <p>Before trying this sample, follow the C# setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.PubSub.V1/" class="external">Pub/Sub C# API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="GoogleCloudPlatform/dotnet-docs-samples/pubsub/api/Pubsub.Samples/PullMessagesWithCustomAttributesAsync.cs/HEAD/pubsub_subscriber_async_pull_custom_attributes" data-code-snippet="true" data-github-includecode-link="https://github.com/GoogleCloudPlatform/dotnet-docs-samples/blob/HEAD/pubsub/api/Pubsub.Samples/PullMessagesWithCustomAttributesAsync.cs" feedback-context="{"language": "csharp", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/GoogleCloudPlatform/dotnet-docs-samples/blob/HEAD/pubsub/api/Pubsub.Samples/PullMessagesWithCustomAttributesAsync.cs"}" feedback-product="1634365" feedback-bucket="data-analytics" language="csharp" data-github-path="GoogleCloudPlatform/dotnet-docs-samples/pubsub/api/Pubsub.Samples/PullMessagesWithCustomAttributesAsync.cs" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_async_pull_custom_attributes" dir="ltr" is-upgraded syntax="C#"><code translate="no" dir="ltr"> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">Google.Cloud.PubSub.V1</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">System</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">System.Collections.Generic</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">System.Threading</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">System.Threading.Tasks</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">class</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nc">PullMessagesWithCustomAttributesAsyncSample</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">async</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Task<List<PubsubMessage></span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">PullMessagesWithCustomAttributesAsync</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-kt">string</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">string</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">bool</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">acknowledge</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SubscriptionName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SubscriptionName</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">FromProjectSubscription</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SubscriberClient</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SubscriberClient</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">CreateAsync</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">subscriptionName</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">var</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">messages</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">List<PubsubMessage></span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Task</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">startTask</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">StartAsync</span><span class="devsite-syntax-p">((</span><span class="devsite-syntax-n">PubsubMessage</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">CancellationToken</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">cancel</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span>> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">messages</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Add</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">string</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Data</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">ToStringUtf8</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">WriteLine</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">$"Message {message.MessageId}: {text}"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Attributes</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">null</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">foreach</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-kt">var</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">attribute</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">in</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Attributes</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">WriteLine</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">$"{attribute.Key} = {attribute.Value}"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Task</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">FromResult</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">acknowledge</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">?</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SubscriberClient</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Reply</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Ack</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SubscriberClient</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Reply</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Nack</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">});</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Run for 7 seconds.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Task</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Delay</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-m">7000</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">StopAsync</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">CancellationToken</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">None</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Lets make sure that the start task finished successfully after the call to stop.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">startTask</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">messages</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-p">}</span></code></pre></devsite-code> </div> </section> <section><h3 id="go_1" data-text="Go" tabindex="-1">Go</h3> <p>Before trying this sample, follow the Go setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://godoc.org/cloud.google.com/go/pubsub" class="external">Pub/Sub Go API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="GoogleCloudPlatform/golang-samples/pubsub/subscriptions/async_pull_custom_attributes.go/HEAD/pubsub_subscriber_async_pull_custom_attributes" data-code-snippet="true" data-github-includecode-link="https://github.com/GoogleCloudPlatform/golang-samples/blob/HEAD/pubsub/subscriptions/async_pull_custom_attributes.go" feedback-context="{"language": "go", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/GoogleCloudPlatform/golang-samples/blob/HEAD/pubsub/subscriptions/async_pull_custom_attributes.go"}" feedback-product="1634365" feedback-bucket="data-analytics" language="go" data-github-path="GoogleCloudPlatform/golang-samples/pubsub/subscriptions/async_pull_custom_attributes.go" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_async_pull_custom_attributes" dir="ltr" is-upgraded syntax="Go"><code translate="no" dir="ltr"><span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"context"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"fmt"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"io"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"time"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"cloud.google.com/go/pubsub"</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">func</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">pullMsgsCustomAttributes</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">w</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">io</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Writer</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">projectID</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subID</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">string</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">error</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// projectID := "my-project-id"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// subID := "my-sub"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">context</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Background</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">pubsub</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">NewClient</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">projectID</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fmt</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Errorf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"pubsub.NewClient: %w"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">defer</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Close</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">sub</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Subscription</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">subID</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Receive messages for 10 seconds, which simplifies testing.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Comment this out in production, since `Receive` should</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// be used as a long running operation.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">cancel</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">context</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">WithTimeout</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mi">10</span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-nx">time</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Second</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">defer</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">cancel</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Receive blocks until the context is cancelled or an error occurs.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">sub</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Receive</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">func</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">_</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">context</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Context</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">msg</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-nx">pubsub</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Message</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fmt</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fprintf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">w</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"Got message :%q\n"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">string</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">msg</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Data</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fmt</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fprintln</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">w</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"Attributes:"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">key</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">value</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">range</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">msg</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Attributes</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fmt</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fprintf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">w</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"%s = %s\n"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">key</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">value</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">msg</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Ack</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">})</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fmt</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Errorf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"sub.Receive: %w"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span> <span class="devsite-syntax-p">}</span> </code></pre></devsite-code> </div> </section> <section><h3 id="java_1" data-text="Java" tabindex="-1">Java</h3> <p>Before trying this sample, follow the Java setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://cloud.google.com/java/docs/reference/google-cloud-pubsub/latest/overview" class="external">Pub/Sub Java API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/java-pubsub/samples/snippets/src/main/java/pubsub/SubscribeWithCustomAttributesExample.java/HEAD/pubsub_subscriber_async_pull_custom_attributes" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/java-pubsub/blob/HEAD/samples/snippets/src/main/java/pubsub/SubscribeWithCustomAttributesExample.java" feedback-context="{"language": "java", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/java-pubsub/blob/HEAD/samples/snippets/src/main/java/pubsub/SubscribeWithCustomAttributesExample.java"}" feedback-product="1634365" feedback-bucket="data-analytics" language="java" data-github-path="googleapis/java-pubsub/samples/snippets/src/main/java/pubsub/SubscribeWithCustomAttributesExample.java" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_async_pull_custom_attributes" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.cloud.pubsub.v1.AckReplyConsumer</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.cloud.pubsub.v1.MessageReceiver</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.cloud.pubsub.v1.Subscriber</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.pubsub.v1.ProjectSubscriptionName</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.pubsub.v1.PubsubMessage</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.util.concurrent.TimeUnit</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.util.concurrent.TimeoutException</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">class</span> <span class="devsite-syntax-nc">SubscribeWithCustomAttributesExample</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">main</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-p">...</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">args</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">throws</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Exception</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// TODO(developer): Replace these variables before running the sample.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"your-project-id"</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"your-subscription-id"</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscribeWithCustomAttributesExample</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">subscribeWithCustomAttributesExample</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ProjectSubscriptionName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ProjectSubscriptionName</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">of</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Instantiate an asynchronous message receiver.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">MessageReceiver</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">receiver</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">PubsubMessage</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">AckReplyConsumer</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">consumer</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">-</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Handle incoming message, then ack the received message.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Id: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getMessageId</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Data: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getData</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">toStringUtf8</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Print message attributes.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">message</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getAttributesMap</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">forEach</span><span class="devsite-syntax-p">((</span><span class="devsite-syntax-n">key</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">value</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">-</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">key</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">" = "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">value</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">consumer</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">ack</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">};</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Subscriber</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">try</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">newBuilder</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">subscriptionName</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">receiver</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Start the subscriber.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">startAsync</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">awaitRunning</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">printf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Listening for messages on %s:\n"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionName</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">toString</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Allow the subscriber to run for 30s unless an unrecoverable error occurs.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">awaitTerminated</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-mi">30</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">TimeUnit</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">SECONDS</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">catch</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">TimeoutException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">timeoutException</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Shut down the subscriber after 30s. Stop receiving messages.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">stopAsync</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-p">}</span></code></pre></devsite-code> </div> </section> <section><h3 id="node.js_2" data-text="Node.js" tabindex="-1">Node.js</h3><p> <p>Before trying this sample, follow the Node.js setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://googleapis.dev/nodejs/pubsub/latest" class="external">Pub/Sub Node.js API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/nodejs-pubsub/samples/listenWithCustomAttributes.js/HEAD/pubsub_subscriber_async_pull_custom_attributes" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/nodejs-pubsub/blob/HEAD/samples/listenWithCustomAttributes.js" feedback-context="{"language": "nodejs", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/nodejs-pubsub/blob/HEAD/samples/listenWithCustomAttributes.js"}" feedback-product="1634365" feedback-bucket="data-analytics" language="nodejs" data-github-path="googleapis/nodejs-pubsub/samples/listenWithCustomAttributes.js" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_async_pull_custom_attributes" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * TODO(developer): Uncomment these variables before running the sample.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-c1">// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID';</span> <span class="devsite-syntax-c1">// const timeout = 60;</span> <span class="devsite-syntax-c1">// Imports the Google Cloud client library</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-nx">PubSub</span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">require</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'@google-cloud/pubsub'</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-c1">// Creates a client; cache this for further use</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">pubSubClient</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">PubSub</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-k">async</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">function</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">listenWithCustomAttributes</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">subscriptionNameOrId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">timeout</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// References an existing subscription, e.g. "my-subscription"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">pubSubClient</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">subscriptionNameOrId</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Create an event handler to handle messages</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">messageHandler</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-sb">`Received message: id </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">id</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb">, data </span><span class="devsite-syntax-si">${</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">data</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb">, attributes: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nb">JSON</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">stringify</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">attributes</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb">`</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// "Ack" (acknowledge receipt of) the message</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">ack</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">};</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Wait a while for the subscription to run. (Part of the sample only.)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">on</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'message'</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">messageHandler</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">setTimeout</span><span class="devsite-syntax-p">(()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">removeListener</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'message'</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">messageHandler</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">timeout</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">1000</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span></code></pre></devsite-code> </div> </p></section> <section><h3 class="selected" id="python_1" data-text="Python" tabindex="-1">Python</h3> <p>Before trying this sample, follow the Python setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="/python/docs/reference/pubsub/latest">Pub/Sub Python API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/python-pubsub/samples/snippets/subscriber.py/HEAD/pubsub_subscriber_async_pull_custom_attributes" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/python-pubsub/blob/HEAD/samples/snippets/subscriber.py" feedback-context="{"language": "python", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/python-pubsub/blob/HEAD/samples/snippets/subscriber.py"}" feedback-product="1634365" feedback-bucket="data-analytics" language="python" data-github-path="googleapis/python-pubsub/samples/snippets/subscriber.py" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_async_pull_custom_attributes" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-kn">from</span> <span class="devsite-syntax-nn">concurrent.futures</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-ne">TimeoutError</span> <span class="devsite-syntax-kn">from</span> <span class="devsite-syntax-nn">google.cloud</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-n">pubsub_v1</span> <span class="devsite-syntax-c1"># TODO(developer)</span> <span class="devsite-syntax-c1"># project_id = "your-project-id"</span> <span class="devsite-syntax-c1"># subscription_id = "your-subscription-id"</span> <span class="devsite-syntax-c1"># Number of seconds the subscriber should listen for messages</span> <span class="devsite-syntax-c1"># timeout = 5.0</span> <span class="devsite-syntax-n">subscriber</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">pubsub_v1</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">SubscriberClient</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-n">subscription_path</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">subscription_path</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">project_id</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">subscription_id</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">callback</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">pubsub_v1</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">Message</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-o">-</span>> <span class="devsite-syntax-kc">None</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sa">f</span><span class="devsite-syntax-s2">"Received </span><span class="devsite-syntax-si">{</span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">data</span><span class="devsite-syntax-si">!r}</span><span class="devsite-syntax-s2">."</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-n">message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">attributes</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s2">"Attributes:"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">for</span> <span class="devsite-syntax-n">key</span> <span class="devsite-syntax-ow">in</span> <span class="devsite-syntax-n">message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">attributes</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">value</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">attributes</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">get</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">key</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sa">f</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">{</span><span class="devsite-syntax-n">key</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">: </span><span class="devsite-syntax-si">{</span><span class="devsite-syntax-n">value</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">ack</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-n">streaming_pull_future</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">subscribe</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">subscription_path</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">callback</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">callback</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sa">f</span><span class="devsite-syntax-s2">"Listening for messages on </span><span class="devsite-syntax-si">{</span><span class="devsite-syntax-n">subscription_path</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">..</span><span class="devsite-syntax-se">\n</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1"># Wrap subscriber in a 'with' block to automatically call close() when done.</span> <span class="devsite-syntax-k">with</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-k">try</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-c1"># When `timeout` is not set, result() will block indefinitely,</span> <span class="devsite-syntax-c1"># unless an exception is encountered first.</span> <span class="devsite-syntax-n">streaming_pull_future</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">timeout</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">timeout</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">except</span> <span class="devsite-syntax-ne">TimeoutError</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">streaming_pull_future</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">cancel</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-c1"># Trigger the shutdown.</span> <span class="devsite-syntax-n">streaming_pull_future</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-c1"># Block until the shutdown is complete.</span></code></pre></devsite-code> </div> </section> <section><h3 id="ruby_1" data-text="Ruby" tabindex="-1">Ruby</h3> <p>Before trying this sample, follow the Ruby setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://googleapis.dev/ruby/google-cloud-pubsub/latest/Google/Cloud/PubSub.html" class="external">Pub/Sub Ruby API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/google-cloud-ruby/google-cloud-pubsub/samples/pubsub_subscriber_async_pull_custom_attributes.rb/HEAD/pubsub_subscriber_async_pull_custom_attributes" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/google-cloud-ruby/blob/HEAD/google-cloud-pubsub/samples/pubsub_subscriber_async_pull_custom_attributes.rb" feedback-context="{"language": "ruby", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/google-cloud-ruby/blob/HEAD/google-cloud-pubsub/samples/pubsub_subscriber_async_pull_custom_attributes.rb"}" feedback-product="1634365" feedback-bucket="data-analytics" language="ruby" data-github-path="googleapis/google-cloud-ruby/google-cloud-pubsub/samples/pubsub_subscriber_async_pull_custom_attributes.rb" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_async_pull_custom_attributes" dir="ltr" is-upgraded syntax="Ruby"><code translate="no" dir="ltr"><span class="devsite-syntax-c1"># subscription_id = "your-subscription-id"</span> <span class="devsite-syntax-n">pubsub</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">Google</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">Cloud</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">Pubsub</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">new</span> <span class="devsite-syntax-n">subscription</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">pubsub</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">subscription</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscription_id</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscription</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">listen</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">do</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">|</span><span class="devsite-syntax-n">received_message</span><span class="devsite-syntax-o">|</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">puts</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Received message: </span><span class="devsite-syntax-si">#{</span><span class="devsite-syntax-n">received_message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">data</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">unless</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">received_message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">attributes</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">empty?</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">puts</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Attributes:"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">received_message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">attributes</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">each</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">do</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">|</span><span class="devsite-syntax-n">key</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">value</span><span class="devsite-syntax-o">|</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">puts</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-si">#{</span><span class="devsite-syntax-n">key</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">: </span><span class="devsite-syntax-si">#{</span><span class="devsite-syntax-n">value</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">end</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">end</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">received_message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">acknowledge!</span> <span class="devsite-syntax-k">end</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">start</span> <span class="devsite-syntax-c1"># Let the main thread sleep for 60 seconds so the thread for listening</span> <span class="devsite-syntax-c1"># messages does not quit</span> <span class="devsite-syntax-nb">sleep</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mi">60</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">stop</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">wait!</span></code></pre></devsite-code> </div> </section> </div> <h3 id="handle_errors_using_the_high-level_client_library" data-text="Handle errors using the high-level client library" tabindex="-1">Handle errors using the high-level client library</h3> <p>The following samples show how to handle errors that arise when subscribing to messages.</p> <div class="ds-selector-tabs" data-ds-scope="code-sample"> <section><h3 id="c++_2" data-text="C++" tabindex="-1">C++</h3> <p>Before trying this sample, follow the C++ setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://googleapis.dev/cpp/google-cloud-pubsub/latest/" class="external">Pub/Sub C++ API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/google-cloud-cpp/google/cloud/pubsub/samples/samples.cc/HEAD/pubsub_subscriber_error_listener" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/google-cloud-cpp/blob/HEAD/google/cloud/pubsub/samples/samples.cc" feedback-context="{"language": "cpp", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/google-cloud-cpp/blob/HEAD/google/cloud/pubsub/samples/samples.cc"}" feedback-product="1634365" feedback-bucket="data-analytics" language="cpp" data-github-path="googleapis/google-cloud-cpp/google/cloud/pubsub/samples/samples.cc" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_error_listener" dir="ltr" is-upgraded syntax="C++"><code translate="no" dir="ltr"><span class="devsite-syntax-k">namespace</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">pubsub</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-nn">google</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-nn">cloud</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-nn">pubsub</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">google</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">cloud</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">future</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">auto</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">sample</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[](</span><span class="devsite-syntax-n">pubsub</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">Subscriber</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Subscribe</span><span class="devsite-syntax-p">([&](</span><span class="devsite-syntax-n">pubsub</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">Message</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">const</span>&<span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">m</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">pubsub</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">AckHandler</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">h</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">std</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">cout</span><span class="devsite-syntax-w"> << </span><span class="devsite-syntax-s">"Received message "</span><span class="devsite-syntax-w"> << </span><span class="devsite-syntax-n">m</span><span class="devsite-syntax-w"> << </span><span class="devsite-syntax-s">"</span><span class="devsite-syntax-se">\n</span><span class="devsite-syntax-s">"</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">std</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">move</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">h</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-n">ack</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">PleaseIgnoreThisSimplifiesTestingTheSamples</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">})</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Setup an error handler for the subscription session</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">then</span><span class="devsite-syntax-p">([](</span><span class="devsite-syntax-n">future<google</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">cloud</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">Status</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">f</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">std</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">cout</span><span class="devsite-syntax-w"> << </span><span class="devsite-syntax-s">"Subscription session result: "</span><span class="devsite-syntax-w"> << </span><span class="devsite-syntax-n">f</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">get</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> << </span><span class="devsite-syntax-s">"</span><span class="devsite-syntax-se">\n</span><span class="devsite-syntax-s">"</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">});</span> <span class="devsite-syntax-p">};</span></code></pre></devsite-code> </div> </section> <section><h3 id="go_2" data-text="Go" tabindex="-1">Go</h3> <p>Before trying this sample, follow the Go setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://godoc.org/cloud.google.com/go/pubsub" class="external">Pub/Sub Go API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="GoogleCloudPlatform/golang-samples/pubsub/subscriptions/pull_error.go/HEAD/pubsub_subscriber_error_listener" data-code-snippet="true" data-github-includecode-link="https://github.com/GoogleCloudPlatform/golang-samples/blob/HEAD/pubsub/subscriptions/pull_error.go" feedback-context="{"language": "go", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/GoogleCloudPlatform/golang-samples/blob/HEAD/pubsub/subscriptions/pull_error.go"}" feedback-product="1634365" feedback-bucket="data-analytics" language="go" data-github-path="GoogleCloudPlatform/golang-samples/pubsub/subscriptions/pull_error.go" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_error_listener" dir="ltr" is-upgraded syntax="Go"><code translate="no" dir="ltr"><span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"context"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"fmt"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"io"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"cloud.google.com/go/pubsub"</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-kd">func</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">pullMsgsError</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">w</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">io</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Writer</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">projectID</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subID</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">string</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">error</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// projectID := "my-project-id"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// subID := "my-sub"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">context</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Background</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">pubsub</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">NewClient</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">projectID</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fmt</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Errorf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"pubsub.NewClient: %w"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">defer</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Close</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// If the service returns a non-retryable error, Receive returns that error after</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// all of the outstanding calls to the handler have returned.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">client</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Subscription</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">subID</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-nx">Receive</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">func</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ctx</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">context</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Context</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">msg</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-nx">pubsub</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Message</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fmt</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Fprintf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">w</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"Got message: %q\n"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">string</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">msg</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Data</span><span class="devsite-syntax-p">))</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">msg</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Ack</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">})</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">fmt</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">Errorf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Receive: %w"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">err</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">nil</span> <span class="devsite-syntax-p">}</span> </code></pre></devsite-code> </div> </section> <section><h3 id="java_2" data-text="Java" tabindex="-1">Java</h3> <p>Before trying this sample, follow the Java setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://cloud.google.com/java/docs/reference/google-cloud-pubsub/latest/overview" class="external">Pub/Sub Java API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/java-pubsub/samples/snippets/src/main/java/pubsub/SubscribeWithErrorListenerExample.java/HEAD/pubsub_subscriber_error_listener" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/java-pubsub/blob/HEAD/samples/snippets/src/main/java/pubsub/SubscribeWithErrorListenerExample.java" feedback-context="{"language": "java", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/java-pubsub/blob/HEAD/samples/snippets/src/main/java/pubsub/SubscribeWithErrorListenerExample.java"}" feedback-product="1634365" feedback-bucket="data-analytics" language="java" data-github-path="googleapis/java-pubsub/samples/snippets/src/main/java/pubsub/SubscribeWithErrorListenerExample.java" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_error_listener" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.gax.core.ExecutorProvider</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.api.gax.core.InstantiatingExecutorProvider</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.cloud.pubsub.v1.AckReplyConsumer</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.cloud.pubsub.v1.MessageReceiver</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.cloud.pubsub.v1.Subscriber</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.common.util.concurrent.MoreExecutors</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.pubsub.v1.ProjectSubscriptionName</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.pubsub.v1.PubsubMessage</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.util.concurrent.TimeUnit</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.util.concurrent.TimeoutException</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">class</span> <span class="devsite-syntax-nc">SubscribeWithErrorListenerExample</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">main</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-p">...</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">args</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">throws</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Exception</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// TODO(developer): Replace these variables before running the sample.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"your-project-id"</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"your-subscription-id"</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscribeWithErrorListenerExample</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">subscribeWithErrorListenerExample</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ProjectSubscriptionName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ProjectSubscriptionName</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">of</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Instantiate an asynchronous message receiver.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">MessageReceiver</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">receiver</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">PubsubMessage</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">AckReplyConsumer</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">consumer</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">-</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Handle incoming message, then ack the received message.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Id: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getMessageId</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Data: "</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">+</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getData</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">toStringUtf8</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">consumer</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">ack</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">};</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Subscriber</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kc">null</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">try</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Provides an executor service for processing messages.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ExecutorProvider</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">executorProvider</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">InstantiatingExecutorProvider</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">newBuilder</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">setExecutorThreadCount</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-mi">4</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">newBuilder</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">subscriptionName</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">receiver</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">setExecutorProvider</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">executorProvider</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Listen for unrecoverable failures. Rebuild a subscriber and restart subscribing</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// when the current subscriber encounters permanent errors.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addListener</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">Listener</span><span class="devsite-syntax-p">()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">failed</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">Subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">State</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">from</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Throwable</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">failure</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">failure</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getStackTrace</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-o">!</span><span class="devsite-syntax-n">executorProvider</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getExecutor</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">isShutdown</span><span class="devsite-syntax-p">())</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscribeWithErrorListenerExample</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">MoreExecutors</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">directExecutor</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Start the subscriber.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">startAsync</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">awaitRunning</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">printf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"Listening for messages on %s:\n"</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionName</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">toString</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Allow the subscriber to run for 30s unless an unrecoverable error occurs.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">awaitTerminated</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-mi">30</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">TimeUnit</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">SECONDS</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">catch</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">TimeoutException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">timeoutException</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Shut down the subscriber after 30s. Stop receiving messages.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">stopAsync</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-p">}</span></code></pre></devsite-code> </div> </section> <section><h3 id="node.js_3" data-text="Node.js" tabindex="-1">Node.js</h3> <p>Before trying this sample, follow the Node.js setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://googleapis.dev/nodejs/pubsub/latest" class="external">Pub/Sub Node.js API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/nodejs-pubsub/samples/listenForErrors.js/HEAD/pubsub_subscriber_error_listener" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/nodejs-pubsub/blob/HEAD/samples/listenForErrors.js" feedback-context="{"language": "nodejs", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/nodejs-pubsub/blob/HEAD/samples/listenForErrors.js"}" feedback-product="1634365" feedback-bucket="data-analytics" language="nodejs" data-github-path="googleapis/nodejs-pubsub/samples/listenForErrors.js" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_error_listener" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * TODO(developer): Uncomment these variables before running the sample.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-c1">// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID';</span> <span class="devsite-syntax-c1">// const timeout = 10;</span> <span class="devsite-syntax-c1">// Imports the Google Cloud client library</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-nx">PubSub</span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">require</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'@google-cloud/pubsub'</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-c1">// Creates a client; cache this for further use</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">pubSubClient</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">PubSub</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-kd">function</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">listenForErrors</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">subscriptionNameOrId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">timeout</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// References an existing subscription</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">pubSubClient</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">subscriptionNameOrId</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Create an event handler to handle messages</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">messageHandler</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Do something with the message</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sb">`Message: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb">`</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// "Ack" (acknowledge receipt of) the message</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">ack</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">};</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Create an event handler to handle errors</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">errorHandler</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">error</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Do something with the error</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">error</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sb">`ERROR: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nx">error</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb">`</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">throw</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">error</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">};</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Listen for new messages/errors until timeout is hit</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">on</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'message'</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">messageHandler</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">on</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'error'</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">errorHandler</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Wait a while for the subscription to run. (Part of the sample only.)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">setTimeout</span><span class="devsite-syntax-p">(()</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">removeListener</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'message'</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">messageHandler</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">removeListener</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'error'</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">errorHandler</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">},</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">timeout</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">1000</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span></code></pre></devsite-code> </div> </section> <section><h3 class="selected" id="python_2" data-text="Python" tabindex="-1">Python</h3> <p>Before trying this sample, follow the Python setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="/python/docs/reference/pubsub/latest">Pub/Sub Python API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/python-pubsub/samples/snippets/subscriber.py/HEAD/pubsub_subscriber_error_listener" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/python-pubsub/blob/HEAD/samples/snippets/subscriber.py" feedback-context="{"language": "python", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/python-pubsub/blob/HEAD/samples/snippets/subscriber.py"}" feedback-product="1634365" feedback-bucket="data-analytics" language="python" data-github-path="googleapis/python-pubsub/samples/snippets/subscriber.py" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_error_listener" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-kn">from</span> <span class="devsite-syntax-nn">google.cloud</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-n">pubsub_v1</span> <span class="devsite-syntax-c1"># TODO(developer)</span> <span class="devsite-syntax-c1"># project_id = "your-project-id"</span> <span class="devsite-syntax-c1"># subscription_id = "your-subscription-id"</span> <span class="devsite-syntax-c1"># Number of seconds the subscriber should listen for messages</span> <span class="devsite-syntax-c1"># timeout = 5.0</span> <span class="devsite-syntax-n">subscriber</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">pubsub_v1</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">SubscriberClient</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-n">subscription_path</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">subscription_path</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">project_id</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">subscription_id</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">def</span> <span class="devsite-syntax-nf">callback</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">pubsub_v1</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">Message</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-o">-</span>> <span class="devsite-syntax-kc">None</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sa">f</span><span class="devsite-syntax-s2">"Received </span><span class="devsite-syntax-si">{</span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">."</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">ack</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-n">streaming_pull_future</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">subscribe</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">subscription_path</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">callback</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">callback</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sa">f</span><span class="devsite-syntax-s2">"Listening for messages on </span><span class="devsite-syntax-si">{</span><span class="devsite-syntax-n">subscription_path</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">..</span><span class="devsite-syntax-se">\n</span><span class="devsite-syntax-s2">"</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1"># Wrap subscriber in a 'with' block to automatically call close() when done.</span> <span class="devsite-syntax-k">with</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-c1"># When `timeout` is not set, result() will block indefinitely,</span> <span class="devsite-syntax-c1"># unless an exception is encountered first.</span> <span class="devsite-syntax-k">try</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">streaming_pull_future</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">timeout</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">timeout</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">except</span> <span class="devsite-syntax-ne">Exception</span> <span class="devsite-syntax-k">as</span> <span class="devsite-syntax-n">e</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-sa">f</span><span class="devsite-syntax-s2">"Listening for messages on </span><span class="devsite-syntax-si">{</span><span class="devsite-syntax-n">subscription_path</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2"> threw an exception: </span><span class="devsite-syntax-si">{</span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">."</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">streaming_pull_future</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">cancel</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-c1"># Trigger the shutdown.</span> <span class="devsite-syntax-n">streaming_pull_future</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">result</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-c1"># Block until the shutdown is complete.</span></code></pre></devsite-code> </div> </section> <section><h3 id="ruby_2" data-text="Ruby" tabindex="-1">Ruby</h3> <p>Before trying this sample, follow the Go setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://godoc.org/cloud.google.com/go/pubsub" class="external">Pub/Sub Go API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/google-cloud-ruby/google-cloud-pubsub/samples/pubsub_subscriber_error_listener.rb/HEAD/pubsub_subscriber_error_listener" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/google-cloud-ruby/blob/HEAD/google-cloud-pubsub/samples/pubsub_subscriber_error_listener.rb" feedback-context="{"language": "ruby", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/google-cloud-ruby/blob/HEAD/google-cloud-pubsub/samples/pubsub_subscriber_error_listener.rb"}" feedback-product="1634365" feedback-bucket="data-analytics" language="ruby" data-github-path="googleapis/google-cloud-ruby/google-cloud-pubsub/samples/pubsub_subscriber_error_listener.rb" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_error_listener" dir="ltr" is-upgraded syntax="Ruby"><code translate="no" dir="ltr"><span class="devsite-syntax-c1"># subscription_id = "your-subscription-id"</span> <span class="devsite-syntax-n">pubsub</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">Google</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">Cloud</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">Pubsub</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">new</span> <span class="devsite-syntax-n">subscription</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">pubsub</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">subscription</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscription_id</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscription</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">listen</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">do</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">|</span><span class="devsite-syntax-n">received_message</span><span class="devsite-syntax-o">|</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">puts</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Received message: </span><span class="devsite-syntax-si">#{</span><span class="devsite-syntax-n">received_message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">data</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">received_message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">acknowledge!</span> <span class="devsite-syntax-k">end</span> <span class="devsite-syntax-c1"># Propagate expection from child threads to the main thread as soon as it is</span> <span class="devsite-syntax-c1"># raised. Exceptions happened in the callback thread are collected in the</span> <span class="devsite-syntax-c1"># callback thread pool and do not propagate to the main thread</span> <span class="devsite-syntax-no">Thread</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">abort_on_exception</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kp">true</span> <span class="devsite-syntax-k">begin</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">start</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># Let the main thread sleep for 60 seconds so the thread for listening</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1"># messages does not quit</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">sleep</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mi">60</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">stop</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">wait!</span> <span class="devsite-syntax-k">rescue</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">StandardError</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">e</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">puts</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Exception </span><span class="devsite-syntax-si">#{</span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">inspect</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">: </span><span class="devsite-syntax-si">#{</span><span class="devsite-syntax-n">e</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">raise</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Stopped listening for messages."</span> <span class="devsite-syntax-k">end</span></code></pre></devsite-code> </div> </section> </div> <h2 id="unary_pull_code_samples" data-text="Unary pull code samples" tabindex="-1">Unary pull code samples</h2> <p>Here's some sample code to <a href="/pubsub/docs/reference/rpc/google.pubsub.v1#google.pubsub.v1.PullRequest">pull</a> and <a href="/pubsub/docs/reference/rpc/google.pubsub.v1#acknowledgerequest">acknowledge</a> a fixed number of messages.</p> <div class="ds-selector-tabs" data-ds-scope="code-sample"> <section><h3 id="c++_3" data-text="C++" tabindex="-1">C++</h3><p> <p>Before trying this sample, follow the C++ setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://googleapis.dev/cpp/google-cloud-pubsub/latest/" class="external">Pub/Sub C++ API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/google-cloud-cpp/google/cloud/pubsub/samples/samples.cc/HEAD/pubsub_subscriber_sync_pull" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/google-cloud-cpp/blob/HEAD/google/cloud/pubsub/samples/samples.cc" feedback-context="{"language": "cpp", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/google-cloud-cpp/blob/HEAD/google/cloud/pubsub/samples/samples.cc"}" feedback-product="1634365" feedback-bucket="data-analytics" language="cpp" data-github-path="googleapis/google-cloud-cpp/google/cloud/pubsub/samples/samples.cc" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_sync_pull" dir="ltr" is-upgraded syntax="C++"><code translate="no" dir="ltr"><span class="devsite-syntax-p">[](</span><span class="devsite-syntax-n">google</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">cloud</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">pubsub</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">Subscriber</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">auto</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Pull</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-o">!</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">throw</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">std</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">move</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-n">status</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">std</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">cout</span><span class="devsite-syntax-w"> << </span><span class="devsite-syntax-s">"Received message "</span><span class="devsite-syntax-w"> << </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">-</span>><span class="devsite-syntax-n">message</span><span class="devsite-syntax-w"> << </span><span class="devsite-syntax-s">"</span><span class="devsite-syntax-se">\n</span><span class="devsite-syntax-s">"</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">std</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-n">move</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">-</span>><span class="devsite-syntax-n">handler</span><span class="devsite-syntax-p">).</span><span class="devsite-syntax-n">ack</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-p">}</span></code></pre></devsite-code> </div> </p></section> <section><h3 id="c_2" data-text="C#" tabindex="-1">C#</h3><p> <p>Before trying this sample, follow the C# setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.PubSub.V1/" class="external">Pub/Sub C# API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="GoogleCloudPlatform/dotnet-docs-samples/pubsub/api/Pubsub.Samples/PullMessagesSync.cs/HEAD/pubsub_subscriber_sync_pull" data-code-snippet="true" data-github-includecode-link="https://github.com/GoogleCloudPlatform/dotnet-docs-samples/blob/HEAD/pubsub/api/Pubsub.Samples/PullMessagesSync.cs" feedback-context="{"language": "csharp", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/GoogleCloudPlatform/dotnet-docs-samples/blob/HEAD/pubsub/api/Pubsub.Samples/PullMessagesSync.cs"}" feedback-product="1634365" feedback-bucket="data-analytics" language="csharp" data-github-path="GoogleCloudPlatform/dotnet-docs-samples/pubsub/api/Pubsub.Samples/PullMessagesSync.cs" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_sync_pull" dir="ltr" is-upgraded syntax="C#"><code translate="no" dir="ltr"> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">Google.Cloud.PubSub.V1</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">Grpc.Core</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">System</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">System.Linq</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">using</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">System.Threading</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-k">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">class</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nc">PullMessagesSyncSample</span> <span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">int</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">PullMessagesSync</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-kt">string</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">string</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">bool</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">acknowledge</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SubscriptionName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SubscriptionName</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">FromProjectSubscription</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SubscriberServiceApiClient</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriberClient</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SubscriberServiceApiClient</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Create</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">int</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">messageCount</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">0</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">try</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Pull messages from server,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// allowing an immediate response if there are no messages.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">PullResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriberClient</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Pull</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">subscriptionName</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">maxMessages</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-m">20</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Print out each received message.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">foreach</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">ReceivedMessage</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">msg</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">in</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">ReceivedMessages</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">string</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">text</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">msg</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Data</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">ToStringUtf8</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">WriteLine</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">$"Message {msg.Message.MessageId}: {text}"</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Interlocked</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Increment</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-k">ref</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">messageCount</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// If acknowledgement required, send to server.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">acknowledge</span><span class="devsite-syntax-w"> && </span><span class="devsite-syntax-n">messageCount</span><span class="devsite-syntax-w"> > </span><span class="devsite-syntax-m">0</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriberClient</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Acknowledge</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">subscriptionName</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">ReceivedMessages</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Select</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">msg</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span>><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">msg</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">AckId</span><span class="devsite-syntax-p">));</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">catch</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">RpcException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ex</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">when</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">ex</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Status</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">StatusCode</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">==</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">StatusCode</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-n">Unavailable</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// UNAVAILABLE due to too many concurrent pull requests pending for the given subscription.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">messageCount</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-p">}</span></code></pre></devsite-code> </div> </p></section> <section><h3 id="java_3" data-text="Java" tabindex="-1">Java</h3><p> <p>Before trying this sample, follow the Java setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://cloud.google.com/java/docs/reference/google-cloud-pubsub/latest/overview" class="external">Pub/Sub Java API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/java-pubsub/samples/snippets/src/main/java/pubsub/SubscribeSyncExample.java/HEAD/pubsub_subscriber_sync_pull" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/java-pubsub/blob/HEAD/samples/snippets/src/main/java/pubsub/SubscribeSyncExample.java" feedback-context="{"language": "java", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/java-pubsub/blob/HEAD/samples/snippets/src/main/java/pubsub/SubscribeSyncExample.java"}" feedback-product="1634365" feedback-bucket="data-analytics" language="java" data-github-path="googleapis/java-pubsub/samples/snippets/src/main/java/pubsub/SubscribeSyncExample.java" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_sync_pull" dir="ltr" is-upgraded syntax="Java"><code translate="no" dir="ltr"> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.cloud.pubsub.v1.stub.GrpcSubscriberStub</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.cloud.pubsub.v1.stub.SubscriberStub</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.cloud.pubsub.v1.stub.SubscriberStubSettings</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.pubsub.v1.AcknowledgeRequest</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.pubsub.v1.ProjectSubscriptionName</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.pubsub.v1.PullRequest</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.pubsub.v1.PullResponse</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">com.google.pubsub.v1.ReceivedMessage</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.io.IOException</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.util.ArrayList</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kn">import</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nn">java.util.List</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">class</span> <span class="devsite-syntax-nc">SubscribeSyncExample</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">main</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-p">...</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">args</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">throws</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Exception</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// TODO(developer): Replace these variables before running the sample.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"your-project-id"</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s">"your-subscription-id"</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Integer</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">numOfMessages</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mi">10</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscribeSyncExample</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">numOfMessages</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">public</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">static</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kt">void</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nf">subscribeSyncExample</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">Integer</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">numOfMessages</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">throws</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">IOException</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SubscriberStubSettings</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriberStubSettings</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SubscriberStubSettings</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">newBuilder</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">setTransportChannelProvider</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">SubscriberStubSettings</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">defaultGrpcTransportProviderBuilder</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">setMaxInboundMessageSize</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-mi">20</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mi">1024</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">*</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mi">1024</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// 20MB (maximum message size).</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">())</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">try</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">SubscriberStub</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">GrpcSubscriberStub</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">create</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">subscriberStubSettings</span><span class="devsite-syntax-p">))</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">String</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionName</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ProjectSubscriptionName</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">format</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriptionId</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">PullRequest</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">pullRequest</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">PullRequest</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">newBuilder</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">setMaxMessages</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">numOfMessages</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">setSubscription</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">subscriptionName</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Use pullCallable().futureCall to asynchronously perform this operation.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">PullResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">pullResponse</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">pullCallable</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">call</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">pullRequest</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Stop the program if the pull response is empty to avoid acknowledging</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// an empty list of ack IDs.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">pullResponse</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getReceivedMessagesList</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">isEmpty</span><span class="devsite-syntax-p">())</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s">"No message was pulled. Exiting."</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">return</span><span class="devsite-syntax-p">;</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">List<String></span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ackIds</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ArrayList</span><><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">ReceivedMessage</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">pullResponse</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getReceivedMessagesList</span><span class="devsite-syntax-p">())</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Handle received message</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// ...</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">ackIds</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">add</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getAckId</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Acknowledge received messages.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">AcknowledgeRequest</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">acknowledgeRequest</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">AcknowledgeRequest</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">newBuilder</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">setSubscription</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">subscriptionName</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">addAllAckIds</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">ackIds</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">build</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Use acknowledgeCallable().futureCall to asynchronously perform this operation.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">acknowledgeCallable</span><span class="devsite-syntax-p">().</span><span class="devsite-syntax-na">call</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">acknowledgeRequest</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">System</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">out</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">println</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">pullResponse</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-na">getReceivedMessagesList</span><span class="devsite-syntax-p">());</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-p">}</span></code></pre></devsite-code> </div> </p></section> <section><h3 id="node.js_4" data-text="Node.js" tabindex="-1">Node.js</h3><p> <p>Before trying this sample, follow the Node.js setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://googleapis.dev/nodejs/pubsub/latest" class="external">Pub/Sub Node.js API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/nodejs-pubsub/samples/synchronousPull.js/HEAD/pubsub_subscriber_sync_pull" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/nodejs-pubsub/blob/HEAD/samples/synchronousPull.js" feedback-context="{"language": "nodejs", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/nodejs-pubsub/blob/HEAD/samples/synchronousPull.js"}" feedback-product="1634365" feedback-bucket="data-analytics" language="nodejs" data-github-path="googleapis/nodejs-pubsub/samples/synchronousPull.js" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_sync_pull" dir="ltr" is-upgraded syntax="JavaScript"><code translate="no" dir="ltr"><span class="devsite-syntax-cm">/**</span> <span class="devsite-syntax-cm"> * TODO(developer): Uncomment these variables before running the sample.</span> <span class="devsite-syntax-cm"> */</span> <span class="devsite-syntax-c1">// const projectId = 'YOUR_PROJECT_ID';</span> <span class="devsite-syntax-c1">// const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID';</span> <span class="devsite-syntax-c1">// Imports the Google Cloud client library. v1 is for the lower level</span> <span class="devsite-syntax-c1">// proto access.</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-nx">v1</span><span class="devsite-syntax-p">}</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">require</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'@google-cloud/pubsub'</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-c1">// Creates a client; cache this for further use.</span> <span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subClient</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-ow">new</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">v1</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">SubscriberClient</span><span class="devsite-syntax-p">();</span> <span class="devsite-syntax-k">async</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">function</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">synchronousPull</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscriptionNameOrId</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// The low level API client requires a name only.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">formattedSubscription</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscriptionNameOrId</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">indexOf</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'/'</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span>><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">0</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">?</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscriptionNameOrId</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subClient</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">subscriptionPath</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">projectId</span><span class="devsite-syntax-p">,</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscriptionNameOrId</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// The maximum number of messages returned for this request.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Pub/Sub may return fewer than the number specified.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">request</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">formattedSubscription</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">maxMessages</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">10</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">};</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// The subscriber pulls a specified number of messages.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span><span class="devsite-syntax-nx">response</span><span class="devsite-syntax-p">]</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subClient</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">pull</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">request</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Process the messages.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">ackIds</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[];</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">for</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">of</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">response</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">receivedMessages</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">||</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[])</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sb">`Received message: </span><span class="devsite-syntax-si">${</span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">data</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-sb">`</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">ackId</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">ackIds</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">push</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">message</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">ackId</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">if</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ackIds</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">length</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">!==</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-mf">0</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// Acknowledge all of the messages. You could also acknowledge</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-c1">// these individually, but this is more efficient.</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-kd">const</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">ackRequest</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subscription</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">formattedSubscription</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">ackIds</span><span class="devsite-syntax-o">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">ackIds</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">};</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">await</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">subClient</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">acknowledge</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-nx">ackRequest</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nx">console</span><span class="devsite-syntax-p">.</span><span class="devsite-syntax-nx">log</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-s1">'Done.'</span><span class="devsite-syntax-p">);</span> <span class="devsite-syntax-p">}</span></code></pre></devsite-code> </div> </p></section> <section><h3 id="php" data-text="PHP" tabindex="-1">PHP</h3><p> <p>Before trying this sample, follow the Node.js setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://googleapis.dev/nodejs/pubsub/latest" class="external">Pub/Sub Node.js API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="GoogleCloudPlatform/php-docs-samples/pubsub/api/src/pull_messages.php/HEAD/pubsub_subscriber_sync_pull" data-code-snippet="true" data-github-includecode-link="https://github.com/GoogleCloudPlatform/php-docs-samples/blob/HEAD/pubsub/api/src/pull_messages.php" feedback-context="{"language": "php", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/GoogleCloudPlatform/php-docs-samples/blob/HEAD/pubsub/api/src/pull_messages.php"}" feedback-product="1634365" feedback-bucket="data-analytics" language="php" data-github-path="GoogleCloudPlatform/php-docs-samples/pubsub/api/src/pull_messages.php" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_sync_pull" dir="ltr" is-upgraded syntax="PHP"><code translate="no" dir="ltr"><span class="devsite-syntax-x">use Google\Cloud\PubSub\PubSubClient;</span> <span class="devsite-syntax-x">/**</span> <span class="devsite-syntax-x"> * Pulls all Pub/Sub messages for a subscription.</span> <span class="devsite-syntax-x"> *</span> <span class="devsite-syntax-x"> * @param string $projectId The Google project ID.</span> <span class="devsite-syntax-x"> * @param string $subscriptionName The Pub/Sub subscription name.</span> <span class="devsite-syntax-x"> */</span> <span class="devsite-syntax-x">function pull_messages($projectId, $subscriptionName)</span> <span class="devsite-syntax-x">{</span> <span class="devsite-syntax-x"> $pubsub = new PubSubClient([</span> <span class="devsite-syntax-x"> 'projectId' => $projectId,</span> <span class="devsite-syntax-x"> ]);</span> <span class="devsite-syntax-x"> $subscription = $pubsub->subscription($subscriptionName);</span> <span class="devsite-syntax-x"> foreach ($subscription->pull() as $message) {</span> <span class="devsite-syntax-x"> printf('Message: %s' . PHP_EOL, $message->data());</span> <span class="devsite-syntax-x"> // Acknowledge the Pub/Sub message has been received, so it will not be pulled multiple times.</span> <span class="devsite-syntax-x"> $subscription->acknowledge($message);</span> <span class="devsite-syntax-x"> }</span> <span class="devsite-syntax-x">}</span></code></pre></devsite-code> </div> </p></section> <section><h3 id="ruby_3" data-text="Ruby" tabindex="-1">Ruby</h3><p> <p>Before trying this sample, follow the Ruby setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="https://googleapis.dev/ruby/google-cloud-pubsub/latest/Google/Cloud/PubSub.html" class="external">Pub/Sub Ruby API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/google-cloud-ruby/google-cloud-pubsub/samples/pubsub_subscriber_sync_pull.rb/HEAD/pubsub_subscriber_sync_pull" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/google-cloud-ruby/blob/HEAD/google-cloud-pubsub/samples/pubsub_subscriber_sync_pull.rb" feedback-context="{"language": "ruby", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/google-cloud-ruby/blob/HEAD/google-cloud-pubsub/samples/pubsub_subscriber_sync_pull.rb"}" feedback-product="1634365" feedback-bucket="data-analytics" language="ruby" data-github-path="googleapis/google-cloud-ruby/google-cloud-pubsub/samples/pubsub_subscriber_sync_pull.rb" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_sync_pull" dir="ltr" is-upgraded syntax="Ruby"><code translate="no" dir="ltr"><span class="devsite-syntax-c1"># subscription_id = "your-subscription-id"</span> <span class="devsite-syntax-n">pubsub</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-no">Google</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">Cloud</span><span class="devsite-syntax-o">::</span><span class="devsite-syntax-no">Pubsub</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">new</span> <span class="devsite-syntax-n">subscription</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">pubsub</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">subscription</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">subscription_id</span> <span class="devsite-syntax-n">subscription</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">pull</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-ss">immediate</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-kp">false</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">each</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-k">do</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-o">|</span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-o">|</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nb">puts</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"Message pulled: </span><span class="devsite-syntax-si">#{</span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">data</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">acknowledge!</span> <span class="devsite-syntax-k">end</span></code></pre></devsite-code> </div> </p></section> <section><h3 id="protocol" data-text="Protocol" tabindex="-1">Protocol</h3><p>Request:</p> <p><code translate="no" dir="ltr">POST https://pubsub.googleapis.com/v1/projects/myproject/subscriptions/mysubscription:pull</code></p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="JSON"><code translate="no" dir="ltr"><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nt">"returnImmediately"</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"false"</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nt">"maxMessages"</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"1"</span> <span class="devsite-syntax-p">}</span> </code></pre></devsite-code> <p>Response:</p> <p><code translate="no" dir="ltr">200 OK</code></p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="JSON"><code translate="no" dir="ltr"><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nt">"receivedMessages"</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nt">"ackId"</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"dQNNHlAbEGEIBERNK0EPKVgUWQYyODM2LwgRHFEZDDsLRk1SK..."</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nt">"message"</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nt">"data"</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"SGVsbG8gQ2xvdWQgUHViL1N1YiEgSGVyZSBpcyBteSBtZXNzYWdlIQ=="</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nt">"messageId"</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"19917247034"</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">}]</span> <span class="devsite-syntax-p">}</span> </code></pre></devsite-code> <p>Request:</p> <p><code translate="no" dir="ltr">POST https://pubsub.googleapis.com/v1/projects/myproject/subscriptions/mysubscription:acknowledge</code></p> <div></div><devsite-code><pre class="devsite-click-to-copy" translate="no" dir="ltr" is-upgraded syntax="JSON"><code translate="no" dir="ltr"><span class="devsite-syntax-p">{</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-nt">"ackIds"</span><span class="devsite-syntax-p">:</span><span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">[</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-s2">"dQNNHlAbEGEIBERNK0EPKVgUWQYyODM2LwgRHFEZDDsLRk1SK..."</span> <span class="devsite-syntax-w"> </span><span class="devsite-syntax-p">]</span> <span class="devsite-syntax-p">}</span> </code></pre></devsite-code></section> <section><h3 class="selected" id="python_3" data-text="Python" tabindex="-1">Python</h3><p> <p>Before trying this sample, follow the Python setup instructions in <a href="/pubsub/docs/create-topic-client-libraries">Quickstart: Using Client Libraries</a>. For more information, see the <a href="/python/docs/reference/pubsub/latest">Pub/Sub Python API reference documentation</a>. </p> <div class="github-docwidget-gitinclude-code"> <div></div><devsite-code><pre suppresswarning="suppresswarning" translate="no" class="devsite-click-to-copy" track-metadata-position="googleapis/python-pubsub/samples/snippets/subscriber.py/HEAD/pubsub_subscriber_sync_pull" data-code-snippet="true" data-github-includecode-link="https://github.com/googleapis/python-pubsub/blob/HEAD/samples/snippets/subscriber.py" feedback-context="{"language": "python", "region_tag": "pubsub_detach_subscription", "snippet_file_url": "https://github.com/googleapis/python-pubsub/blob/HEAD/samples/snippets/subscriber.py"}" feedback-product="1634365" feedback-bucket="data-analytics" language="python" data-github-path="googleapis/python-pubsub/samples/snippets/subscriber.py" data-git-revision="HEAD" data-region-tag="pubsub_subscriber_sync_pull" dir="ltr" is-upgraded syntax="Python"><code translate="no" dir="ltr"><span class="devsite-syntax-kn">from</span> <span class="devsite-syntax-nn">google.api_core</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-n">retry</span> <span class="devsite-syntax-kn">from</span> <span class="devsite-syntax-nn">google.cloud</span> <span class="devsite-syntax-kn">import</span> <span class="devsite-syntax-n">pubsub_v1</span> <span class="devsite-syntax-c1"># TODO(developer)</span> <span class="devsite-syntax-c1"># project_id = "your-project-id"</span> <span class="devsite-syntax-c1"># subscription_id = "your-subscription-id"</span> <span class="devsite-syntax-n">subscriber</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">pubsub_v1</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">SubscriberClient</span><span class="devsite-syntax-p">()</span> <span class="devsite-syntax-n">subscription_path</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">subscription_path</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">project_id</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-n">subscription_id</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">NUM_MESSAGES</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-mi">3</span> <span class="devsite-syntax-c1"># Wrap the subscriber in a 'with' block to automatically call close() to</span> <span class="devsite-syntax-c1"># close the underlying gRPC channel when done.</span> <span class="devsite-syntax-k">with</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-c1"># The subscriber pulls a specific number of messages. The actual</span> <span class="devsite-syntax-c1"># number of messages pulled may be smaller than max_messages.</span> <span class="devsite-syntax-n">response</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">pull</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">request</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-s2">"subscription"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">subscription_path</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-s2">"max_messages"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">NUM_MESSAGES</span><span class="devsite-syntax-p">},</span> <span class="devsite-syntax-n">retry</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-n">retry</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">Retry</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">deadline</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-mi">300</span><span class="devsite-syntax-p">),</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-k">if</span> <span class="devsite-syntax-nb">len</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">received_messages</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-o">==</span> <span class="devsite-syntax-mi">0</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-k">return</span> <span class="devsite-syntax-n">ack_ids</span> <span class="devsite-syntax-o">=</span> <span class="devsite-syntax-p">[]</span> <span class="devsite-syntax-k">for</span> <span class="devsite-syntax-n">received_message</span> <span class="devsite-syntax-ow">in</span> <span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">received_messages</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-sa">f</span><span class="devsite-syntax-s2">"Received: </span><span class="devsite-syntax-si">{</span><span class="devsite-syntax-n">received_message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">data</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">."</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-n">ack_ids</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">append</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">received_message</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">ack_id</span><span class="devsite-syntax-p">)</span> <span class="devsite-syntax-c1"># Acknowledges the received messages so they will not be sent again.</span> <span class="devsite-syntax-n">subscriber</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">acknowledge</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-n">request</span><span class="devsite-syntax-o">=</span><span class="devsite-syntax-p">{</span><span class="devsite-syntax-s2">"subscription"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">subscription_path</span><span class="devsite-syntax-p">,</span> <span class="devsite-syntax-s2">"ack_ids"</span><span class="devsite-syntax-p">:</span> <span class="devsite-syntax-n">ack_ids</span><span class="devsite-syntax-p">}</span> <span class="devsite-syntax-p">)</span> <span class="devsite-syntax-nb">print</span><span class="devsite-syntax-p">(</span> <span class="devsite-syntax-sa">f</span><span class="devsite-syntax-s2">"Received and acknowledged </span><span class="devsite-syntax-si">{</span><span class="devsite-syntax-nb">len</span><span class="devsite-syntax-p">(</span><span class="devsite-syntax-n">response</span><span class="devsite-syntax-o">.</span><span class="devsite-syntax-n">received_messages</span><span class="devsite-syntax-p">)</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2"> messages from </span><span class="devsite-syntax-si">{</span><span class="devsite-syntax-n">subscription_path</span><span class="devsite-syntax-si">}</span><span class="devsite-syntax-s2">."</span> <span class="devsite-syntax-p">)</span></code></pre></devsite-code> </div> </p></section> </div> <p>Pub/Sub delivers a list of messages. If the list has multiple messages, Pub/Sub orders the messages with the same <a href="/pubsub/docs/ordering">ordering key</a>. The following are some important caveats:</p> <ul> <li><p>Setting a value for <code translate="no" dir="ltr">max_messages</code> in the request does not guarantee that <code translate="no" dir="ltr">max_messages</code> are returned, even if there are that many messages in the backlog. The Pub/Sub Pull API might return fewer than <code translate="no" dir="ltr">max_messages</code> in order to reduce the delivery latency for messages that are readily available to be delivered.</p></li> <li><p>A pull response that comes with 0 messages must not be used as an indicator that there are no messages in the backlog. It's possible to get a response with 0 messages and have a subsequent request that returns messages.</p></li> <li><p>To achieve low message delivery latency with the unary pull mode, it's essential to have many simultaneously outstanding pull requests. As the throughput of the topic increases, more pull requests are necessary. In general, the StreamingPull mode is preferable for latency-sensitive applications.</p></li> </ul> <h2 id="quotas_and_limits" data-text="Quotas and limits" tabindex="-1">Quotas and limits</h2> <p>Both Pull and StreamingPull connections are subject to quotas and limits. For more information, see <a href="/pubsub/quotas">Pub/Sub quotas and limits</a>.</p> <h2 id="whats_next" data-text="What's next" tabindex="-1">What's next</h2> <ul> <li><p>Create a pull <a href="/pubsub/docs/create-subscription">subscription</a> for your topic.</p></li> <li><p>Create or modify a subscription with <a href="/sdk/gcloud/reference/pubsub/subscriptions">gcloud CLI</a>.</p></li> <li><p>Create or modify a subscription with <a href="/pubsub/docs/reference/rest/v1/projects.subscriptions">REST APIs</a>.</p></li> <li><p>Create or modify a subscription with <a href="/pubsub/docs/reference/rpc/google.pubsub.v1">RPC APIs</a>.</p></li> </ul> <devsite-hats-survey class="nocontent" hats-id="Nd7nTix2o0eU5NUYprb0ThtUc5jf" listnr-id="83405"></devsite-hats-survey> </div> <devsite-thumb-rating position="footer"> </devsite-thumb-rating> <devsite-feedback position="footer" project-name="Pub/Sub Documentation" product-id="96707" bucket="documentation" context="" version="t-devsite-webserver-20241114-r00-rc02.464922260396498922" data-label="Send Feedback Button" track-type="feedback" track-name="sendFeedbackLink" track-metadata-position="footer" class="nocontent" project-feedback-url="https://issuetracker.google.com/issues/new?component=187173&template=0" project-icon="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/cloud/images/favicons/onecloud/super_cloud.png" > <button> Send feedback </button> </devsite-feedback> <div class="devsite-floating-action-buttons"> </div> </article> <devsite-content-footer class="nocontent"> <p>Except as otherwise noted, the content of this page is licensed under the <a href="https://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 License</a>, and code samples are licensed under the <a href="https://www.apache.org/licenses/LICENSE-2.0">Apache 2.0 License</a>. For details, see the <a href="https://developers.google.com/site-policies">Google Developers Site Policies</a>. Java is a registered trademark of Oracle and/or its affiliates.</p> <p>Last updated 2024-11-22 UTC.</p> </devsite-content-footer> <devsite-notification > </devsite-notification> <div class="devsite-content-data"> <template class="devsite-thumb-rating-feedback"> <devsite-feedback position="thumb-rating" project-name="Pub/Sub Documentation" product-id="96707" bucket="documentation" context="" version="t-devsite-webserver-20241114-r00-rc02.464922260396498922" data-label="Send Feedback Button" track-type="feedback" track-name="sendFeedbackLink" track-metadata-position="thumb-rating" class="nocontent" project-feedback-url="https://issuetracker.google.com/issues/new?component=187173&template=0" project-icon="https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/cloud/images/favicons/onecloud/super_cloud.png" > <button> Need to tell us more? </button> </devsite-feedback> </template> <template class="devsite-content-data-template"> [[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-11-22 UTC."],[],[]] </template> </div> </devsite-content> </main> <devsite-footer-promos class="devsite-footer"> </devsite-footer-promos> <devsite-footer-linkboxes class="devsite-footer"> <nav class="devsite-footer-linkboxes nocontent" aria-label="Footer links"> <ul class="devsite-footer-linkboxes-list"> <li class="devsite-footer-linkbox "> <h3 class="devsite-footer-linkbox-heading no-link">Why Google</h3> <ul class="devsite-footer-linkbox-list"> <li class="devsite-footer-linkbox-item"> <a href="/why-google-cloud/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 1)" track-metadata-child_headline="why google"track-type="footer link"track-name="choosing google cloud"track-metadata-position="footer"track-metadata-module="footer"track-metadata-eventDetail="cloud.google.com/why-google-cloud/"> Choosing Google Cloud </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/trust-center/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 2)" track-metadata-eventDetail="cloud.google.com/security/"track-metadata-position="footer"track-metadata-child_headline="why google"track-metadata-module="footer"track-name="trust and security"track-type="footer link"> Trust and security </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/solutions/modern-infrastructure/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 3)" track-metadata-position="footer"track-type="footer link"track-metadata-child_headline="why google"track-name="modern infrastructure cloud"track-metadata-eventDetail="cloud.google.com/solutions/modern-infrastructure/"track-metadata-module="footer"> Modern Infrastructure Cloud </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/multicloud/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 4)" track-metadata-child_headline="why google"track-metadata-eventDetail="cloud.google.com/multicloud/"track-metadata-module="footer"track-metadata-position="footer"track-type="footer link"track-name="multicloud"> Multicloud </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/infrastructure/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 5)" track-metadata-child_headline="why google"track-metadata-module="footer"track-name="global infrastructure"track-metadata-eventDetail="cloud.google.com/infrastructure/"track-type="footer link"track-metadata-position="footer"> Global infrastructure </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/customers/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 6)" track-metadata-module="footer"track-metadata-position="footer"track-metadata-child_headline="why google"track-metadata-eventDetail="cloud.google.com/customers/"track-type="footer link"track-name="customers and case studies"> Customers and case studies </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/analyst-reports/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 7)" track-metadata-child_headline="why google"track-name="analyst reports"track-metadata-module="footer"track-metadata-eventDetail="cloud.google.com/analyst-reports/"track-metadata-position="footer"track-type="footer link"> Analyst reports </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/whitepapers/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 8)" track-name="whitepapers"track-metadata-eventDetail="cloud.google.com/whitepapers/"track-metadata-module="footer"track-metadata-position="footer"track-type="footer link"track-metadata-child_headline="why google"> Whitepapers </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//cloud.google.com/blog/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 9)" track-name="blog"track-metadata-child_headline="engage"track-type="footer link"track-metadata-position="footer"track-metadata-module="footer"track-metadata-eventDetail="cloud.google.com/blog/"> Blog </a> </li> </ul> </li> <li class="devsite-footer-linkbox "> <h3 class="devsite-footer-linkbox-heading no-link">Products and pricing</h3> <ul class="devsite-footer-linkbox-list"> <li class="devsite-footer-linkbox-item"> <a href="/pricing/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 1)" track-metadata-child_headline="products and pricing"track-metadata-eventDetail="cloud.google.com/pricing/"track-type="footer link"track-metadata-position="footer"track-metadata-module="footer"track-name="google cloud pricing"> Google Cloud pricing </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//workspace.google.com/pricing.html" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 2)" track-metadata-module="footer"track-type="footer link"track-metadata-eventDetail="workspace.google.com/pricing.html"target="_blank"track-metadata-child_headline="products and pricing"track-name="google workspace pricing"track-metadata-position="footer"> Google Workspace pricing </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/products/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 3)" track-name="see all products"track-metadata-position="footer"track-type="footer link"track-metadata-module="footer"track-metadata-eventDetail="cloud.google.com/products/"track-metadata-child_headline="products and pricing"> See all products </a> </li> </ul> </li> <li class="devsite-footer-linkbox "> <h3 class="devsite-footer-linkbox-heading no-link">Solutions</h3> <ul class="devsite-footer-linkbox-list"> <li class="devsite-footer-linkbox-item"> <a href="/solutions/infrastructure-modernization/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 1)" track-metadata-child_headline="solutions"track-metadata-eventDetail="cloud.google.com/solutions/infrastructure-modernization/"track-type="footer link"track-metadata-module="footer"track-name="infrastructure modernization"track-metadata-position="footer"> Infrastructure modernization </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/solutions/databases/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 2)" track-name="databases"track-metadata-module="footer"track-metadata-position="footer"track-metadata-eventDetail="cloud.google.com/solutions/databases"track-metadata-child_headline="solutions"track-type="footer link"> Databases </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/solutions/application-modernization/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 3)" track-name="application development"track-type="footer link"track-metadata-position="footer"track-metadata-module="footer"track-metadata-eventDetail="cloud.google.com/solutions/application-modernization/"track-metadata-child_headline="solutions"> Application modernization </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/solutions/smart-analytics/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 4)" track-name="smart analytics"track-metadata-module="footer"track-metadata-child_headline="solutions"track-type="footer link"track-metadata-eventDetail="cloud.google.com/solutions/smart-analytics/"track-metadata-position="footer"> Smart analytics </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/solutions/ai/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 5)" track-type="footer link"track-metadata-position="footer"track-metadata-eventDetail="cloud.google.com/solutions/ai/"track-metadata-child_headline="solutions"track-metadata-module="footer"track-name="artificial intelligence"> Artificial Intelligence </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/solutions/security/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 6)" track-name="security"track-type="footer link"track-metadata-child_headline="solutions"track-metadata-module="footer"track-metadata-position="footer"track-metadata-eventDetail="cloud.google.com/solutions/security/"> Security </a> </li> <li class="devsite-footer-linkbox-item"> <a href="https://workspace.google.com/enterprise/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 7)" track-metadata-child_headline="solutions"track-type="footer link"track-metadata-module="footer"track-name="productivity and work transformation"track-metadata-position="footer"target="_blank"track-metadata-eventDetail="workspace.google.com/enterprise/"> Productivity & work transformation </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/solutions/#industry-solutions" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 8)" track-name="industry solutions"track-metadata-module="footer"track-metadata-eventDetail="cloud.google.com/solutions/#industry-solutions"track-metadata-position="footer"track-metadata-child_headline="solutions"track-type="footer link"> Industry solutions </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/solutions/devops/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 9)" track-metadata-position="footer"track-name="devops solutions"track-metadata-eventDetail="cloud.google.com/solutions/devops/"track-type="footer link"track-metadata-child_headline="solutions"track-metadata-module="footer"> DevOps solutions </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/solutions/#section-14" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 10)" track-metadata-child_headline="solutions"track-type="footer link"track-metadata-eventDetail="cloud.google.com/solutions/#section-14"track-metadata-position="footer"track-metadata-module="footer"track-name="small business solutions"> Small business solutions </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/solutions/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 11)" track-metadata-eventDetail="cloud.google.com/solutions/"track-metadata-position="footer"track-metadata-child_headline="solutions"track-type="footer link"track-metadata-module="footer"track-name="see all solutions"> See all solutions </a> </li> </ul> </li> <li class="devsite-footer-linkbox "> <h3 class="devsite-footer-linkbox-heading no-link">Resources</h3> <ul class="devsite-footer-linkbox-list"> <li class="devsite-footer-linkbox-item"> <a href="/affiliate-program/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 1)" track-type="footer link"track-name="google cloud affiliate program"track-metadata-eventDetail="cloud.google.com/affiliate-program/"track-metadata-module="footer"track-metadata-position="footer"track-metadata-child_headline="resources"> Google Cloud Affiliate Program </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/docs/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 2)" track-name="google cloud documentation"track-metadata-position="footer"track-metadata-child_headline="resources"track-metadata-eventDetail="cloud.google.com/docs/"track-metadata-module="footer"track-type="footer link"> Google Cloud documentation </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/docs/get-started/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 3)" track-name="google cloud quickstarts"track-metadata-child_headline="resources"track-metadata-eventDetail="cloud.google.com/docs/get-started/"track-metadata-module="footer"track-metadata-position="footer"track-type="footer link"> Google Cloud quickstarts </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/marketplace/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 4)" track-metadata-module="footer"track-name="google cloud marketplace"track-metadata-position="footer"track-type="footer link"track-metadata-child_headline="resources"track-metadata-eventDetail="cloud.google.com/marketplace/"> Google Cloud Marketplace </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/discover/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 5)" track-metadata-module="footer"track-type="footer link"track-metadata-child_headline="resources"track-metadata-eventDetail="learn/"track-name="learn about cloud computing"track-metadata-position="footer"> Learn about cloud computing </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/support-hub/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 6)" track-name="support"track-metadata-child_headline="resources"track-type="footer link"track-metadata-eventDetail="cloud.google.com/support-hub/"track-metadata-position="footer"track-metadata-module="footer"> Support </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/docs/samples" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 7)" track-metadata-module="footer"track-type="footer link"track-name="code samples"track-metadata-child_headline="resources"track-metadata-eventDetail="cloud.google.com/docs/samples"track-metadata-position="footer"> Code samples </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/architecture/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 8)" track-name="cloud architecture center"track-type="footer link"track-metadata-module="footer"track-metadata-child_headline="resources"track-metadata-position="footer"track-metadata-eventDetail="cloud.google.com/architecture/"> Cloud Architecture Center </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/learn/training/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 9)" track-metadata-child_headline="resources"track-metadata-position="footer"track-metadata-eventDetail="cloud.google.com/training/"track-metadata-module="footer"track-type="footer link"track-name="training"> Training </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/learn/certification/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 10)" track-metadata-eventDetail="cloud.google.com/certification"track-metadata-position="footer"track-metadata-child_headline="resources"track-metadata-module="footer"track-name="certifications"track-type="footer link"> Certifications </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//developers.google.com" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 11)" track-name="google developers"track-metadata-module="footer"track-metadata-child_headline="resources"track-metadata-position="footer"target="_blank"track-type="footer link"track-metadata-eventDetail="developers.google.com"> Google for Developers </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/startup/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 12)" track-name="google cloud for startups"track-metadata-child_headline="resources"track-metadata-eventDetail="cloud.google.com/startup/"track-type="footer link"track-metadata-position="footer"track-metadata-module="footer"> Google Cloud for Startups </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//status.cloud.google.com" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 13)" track-type="footer link"track-metadata-eventDetail="status.cloud.google.com"track-name="system status"target="_blank"track-metadata-position="footer"track-metadata-child_headline="resources"track-metadata-module="footer"> System status </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/release-notes" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 14)" track-metadata-position="footer"track-metadata-module="footer"track-type="footer link"track-metadata-eventDetail="cloud.google.com/release-notes/"track-metadata-child_headline="resources"track-name="release notes"> Release Notes </a> </li> </ul> </li> <li class="devsite-footer-linkbox "> <h3 class="devsite-footer-linkbox-heading no-link">Engage</h3> <ul class="devsite-footer-linkbox-list"> <li class="devsite-footer-linkbox-item"> <a href="/contact/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 1)" track-metadata-module="footer"track-type="footer link"track-metadata-child_headline="engage"track-metadata-eventDetail="cloud.google.com/contact/"track-metadata-position="footer"track-name="contact sales"> Contact sales </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//cloud.google.com/find-a-partner" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 2)" track-metadata-eventDetail="cloud.google.com/find-a-partner"track-metadata-child_headline="engage"track-name="find a partner"track-type="footer link"track-metadata-module="footer"track-metadata-position="footer"target="_blank"> Find a Partner </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/partners/become-a-partner/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 3)" track-metadata-child_headline="engage"track-metadata-position="footer"track-metadata-eventDetail="cloud.google.com/partners/become-a-partner/"track-type="footer link"track-metadata-module="footer"track-name="become a partner"> Become a Partner </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/events/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 4)" track-type="footer link"track-metadata-eventDetail="cloud.withgoogle.com/events"track-name="events"track-metadata-module="footer"track-metadata-child_headline="engage"track-metadata-position="footer"> Events </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/podcasts/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 5)" track-metadata-module="footer"target="_blank"track-metadata-position="footer"track-name="podcasts"track-metadata-child_headline="engage"track-type="footer link"track-metadata-eventDetail="cloud.google.com/podcasts/"rel="noopener"> Podcasts </a> </li> <li class="devsite-footer-linkbox-item"> <a href="/developers/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 6)" track-metadata-position="footer"track-metadata-eventDetail="cloud.google.com/developers/"track-metadata-child_headline="engage"track-type="footer link"track-metadata-module="footer"track-name="developer center"> Developer Center </a> </li> <li class="devsite-footer-linkbox-item"> <a href="https://www.googlecloudpresscorner.com/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 7)" track-type="footer link"target="_blank"track-name="press corner"track-metadata-module="footer"track-metadata-eventDetail="www.googlecloudpresscorner.com"rel="noopener"track-metadata-position="footer"track-metadata-child_headline="engage"> Press Corner </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//www.youtube.com/googlecloud" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 8)" track-metadata-position="footer"track-name="google cloud on youtube"target="_blank"track-type="footer link"track-metadata-module="footer"track-metadata-eventDetail="www.youtube.com/googlecloud"track-metadata-child_headline="engage"rel="noopener"> Google Cloud on YouTube </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//www.youtube.com/googlecloudplatform" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 9)" target="_blank"track-type="footer link"track-metadata-child_headline="engage"track-name="google cloud tech on youtube"track-metadata-eventDetail="www.youtube.com/googlecloudplatform"rel="noopener"track-metadata-module="footer"track-metadata-position="footer"> Google Cloud Tech on YouTube </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//x.com/googlecloud" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 10)" track-metadata-module="footer"track-metadata-eventDetail="x.com/googlecloud"track-metadata-child_headline="engage"track-metadata-position="footer"rel="noopener"track-type="footer link"target="_blank"track-name="follow on x"> Follow on X </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//userresearch.google.com/?reserved=1&utm_source=website&Q_Language=en&utm_medium=own_srch&utm_campaign=CloudWebFooter&utm_term=0&utm_content=0&productTag=clou&campaignDate=jul19&pType=devel&referral_code=jk212693" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 11)" track-metadata-eventDetail="userresearch.google.com/?reserved=1&utm_source=website&Q_Language=en&utm_medium=own_srch&utm_campaign=CloudWebFooter&utm_term=0&utm_content=0&productTag=clou&campaignDate=jul19&pType=devel&referral_code=jk212693"track-metadata-child_headline="engage"track-name="join user research"track-metadata-position="footer"target="_blank"track-metadata-module="footer"track-type="footer link"> Join User Research </a> </li> <li class="devsite-footer-linkbox-item"> <a href="//careers.google.com/cloud" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 12)" track-metadata-eventDetail="careers.google.com/cloud"track-name="we are hiring join google cloud"track-type="footer link"target="_blank"track-metadata-module="footer"track-metadata-position="footer"track-metadata-child_headline="engage"> We're hiring. Join Google Cloud! </a> </li> <li class="devsite-footer-linkbox-item"> <a href="https://www.googlecloudcommunity.com/" class="devsite-footer-linkbox-link gc-analytics-event" data-category="Site-Wide Custom Events" data-label="Footer Link (index 13)" track-type="footer link"track-metadata-position="footer"rel="noopener"track-metadata-eventDetail="www.googlecloudcommunity.com"track-metadata-module="footer"track-metadata-child_headline="engage"target="_blank"track-name="google cloud community"> Google Cloud Community </a> </li> </ul> </li> </ul> </nav> </devsite-footer-linkboxes> <devsite-footer-utility class="devsite-footer"> <div class="devsite-footer-utility nocontent"> <nav class="devsite-footer-utility-links" aria-label="Utility links"> <ul class="devsite-footer-utility-list"> <li class="devsite-footer-utility-item "> <a class="devsite-footer-utility-link gc-analytics-event" href="//about.google/" data-category="Site-Wide Custom Events" data-label="Footer About Google link" track-metadata-module="utility footer" track-metadata-position="footer" track-name="about google" track-metadata-eventDetail="//about.google/" target="_blank" track-type="footer link" > About Google </a> </li> <li class="devsite-footer-utility-item devsite-footer-privacy-link"> <a class="devsite-footer-utility-link gc-analytics-event" href="//policies.google.com/privacy" data-category="Site-Wide Custom Events" data-label="Footer Privacy link" track-type="footer link" track-metadata-eventDetail="//policies.google.com/privacy" track-name="privacy" track-metadata-module="utility footer" track-metadata-position="footer" target="_blank" > Privacy </a> </li> <li class="devsite-footer-utility-item "> <a class="devsite-footer-utility-link gc-analytics-event" href="//www.google.com/intl/en/policies/terms/regional.html" data-category="Site-Wide Custom Events" data-label="Footer Site terms link" track-type="footer link" track-metadata-position="footer" track-name="site terms" track-metadata-eventDetail="//www.google.com/intl/en/policies/terms/regional.html" target="_blank" track-metadata-module="utility footer" > Site terms </a> </li> <li class="devsite-footer-utility-item "> <a class="devsite-footer-utility-link gc-analytics-event" href="/product-terms/" data-category="Site-Wide Custom Events" data-label="Footer Google Cloud terms link" track-metadata-eventDetail="/product-terms/" track-type="footer link" track-metadata-module="utility footer" track-metadata-position="footer" track-name="google cloud terms" > Google Cloud terms </a> </li> <li class="devsite-footer-utility-item glue-cookie-notification-bar-control"> <a class="devsite-footer-utility-link gc-analytics-event" href="#" data-category="Site-Wide Custom Events" data-label="Footer Manage cookies link" track-metadata-eventDetail="#" track-metadata-position="footer" track-metadata-module="utility footer" track-name="Manage cookies" aria-hidden="true" track-type="footer link" > Manage cookies </a> </li> <li class="devsite-footer-utility-item devsite-footer-carbon-button"> <a class="devsite-footer-utility-link gc-analytics-event" href="/sustainability" data-category="Site-Wide Custom Events" data-label="Footer Our third decade of climate action: join us link" track-name="Our third decade of climate action: join us" track-metadata-module="utility footer" track-metadata-position="footer" track-type="footer link" track-metadata-eventDetail="/sustainability/" > Our third decade of climate action: join us </a> </li> <li class="devsite-footer-utility-item devsite-footer-utility-button"> <span class="devsite-footer-utility-description">Sign up for the Google Cloud newsletter</span> <a class="devsite-footer-utility-link gc-analytics-event" href="/newsletter/" data-category="Site-Wide Custom Events" data-label="Footer Subscribe link" track-metadata-eventDetail="/newsletter/" track-name="subscribe" track-type="footer link" track-metadata-position="footer" track-metadata-module="utility footer" > Subscribe </a> </li> </ul> <devsite-language-selector> <ul role="presentation"> <li role="presentation"> <a role="menuitem" lang="en" >English</a> </li> <li role="presentation"> <a role="menuitem" lang="de" >Deutsch</a> </li> <li role="presentation"> <a role="menuitem" lang="es_419" >Español – América Latina</a> </li> <li role="presentation"> <a role="menuitem" lang="fr" >Français</a> </li> <li role="presentation"> <a role="menuitem" lang="id" >Indonesia</a> </li> <li role="presentation"> <a role="menuitem" lang="it" >Italiano</a> </li> <li role="presentation"> <a role="menuitem" lang="pt_br" >Português – Brasil</a> </li> <li role="presentation"> <a role="menuitem" lang="zh_cn" >中文 – 简体</a> </li> <li role="presentation"> <a role="menuitem" lang="ja" >日本語</a> </li> <li role="presentation"> <a role="menuitem" lang="ko" >한국어</a> </li> </ul> </devsite-language-selector> </nav> </div> </devsite-footer-utility> <devsite-panel></devsite-panel> </section></section> <devsite-sitemask></devsite-sitemask> <devsite-snackbar></devsite-snackbar> <devsite-tooltip ></devsite-tooltip> <devsite-heading-link></devsite-heading-link> <devsite-analytics> <script type="application/json" analytics>[]</script> <script type="application/json" tag-management>{"at": "True", "ga4": [], "ga4p": [], "gtm": [{"id": "GTM-5CVQBG", "purpose": 1}], "parameters": {"internalUser": "False", "language": {"machineTranslated": "False", "requested": "en", "served": "en"}, "pageType": "article", "projectName": "Pub/Sub Documentation", "signedIn": "False", "tenant": "cloud", "recommendations": {"sourcePage": "", "sourceType": 0, "sourceRank": 0, "sourceIdenticalDescriptions": 0, "sourceTitleWords": 0, "sourceDescriptionWords": 0, "experiment": ""}, "experiment": {"ids": ""}}}</script> </devsite-analytics> <devsite-badger></devsite-badger> <cloudx-user></cloudx-user> <cloudx-free-trial-eligible-store freeTrialEligible='true'></cloudx-free-trial-eligible-store> <cloudx-pricing-socket></cloudx-pricing-socket> <cloudx-experiments type="TestAACodivertedExperiment" path="/virtual/TestAACodivertedExperiment/configureExperiment" location="SG" variant="variant2" ></cloudx-experiments> <cloudx-experiment-ids userCountry="SG" devsiteExperimentIdList="[39300012, 39300020, 39300118, 39300192, 39300195, 39300241, 39300317, 39300320, 39300324, 39300345, 39300354, 39300363, 39300374, 39300412, 39300422, 39300436, 39300471, 39300488, 39300496, 39300498]"> </cloudx-experiment-ids> <script nonce="AoqtxzlAlzq0jAvpnHaehOtY9zMNgD"> (function(d,e,v,s,i,t,E){d['GoogleDevelopersObject']=i; t=e.createElement(v);t.async=1;t.src=s;E=e.getElementsByTagName(v)[0]; E.parentNode.insertBefore(t,E);})(window, document, 'script', 'https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/cloud/js/app_loader.js', '[2,"en",null,"/js/devsite_app_module.js","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/cloud","https://cloud-dot-devsite-v2-prod.appspot.com",null,null,["/_pwa/cloud/manifest.json","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/images/video-placeholder.svg","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/cloud/images/favicons/onecloud/favicon.ico","https://www.gstatic.com/devrel-devsite/prod/v870e399c64f7c43c99a3043db4b3a74327bb93d0914e84a0c3dba90bbfd67625/cloud/images/cloud-logo.svg","https://fonts.googleapis.com/css?family=Google+Sans:400,500,700|Google+Sans+Text:400,400italic,500,500italic,700,700italic|Roboto:400,400italic,500,500italic,700,700italic|Roboto+Mono:400,500,700&display=swap"],1,null,[1,6,8,12,14,17,21,25,50,52,63,70,75,76,80,87,91,92,93,97,98,100,101,102,103,104,105,107,108,109,110,112,113,116,117,118,120,122,124,125,126,127,129,130,131,132,133,134,135,136,138,140,141,147,148,149,151,152,156,157,158,159,161,163,164,168,169,170,179,180,182,183,186,191,193,196],"AIzaSyAP-jjEJBzmIyKR4F-3XITp8yM9T1gEEI8","AIzaSyB6xiKGDR5O3Ak2okS4rLkauxGUG7XP0hg","cloud.google.com","AIzaSyAQk0fBONSGUqCNznf6Krs82Ap1-NV6J4o","AIzaSyCCxcqdrZ_7QMeLCRY20bh_SXdAYqy70KY",null,null,null,["Search__scope_to_project_tenant","Cloud__enable_cloud_facet_chat","MiscFeatureFlags__enable_view_transitions","Search__enable_ai_search_summaries","Search__enable_ai_eligibility_checks","MiscFeatureFlags__developers_footer_image","MiscFeatureFlags__emergency_css","Cloud__enable_cloud_shell","Analytics__enable_clearcut_logging","Profiles__enable_awarding_url","MiscFeatureFlags__enable_project_variables","CloudShell__cloud_code_overflow_menu","MiscFeatureFlags__enable_firebase_utm","MiscFeatureFlags__developers_footer_dark_image","Concierge__enable_concierge_restricted","MiscFeatureFlags__enable_variable_operator","DevPro__enable_developer_subscriptions","Cloud__enable_llm_concierge_chat","BookNav__enable_tenant_cache_key","Search__enable_page_map","Cloud__enable_cloud_shell_fte_user_flow","Concierge__enable_pushui","Profiles__enable_developer_profiles_callout","Profiles__enable_page_saving","Profiles__enable_profile_collections","Profiles__enable_public_developer_profiles","Cloud__enable_legacy_calculator_redirect","Experiments__reqs_query_experiments","Search__enable_suggestions_from_borg","Search__enable_dynamic_content_confidential_banner","Profiles__enable_dashboard_curated_recommendations","Search__enable_ai_search_summaries_restricted","Cloud__enable_cloud_dlp_service","Profiles__enable_recognition_badges","Profiles__enable_release_notes_notifications","TpcFeatures__enable_required_headers","CloudShell__cloud_shell_button","Cloud__enable_free_trial_server_call","MiscFeatureFlags__enable_explain_this_code","Profiles__require_profile_eligibility_for_signin","DevPro__enable_cloud_innovators_plus","Profiles__enable_completecodelab_endpoint","Profiles__enable_complete_playlist_endpoint","Cloud__enable_cloudx_experiment_ids","EngEduTelemetry__enable_engedu_telemetry","TpcFeatures__enable_mirror_tenant_redirects","Cloud__enable_cloudx_ping"],null,null,"AIzaSyBLEMok-5suZ67qRPzx0qUtbnLmyT_kCVE","https://developerscontentserving-pa.clients6.google.com","AIzaSyCM4QpTRSqP5qI4Dvjt4OAScIN8sOUlO-k","https://developerscontentsearch-pa.clients6.google.com",1,4,1,"https://developerprofiles-pa.clients6.google.com",[2,"cloud","Google Cloud","cloud.google.com",null,"cloud-dot-devsite-v2-prod.appspot.com",null,null,[1,1,null,null,null,null,null,null,null,null,null,[1],null,null,null,null,null,1,[1],[null,null,null,[1,20],"/terms/recommendations"],[1],null,[1],[1,null,1],[1,1,null,null,1,null,["/vertex-ai/"]]],null,[22,null,null,null,null,null,"/images/cloud-logo.svg","/images/favicons/onecloud/apple-icon.png",null,null,null,null,1,1,1,[6,5],[],null,null,[[],[],[],[],[],[],[],[]],null,1,null,null,null,null,[]],[],null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,[6,1,14,15,22,23,29,37],null,[[null,null,null,null,null,null,[1,[["docType","Choose a content type",[["ApiReference",null,null,null,null,null,null,null,null,"API reference"],["Sample",null,null,null,null,null,null,null,null,"Code sample"],["ReferenceArchitecture",null,null,null,null,null,null,null,null,"Reference architecture"],["Tutorial",null,null,null,null,null,null,null,null,"Tutorial"]]],["category","Choose a topic",[["AiAndMachineLearning",null,null,null,null,null,null,null,null,"Artificial intelligence and machine learning (AI/ML)"],["ApplicationDevelopment",null,null,null,null,null,null,null,null,"Application development"],["BigDataAndAnalytics",null,null,null,null,null,null,null,null,"Big data and analytics"],["Compute",null,null,null,null,null,null,null,null,"Compute"],["Containers",null,null,null,null,null,null,null,null,"Containers"],["Databases",null,null,null,null,null,null,null,null,"Databases"],["HybridCloud",null,null,null,null,null,null,null,null,"Hybrid and multicloud"],["LoggingAndMonitoring",null,null,null,null,null,null,null,null,"Logging and monitoring"],["Migrations",null,null,null,null,null,null,null,null,"Migrations"],["Networking",null,null,null,null,null,null,null,null,"Networking"],["SecurityAndCompliance",null,null,null,null,null,null,null,null,"Security and compliance"],["Serverless",null,null,null,null,null,null,null,null,"Serverless"],["Storage",null,null,null,null,null,null,null,null,"Storage"]]]]]],[1],null,1],[[null,null,null,null,null,["GTM-5CVQBG"],null,null,null,null,null,[["GTM-5CVQBG",2]],1],null,null,null,null,null,1],"mwETRvWii0eU5NUYprb0Y9z5GVbc",4,null,null,null,null,null,null,null,null,null,null,null,null,null,"cloud.devsite.google"],null,"pk_live_5170syrHvgGVmSx9sBrnWtA5luvk9BwnVcvIi7HizpwauFG96WedXsuXh790rtij9AmGllqPtMLfhe2RSwD6Pn38V00uBCydV4m"]') </script> <devsite-a11y-announce></devsite-a11y-announce> </body> </html>