Publish API for Python-Asyncio SDK

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

All three calls require the publish key of your keyset, which you pass as publish_key when you create the client. See Initialization.

A builder sends its request when you await its future(). A request that fails, or fails validation, does not raise an exception. future() resolves with a PubNubAsyncioException whose result is None and whose status holds the error. Call is_error() on the awaited value before you read result.

Each builder method has a keyword argument of the same name.

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 request, see Troubleshooting.

Publish​

publish() sends a message to one channel through a PubNub client for Python-Asyncio 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. A `None` value fails validation with the message Message missing. Other falsy values such as 0, False, and "" are valid.
channel required#
Type: str
Default: no default
The ID of the one channel to publish to. The method takes the ID string, not an SDK channel entity. An empty value fails validation with the message Channel missing.
should_store#
Type: bool
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: Any
Default: None
Metadata that PubNub attaches to the message. Subscribers can use it in message filters.
use_post#
Type: bool
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.
ttl#
Type: int
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. The SDK sends ttl only when the value is not 0. A keyset using Unlimited retention ignores any per-message TTL and stores the message indefinitely.
custom_message_type#
Type: str
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 an AsyncioEnvelope whose result is a PNPublishResult and whose status is a PNStatus.

PNPublishResult​

timetoken#
Type: int
The timetoken that PubNub assigned to the message. 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​

A failed publish() does not raise. The awaited value is a PubNubAsyncioException with result set to None. Call is_error() on it, and read the cause from its status.

The SDK checks the inputs before it sends any request, and a failed check is reported the same way.

ConditionResult
channel is omitted or emptyError status with the message Channel missing.
message is None or omittedError status with the message Message missing.
publish_key was not set at initializationError status with the message Publish key not configured.
subscribe_key was not set at initializationError status with the message Subscribe key not configured.
message or meta is not JSON-serializableError status with the message Trying to publish not JSON serializable object.
custom_message_type is not in the accepted formatThe SDK does not check the format, so PubNub rejects the request. Read the status for the reason.
The token or auth_key has no write permission on the channelError status for HTTP status 403, with category PNAccessDeniedCategory. See Access Manager.
The message exceeds the size limitError status for HTTP status 413 over POST or 414 over GET.
The network is unreachableError status with the message Connection Error.

Syntax

1await pubnub.publish(
2 message: Any,
3 channel: str,
4 should_store: bool,
5 meta: Any,
6 use_post: bool,
7 ttl: int,
8 custom_message_type: str
9).future() -> AsyncioEnvelope

Signal​

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

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, should_store, ttl, or use_post. The SDK does not encrypt the message of a signal, even when a crypto_module is configured.

PubNub rejects a payload over the limit. The error status carries the HTTP status code and the reason.

signal() fails validation when channel is missing or empty, or when publish_key or subscribe_key is missing. The SDK does not check message. See Publish errors.

required
message required#
Type: Any
Default: no default
The payload to send. Any JSON-serializable value.
channel required#
Type: str
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. An empty value fails validation with the message Channel missing.
custom_message_type#
Type: str
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(), described in the Custom message type note under Publish.

Returns​

signal() resolves with an AsyncioEnvelope whose result is a PNSignalResult and whose status is a PNStatus.

PNSignalResult​

timetoken#
Type: str
The timetoken that PubNub assigned to the signal.

Syntax

1await pubnub.signal(
2 message: Any,
3 channel: str,
4 custom_message_type: str
5).future() -> AsyncioEnvelope

Fire​

fire() sends a message to Functions event handlers and Illuminate through a PubNub client for Python-Asyncio, bypassing subscribers and storage.

The message goes straight to the handlers registered on the target channel and triggers them. Each handler can read the request body. PubNub does not replicate a fired message to subscribers and does not store it in Message Persistence.

required
message required#
Type: Any
Default: no default
The payload to publish. Any JSON-serializable value, such as an object, array, string, or number. A `None` value fails validation with the message Message missing.
channel required#
Type: str
Default: no default
The ID of the one channel to fire to. The method takes the ID string, not an SDK channel entity. An empty value fails validation with the message Channel missing.
meta#
Type: Any
Default: None
Metadata that PubNub attaches to the message. Subscribers can use it in message filters.
use_post#
Type: bool
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.

Returns​

fire() resolves with an AsyncioEnvelope whose result is a PNFireResult and whose status is a PNStatus.

PNFireResult​

timetoken#
Type: int
The timetoken that PubNub assigned to the fired message.

Fire errors​

fire() fails validation on the same inputs as publish(): a missing channel, a None message, or a missing publish_key or subscribe_key. See Publish errors.

Syntax

1await pubnub.fire(
2 message: Any,
3 channel: str,
4 meta: Any,
5 use_post: bool
6).future() -> AsyncioEnvelope

Was this page useful?

Last updated on