Class Subscription (2.16.6)

A Subscription object will give you access to your Cloud Pub/Sub subscription.

Subscriptions are sometimes retrieved when using various methods:

Subscription objects may be created directly with:

All Subscription objects are instances of an [EventEmitter](http://nodejs.org/api/events.html). The subscription will pull for messages automatically as long as there is at least one listener assigned for the message event. Available events:

Upon receipt of a message: on(event: 'message', listener: (message: Message) => void): this;

Upon receipt of an error: on(event: 'error', listener: (error: Error) => void): this;

Upon the closing of the subscriber: on(event: 'close', listener: Function): this;

By default Subscription objects allow you to process 100 messages at the same time. You can fine tune this value by adjusting the options.flowControl.maxMessages option.

If your subscription is seeing more re-deliveries than preferable, you might try increasing your options.ackDeadline value or decreasing the options.streamingOptions.maxStreams value.

Subscription objects handle ack management, by automatically extending the ack deadline while the message is being processed, to then issue the ack or nack of such message when the processing is done. **Note:** message redelivery is still possible.

By default each PubSub instance can handle 100 open streams, with default options this translates to less than 20 Subscriptions per PubSub instance. If you wish to create more Subscriptions than that, you can either create multiple PubSub instances or lower the options.streamingOptions.maxStreams value on each Subscription object.

Inheritance

EventEmitter > Subscription

Package

@google-cloud/pubsub!

Constructors

(constructor)(pubsub, name, options)

constructor(pubsub: PubSub, name: string, options?: SubscriptionOptions);

Constructs a new instance of the Subscription class

Parameters
NameDescription
pubsub PubSub
name string
options SubscriptionOptions

Properties

iam

iam: IAM;
Property Value
TypeDescription
IAM

isOpen

get isOpen(): boolean;

Indicates if the Subscription is open and receiving messages.

{boolean}

Property Value
TypeDescription
boolean

metadata

metadata?: google.pubsub.v1.ISubscription;
Property Value
TypeDescription
google.pubsub.v1.ISubscription

name

name: string;
Property Value
TypeDescription
string

projectId

get projectId(): string;

{string}

Property Value
TypeDescription
string

pubsub

pubsub: PubSub;
Property Value
TypeDescription
PubSub

request

request: typeof PubSub.prototype.request;
Property Value
TypeDescription
typeof PubSub#request

topic

topic?: Topic | string;
Property Value
TypeDescription
Topic | string

Methods

close()

close(): Promise<void>;
Returns
TypeDescription
Promise<void>

close(callback)

close(callback: SubscriptionCloseCallback): void;
Parameter
NameDescription
callback SubscriptionCloseCallback
Returns
TypeDescription
void

create(options)

create(options?: CreateSubscriptionOptions): Promise<CreateSubscriptionResponse>;
Parameter
NameDescription
options CreateSubscriptionOptions
Returns
TypeDescription
Promise<CreateSubscriptionResponse>

create(callback)

create(callback: CreateSubscriptionCallback): void;
Parameter
NameDescription
callback CreateSubscriptionCallback
Returns
TypeDescription
void

create(options, callback)

create(options: CreateSubscriptionOptions, callback: CreateSubscriptionCallback): void;
Parameters
NameDescription
options CreateSubscriptionOptions
callback CreateSubscriptionCallback
Returns
TypeDescription
void

createSnapshot(name, gaxOpts)

createSnapshot(name: string, gaxOpts?: CallOptions): Promise<CreateSnapshotResponse>;
Parameters
NameDescription
name string
gaxOpts CallOptions
Returns
TypeDescription
Promise<CreateSnapshotResponse>

createSnapshot(name, callback)

createSnapshot(name: string, callback: CreateSnapshotCallback): void;
Parameters
NameDescription
name string
callback CreateSnapshotCallback
Returns
TypeDescription
void

createSnapshot(name, gaxOpts, callback)

createSnapshot(name: string, gaxOpts: CallOptions, callback: CreateSnapshotCallback): void;
Parameters
NameDescription
name string
gaxOpts CallOptions
callback CreateSnapshotCallback
Returns
TypeDescription
void

delete(gaxOpts)

delete(gaxOpts?: CallOptions): Promise<EmptyResponse>;
Parameter
NameDescription
gaxOpts CallOptions
Returns
TypeDescription
Promise<EmptyResponse>

delete(callback)

delete(callback: EmptyCallback): void;
Parameter
NameDescription
callback EmptyCallback
Returns
TypeDescription
void

delete(gaxOpts, callback)

delete(gaxOpts: CallOptions, callback: EmptyCallback): void;
Parameters
NameDescription
gaxOpts CallOptions
callback EmptyCallback
Returns
TypeDescription
void

detached()

detached(): Promise<DetachedResponse>;
Returns
TypeDescription
Promise<DetachedResponse>

detached(callback)

detached(callback: DetachedCallback): void;
Parameter
NameDescription
callback DetachedCallback
Returns
TypeDescription
void

exists()

exists(): Promise<ExistsResponse>;
Returns
TypeDescription
Promise<ExistsResponse>

exists(callback)

exists(callback: ExistsCallback): void;
Parameter
NameDescription
callback ExistsCallback
Returns
TypeDescription
void

formatMetadata_(metadata)

static formatMetadata_(metadata: SubscriptionMetadata): google.pubsub.v1.ISubscription;
Parameter
NameDescription
metadata SubscriptionMetadata
Returns
TypeDescription
google.pubsub.v1.ISubscription

formatName_(projectId, name)

static formatName_(projectId: string, name: string): string;
Parameters
NameDescription
projectId string
name string
Returns
TypeDescription
string

get(gaxOpts)

get(gaxOpts?: GetSubscriptionOptions): Promise<GetSubscriptionResponse>;
Parameter
NameDescription
gaxOpts GetSubscriptionOptions
Returns
TypeDescription
Promise<GetSubscriptionResponse>

get(callback)

get(callback: GetSubscriptionCallback): void;
Parameter
NameDescription
callback GetSubscriptionCallback
Returns
TypeDescription
void

get(gaxOpts, callback)

get(gaxOpts: GetSubscriptionOptions, callback: GetSubscriptionCallback): void;
Parameters
NameDescription
gaxOpts GetSubscriptionOptions
callback GetSubscriptionCallback
Returns
TypeDescription
void

getMetadata(gaxOpts)

getMetadata(gaxOpts?: CallOptions): Promise<GetSubscriptionMetadataResponse>;
Parameter
NameDescription
gaxOpts CallOptions
Returns
TypeDescription
Promise<GetSubscriptionMetadataResponse>

getMetadata(callback)

getMetadata(callback: GetSubscriptionMetadataCallback): void;
Parameter
NameDescription
callback GetSubscriptionMetadataCallback
Returns
TypeDescription
void

getMetadata(gaxOpts, callback)

getMetadata(gaxOpts: CallOptions, callback: GetSubscriptionMetadataCallback): void;
Parameters
NameDescription
gaxOpts CallOptions
callback GetSubscriptionMetadataCallback
Returns
TypeDescription
void

modifyPushConfig(config, gaxOpts)

modifyPushConfig(config: PushConfig, gaxOpts?: CallOptions): Promise<EmptyResponse>;
Parameters
NameDescription
config PushConfig
gaxOpts CallOptions
Returns
TypeDescription
Promise<EmptyResponse>

modifyPushConfig(config, callback)

modifyPushConfig(config: PushConfig, callback: EmptyCallback): void;
Parameters
NameDescription
config PushConfig
callback EmptyCallback
Returns
TypeDescription
void

modifyPushConfig(config, gaxOpts, callback)

modifyPushConfig(config: PushConfig, gaxOpts: CallOptions, callback: EmptyCallback): void;
Parameters
NameDescription
config PushConfig
gaxOpts CallOptions
callback EmptyCallback
Returns
TypeDescription
void

open()

open(): void;

Opens the Subscription to receive messages. In general this method shouldn't need to be called, unless you wish to receive messages after calling . Alternatively one could just assign a new message event listener which will also re-open the Subscription.

Returns
TypeDescription
void

seek(snapshot, gaxOpts)

seek(snapshot: string | Date, gaxOpts?: CallOptions): Promise<SeekResponse>;
Parameters
NameDescription
snapshot string | Date
gaxOpts CallOptions
Returns
TypeDescription
Promise<SeekResponse>

seek(snapshot, callback)

seek(snapshot: string | Date, callback: SeekCallback): void;
Parameters
NameDescription
snapshot string | Date
callback SeekCallback
Returns
TypeDescription
void

seek(snapshot, gaxOpts, callback)

seek(snapshot: string | Date, gaxOpts: CallOptions, callback: SeekCallback): void;
Parameters
NameDescription
snapshot string | Date
gaxOpts CallOptions
callback SeekCallback
Returns
TypeDescription
void

setMetadata(metadata, gaxOpts)

setMetadata(metadata: SubscriptionMetadata, gaxOpts?: CallOptions): Promise<SetSubscriptionMetadataResponse>;
Parameters
NameDescription
metadata SubscriptionMetadata
gaxOpts CallOptions
Returns
TypeDescription
Promise<SetSubscriptionMetadataResponse>

setMetadata(metadata, callback)

setMetadata(metadata: SubscriptionMetadata, callback: SetSubscriptionMetadataCallback): void;
Parameters
NameDescription
metadata SubscriptionMetadata
callback SetSubscriptionMetadataCallback
Returns
TypeDescription
void

setMetadata(metadata, gaxOpts, callback)

setMetadata(metadata: SubscriptionMetadata, gaxOpts: CallOptions, callback: SetSubscriptionMetadataCallback): void;
Parameters
NameDescription
metadata SubscriptionMetadata
gaxOpts CallOptions
callback SetSubscriptionMetadataCallback
Returns
TypeDescription
void

setOptions(options)

setOptions(options: SubscriberOptions): void;

Sets the Subscription options.

Parameter
NameDescription
options SubscriberOptions

The options.

Returns
TypeDescription
void

snapshot(name)

snapshot(name: string): Snapshot;

Create a Snapshot object. See to create a snapshot.

Parameter
NameDescription
name string

The name of the snapshot.

Returns
TypeDescription
Snapshot

{Snapshot}