UNPKG

20.2 kBMarkdownView Raw
1[//]: # "This README.md file is auto-generated, all changes to this file will be lost."
2[//]: # "To regenerate it, use `python -m synthtool`."
3<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>
4
5# [Google Cloud Pub/Sub: Node.js Client](https://github.com/googleapis/nodejs-pubsub)
6
7[![release level](https://img.shields.io/badge/release%20level-general%20availability%20%28GA%29-brightgreen.svg?style=flat)](https://cloud.google.com/terms/launch-stages)
8[![npm version](https://img.shields.io/npm/v/@google-cloud/pubsub.svg)](https://www.npmjs.org/package/@google-cloud/pubsub)
9[![codecov](https://img.shields.io/codecov/c/github/googleapis/nodejs-pubsub/master.svg?style=flat)](https://codecov.io/gh/googleapis/nodejs-pubsub)
10
11
12
13
14[Cloud Pub/Sub](https://cloud.google.com/pubsub/docs) is a fully-managed real-time messaging service that allows
15you to send and receive messages between independent applications.
16
17This document contains links to an [API reference](https://googleapis.dev/nodejs/pubsub/latest/index.html#reference), samples,
18and other resources useful to developing Node.js applications.
19For additional help developing Pub/Sub applications, in Node.js and other languages, see our
20[Pub/Sub quickstart](https://cloud.google.com/pubsub/docs/quickstart-client-libraries),
21[publisher](https://cloud.google.com/pubsub/docs/publisher), and [subscriber](https://cloud.google.com/pubsub/docs/subscriber)
22guides.
23
24
25A comprehensive list of changes in each version may be found in
26[the CHANGELOG](https://github.com/googleapis/nodejs-pubsub/blob/master/CHANGELOG.md).
27
28* [Google Cloud Pub/Sub Node.js Client API Reference][client-docs]
29* [Google Cloud Pub/Sub Documentation][product-docs]
30* [github.com/googleapis/nodejs-pubsub](https://github.com/googleapis/nodejs-pubsub)
31
32Read more about the client libraries for Cloud APIs, including the older
33Google APIs Client Libraries, in [Client Libraries Explained][explained].
34
35[explained]: https://cloud.google.com/apis/docs/client-libraries-explained
36
37**Table of contents:**
38
39
40* [Quickstart](#quickstart)
41 * [Before you begin](#before-you-begin)
42 * [Installing the client library](#installing-the-client-library)
43 * [Using the client library](#using-the-client-library)
44* [Samples](#samples)
45* [Versioning](#versioning)
46* [Contributing](#contributing)
47* [License](#license)
48
49## Quickstart
50
51### Before you begin
52
531. [Select or create a Cloud Platform project][projects].
541. [Enable billing for your project][billing].
551. [Enable the Google Cloud Pub/Sub API][enable_api].
561. [Set up authentication with a service account][auth] so you can access the
57 API from your local workstation.
58
59### Installing the client library
60
61```bash
62npm install @google-cloud/pubsub
63```
64
65
66### Using the client library
67
68```javascript
69// Imports the Google Cloud client library
70const {PubSub} = require('@google-cloud/pubsub');
71
72async function quickstart(
73 projectId = 'your-project-id', // Your Google Cloud Platform project ID
74 topicName = 'my-topic', // Name for the new topic to create
75 subscriptionName = 'my-sub' // Name for the new subscription to create
76) {
77 // Instantiates a client
78 const pubsub = new PubSub({projectId});
79
80 // Creates a new topic
81 const [topic] = await pubsub.createTopic(topicName);
82 console.log(`Topic ${topic.name} created.`);
83
84 // Creates a subscription on that new topic
85 const [subscription] = await topic.createSubscription(subscriptionName);
86
87 // Receive callbacks for new messages on the subscription
88 subscription.on('message', message => {
89 console.log('Received message:', message.data.toString());
90 process.exit(0);
91 });
92
93 // Receive callbacks for errors on the subscription
94 subscription.on('error', error => {
95 console.error('Received error:', error);
96 process.exit(1);
97 });
98
99 // Send a message to the topic
100 topic.publish(Buffer.from('Test message!'));
101}
102
103```
104## Running gRPC C++ bindings
105
106For some workflows and environments it might make sense to use the C++ gRPC implementation,
107instead of the default one (see: [#770](https://github.com/googleapis/nodejs-pubsub/issues/770)):
108
109To configure `@google-cloud/pubsub` to use an alternative `grpc` transport:
110
1111. `npm install grpc`, adding `grpc` as a dependency.
1121. instantiate `@google-cloud/pubsub` with `grpc`:
113
114 ```js
115 const {PubSub} = require('@google-cloud/pubsub');
116 const grpc = require('grpc');
117 const pubsub = new PubSub({grpc});
118 ```
119
120
121## Samples
122
123Samples are in the [`samples/`](https://github.com/googleapis/nodejs-pubsub/tree/master/samples) directory. Each sample's `README.md` has instructions for running its sample.
124
125| Sample | Source Code | Try it |
126| --------------------------- | --------------------------------- | ------ |
127| Create Push Subscription | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/createPushSubscription.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createPushSubscription.js,samples/README.md) |
128| Create Subscription | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/createSubscription.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createSubscription.js,samples/README.md) |
129| Create Subscription With Dead Letter Policy | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/createSubscriptionWithDeadLetterPolicy.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createSubscriptionWithDeadLetterPolicy.js,samples/README.md) |
130| Create Subscription with ordering enabled | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/createSubscriptionWithOrdering.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createSubscriptionWithOrdering.js,samples/README.md) |
131| Create Topic | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/createTopic.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/createTopic.js,samples/README.md) |
132| Delete Subscription | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/deleteSubscription.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/deleteSubscription.js,samples/README.md) |
133| Delete Topic | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/deleteTopic.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/deleteTopic.js,samples/README.md) |
134| Detach Subscription | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/detachSubscription.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/detachSubscription.js,samples/README.md) |
135| Get Subscription | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/getSubscription.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/getSubscription.js,samples/README.md) |
136| Get Subscription Policy | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/getSubscriptionPolicy.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/getSubscriptionPolicy.js,samples/README.md) |
137| Get Topic Policy | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/getTopicPolicy.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/getTopicPolicy.js,samples/README.md) |
138| List All Topics | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/listAllTopics.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/listAllTopics.js,samples/README.md) |
139| List Subscriptions | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/listSubscriptions.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/listSubscriptions.js,samples/README.md) |
140| List Subscriptions On a Topic | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/listTopicSubscriptions.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/listTopicSubscriptions.js,samples/README.md) |
141| Listen For Errors | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/listenForErrors.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/listenForErrors.js,samples/README.md) |
142| Listen For Messages | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/listenForMessages.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/listenForMessages.js,samples/README.md) |
143| Listen For Messages With Custom Attributes | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/listenWithCustomAttributes.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/listenWithCustomAttributes.js,samples/README.md) |
144| Modify Push Configuration | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/modifyPushConfig.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/modifyPushConfig.js,samples/README.md) |
145| OpenTelemetry Tracing | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/openTelemetryTracing.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/openTelemetryTracing.js,samples/README.md) |
146| Publish Batched Messages | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/publishBatchedMessages.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/publishBatchedMessages.js,samples/README.md) |
147| Publish Message | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/publishMessage.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/publishMessage.js,samples/README.md) |
148| Publish Message With Custom Attributes | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/publishMessageWithCustomAttributes.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/publishMessageWithCustomAttributes.js,samples/README.md) |
149| Publish Ordered Message | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/publishOrderedMessage.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/publishOrderedMessage.js,samples/README.md) |
150| Publish With Retry Settings | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/publishWithRetrySettings.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/publishWithRetrySettings.js,samples/README.md) |
151| Quickstart | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/quickstart.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/quickstart.js,samples/README.md) |
152| Remove Dead Letter Policy | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/removeDeadLetterPolicy.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/removeDeadLetterPolicy.js,samples/README.md) |
153| Resume Publish | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/resumePublish.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/resumePublish.js,samples/README.md) |
154| Set Subscription IAM Policy | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/setSubscriptionPolicy.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/setSubscriptionPolicy.js,samples/README.md) |
155| Set Topic IAM Policy | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/setTopicPolicy.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/setTopicPolicy.js,samples/README.md) |
156| Subscribe With Flow Control Settings | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/subscribeWithFlowControlSettings.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/subscribeWithFlowControlSettings.js,samples/README.md) |
157| Synchronous Pull | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/synchronousPull.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/synchronousPull.js,samples/README.md) |
158| Synchronous Pull with delivery attempt. | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/synchronousPullWithDeliveryAttempts.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/synchronousPullWithDeliveryAttempts.js,samples/README.md) |
159| Synchronous Pull With Lease Management | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/synchronousPullWithLeaseManagement.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/synchronousPullWithLeaseManagement.js,samples/README.md) |
160| Test Subscription Permissions | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/testSubscriptionPermissions.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/testSubscriptionPermissions.js,samples/README.md) |
161| Test Topic Permissions | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/testTopicPermissions.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/testTopicPermissions.js,samples/README.md) |
162| Update Dead Letter Policy | [source code](https://github.com/googleapis/nodejs-pubsub/blob/master/samples/updateDeadLetterPolicy.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-pubsub&page=editor&open_in_editor=samples/updateDeadLetterPolicy.js,samples/README.md) |
163
164
165
166The [Google Cloud Pub/Sub Node.js Client API Reference][client-docs] documentation
167also contains samples.
168
169## Supported Node.js Versions
170
171Our client libraries follow the [Node.js release schedule](https://nodejs.org/en/about/releases/).
172Libraries are compatible with all current _active_ and _maintenance_ versions of
173Node.js.
174
175Client libraries targeting some end-of-life versions of Node.js are available, and
176can be installed via npm [dist-tags](https://docs.npmjs.com/cli/dist-tag).
177The dist-tags follow the naming convention `legacy-(version)`.
178
179_Legacy Node.js versions are supported as a best effort:_
180
181* Legacy versions will not be tested in continuous integration.
182* Some security patches may not be able to be backported.
183* Dependencies will not be kept up-to-date, and features will not be backported.
184
185#### Legacy tags available
186
187* `legacy-8`: install client libraries from this dist-tag for versions
188 compatible with Node.js 8.
189
190## Versioning
191
192This library follows [Semantic Versioning](http://semver.org/).
193
194
195This library is considered to be **General Availability (GA)**. This means it
196is stable; the code surface will not change in backwards-incompatible ways
197unless absolutely necessary (e.g. because of critical security issues) or with
198an extensive deprecation period. Issues and requests against **GA** libraries
199are addressed with the highest priority.
200
201
202
203
204
205More Information: [Google Cloud Platform Launch Stages][launch_stages]
206
207[launch_stages]: https://cloud.google.com/terms/launch-stages
208
209## Contributing
210
211Contributions welcome! See the [Contributing Guide](https://github.com/googleapis/nodejs-pubsub/blob/master/CONTRIBUTING.md).
212
213Please note that this `README.md`, the `samples/README.md`,
214and a variety of configuration files in this repository (including `.nycrc` and `tsconfig.json`)
215are generated from a central template. To edit one of these files, make an edit
216to its template in this
217[directory](https://github.com/googleapis/synthtool/tree/master/synthtool/gcp/templates/node_library).
218
219## License
220
221Apache Version 2.0
222
223See [LICENSE](https://github.com/googleapis/nodejs-pubsub/blob/master/LICENSE)
224
225[client-docs]: https://googleapis.dev/nodejs/pubsub/latest
226[product-docs]: https://cloud.google.com/pubsub/docs/
227[shell_img]: https://gstatic.com/cloudssh/images/open-btn.png
228[projects]: https://console.cloud.google.com/project
229[billing]: https://support.google.com/cloud/answer/6293499#enable-billing
230[enable_api]: https://console.cloud.google.com/flows/enableapi?apiid=pubsub.googleapis.com
231[auth]: https://cloud.google.com/docs/authentication/getting-started
232
\No newline at end of file