Publish API for JavaScript SDK

The PubNub JavaScript SDK sends a message to one channel per call with publish() and a small payload that PubNub never stores with signal().

Both calls require the publish key of your keyset, which you pass as publishKey when you create the client. See Initialization.

Each method returns a promise that resolves with the response and rejects with a PubNubError. The error carries a status object with category and statusCode, and status.errorData holds the details that PubNub returned for a failed request. Each method also takes a callback as the second argument, and with a callback the method returns nothing and PubNub calls it with (status, response). On success, status.error is false and status.operation names the operation, and on failure status.error is true and response is null. All examples use async/await, so wrap a call in try...catch to receive the error status.

For delivery guarantees, ordering, and the difference between messages and signals, see Publishing messages with PubNub. For payload shapes and metadata in practice, see Send different message types. To receive what you publish, see Subscribe. To fix a failed publish, see Troubleshooting.

Publish​

publish() sends a message to one channel through a PubNub client for JavaScript and reports the timetoken that PubNub assigned to the message. PubNub delivers the message to the clients that subscribe to the channel.

Publish-processing latency, the time PubNub takes to accept and acknowledge a publish request, is about 0.5 ms within the same region.

#

#

#

#

#

#

required
message required#
Type: any
Default: no default
The payload to publish. Any JSON-serializable value, such as an object, array, string, or number. The SDK rejects a falsy value (0, false, "", null) with Missing 'message'.
channel required#
Type: string
Default: no default
The ID of the one channel to publish to. The method takes the ID string, not an SDK channel entity. The SDK rejects an empty value with Missing 'channel'.
storeInHistory#
Type: boolean
Default: no default
Whether PubNub stores the message in Message Persistence. true stores it and false skips storage. PubNub stores the message only when Message Persistence is enabled for the keyset. When omitted, the Message Persistence setting of the keyset applies.
meta#
Type: object or array
Default: no default
Metadata that PubNub attaches to the message. Subscribers can use it in message filters.
sendByPost#
Type: boolean
Default: false
Whether the message travels in the body of an HTTP POST request. false puts the message in the URL path of an HTTP GET request. The SDK compresses the body with deflate on Node.js and in browsers that provide CompressionStream.
ttl#
Type: number
Default: no default
The number of hours that Message Persistence keeps the message. When omitted, the retention of the keyset applies. PubNub ignores this value when it does not store the message. A value of 0 stores the message with no expiry. A keyset using Unlimited retention ignores any per-message TTL and stores the message indefinitely.
customMessageType#
Type: string
Default: no default
A business-specific label for the message, for example text, action, or poll. The accepted format is in the Custom message type note for this method.

Returns​

publish() resolves with a PublishResponse. The timetoken is a string, not a number. PubNub assigns every published message a server-side timetoken: a monotonically increasing 17-digit value precise to 100 nanoseconds (10⁻⁷ s), the number of 100-nanosecond intervals since the Unix epoch. For how to use it, see Publish response and the timetoken.

Publish errors​

publish() and signal() reject with a PubNubError. Read error.status.category to tell the cases apart. Validation errors happen before the SDK sends any request.

ConditionResult
message is 0, false, "", null, or omittedPNValidationErrorCategory. The status.message value is Missing 'message'.
channel is omitted or emptyPNValidationErrorCategory. The status.message value is Missing 'channel'.
publishKey was not set at initializationPNValidationErrorCategory. The status.message value is Missing 'publishKey'.
customMessageType is not in the accepted formatThe SDK does not check the format, so PubNub rejects the request. Read status.category and status.errorData for the reason.
The token or authKey has no write permission on the channelPNAccessDeniedCategory with HTTP 403. See Access Manager.
The network is unreachablePNNetworkIssuesCategory with status.statusCode 0.
message contains a circular referenceNo PubNubError. The call rejects with a plain TypeError, error.status is undefined, and a callback is not called.

Syntax

1pubnub.publish({
2 message: any,
3 channel: string,
4 meta?: object | Array<any>,
5 storeInHistory?: boolean,
6 sendByPost?: boolean,
7 ttl?: number,
8 customMessageType?: string
9}): Promise<PublishResponse>;

Signal​

signal() sends a signal to the clients that subscribe to one channel, using a PubNub client for JavaScript.

PubNub never stores signals in Message Persistence, regardless of keyset settings. Signal payloads are limited to 64 bytes. A signal cannot trigger mobile push notifications. A signal does not support meta, storeInHistory, ttl, or sendByPost. The SDK does not encrypt the message of a signal, even when a cryptoModule is configured.

PubNub rejects a payload over the limit. Read status.category and status.errorData for the reason.

signal() fails validation on the same inputs as publish(): a missing or falsy message, a missing channel, or a missing publishKey. See Publish errors.

required
message required#
Type: any
Default: no default
The payload to send. Any JSON-serializable value. The SDK rejects a falsy value with Missing 'message'.
channel required#
Type: string
Default: no default
The ID of the one channel to send the signal to. The method takes the ID string, not an SDK channel entity. The SDK rejects an empty value with Missing 'channel'.
customMessageType#
Type: string
Default: no default
A business-specific label for the signal, for example text, action, or poll. The accepted format is the same as for publish(). See the Custom message type note under Publish.

Returns​

signal() resolves with a SignalResponse. The timetoken is a string.

Syntax

1pubnub.signal({
2 message: any,
3 channel: string,
4 customMessageType?: string
5}): Promise<SignalResponse>;

Deprecated publish methods​

fire() is a deprecated method of the PubNub JavaScript SDK that sends a message to Functions event handlers and Illuminate, and publish() replaces it.

The reference for apps that still call fire() is Publish API: deprecated fire() method for JavaScript SDK. New apps use publish() with storeInHistory: false and replicate: false.

Was this page useful?

Last updated on