Publish API for Python SDK

The PubNub Python 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.

Each builder method has a keyword argument of the same name. A failed request raises a PubNubException from .sync(). With .pn_async(callback), the callback receives a status where status.is_error() returns True.

Request execution and return values

You can decide whether to perform the Python SDK operations synchronously or asynchronously.

  • .sync() returns an Envelope object, which has two fields: Envelope.result, whose type differs for each API, and Envelope.status of type PnStatus.

    1pubnub.publish() \
    2 .channel("myChannel") \
    3 .message("Hello from PubNub Python SDK") \
    4 .sync()
  • .pn_async(callback) returns None and passes the values of Envelope.result and Envelope.status to a callback you must define beforehand.

    1def my_callback_function(result, status):
    2 print(f'TT: {result.timetoken}, status: {status.category.name}')
    3
    4pubnub.publish() \
    5 .channel("myChannel") \
    6 .message("Hello from PubNub Python SDK") \
    7 .pn_async(my_callback_function)

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 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 None with 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. The SDK rejects an empty value with 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() returns an Envelope 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​

publish() raises a PubNubException when it fails, and the message of the exception names the cause.

Validation errors happen before the SDK sends any request.

ConditionResult
channel is omitted or emptyPubNubException with the message Channel missing.
message is None or omittedPubNubException with the message Message missing.
publish_key was not set at initializationPubNubException with the message Publish key not configured.
subscribe_key was not set at initializationPubNubException with the message Subscribe key not configured.
message or meta is not JSON-serializablePubNubException 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 exception message for the reason.
The token or auth_key has no write permission on the channelPubNubException for HTTP status 403, with category PNAccessDeniedCategory in the status. See Access Manager.
The message exceeds the size limitPubNubException for HTTP status 413 over POST or 414 over GET.
The network is unreachablePubNubException with the message Connection Error.

Syntax

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

Signal​

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

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 PubNubException 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. The SDK rejects an empty value with 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() returns an Envelope whose result is a PNSignalResult and whose status is a PNStatus.

PNSignalResult​

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

Syntax

1pubnub.signal() \
2 .message(message: Any) \
3 .channel(channel: str) \
4 .custom_message_type(custom_message_type: str) \
5 .sync() -> SignalResultEnvelope

Fire​

fire() sends a message to Functions event handlers and Illuminate through a PubNub client for Python, 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. The SDK rejects None with 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. The SDK rejects an empty value with 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() returns an Envelope 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

1pubnub.fire() \
2 .message(message: Any) \
3 .channel(channel: str) \
4 .meta(meta: Any) \
5 .use_post(use_post: bool) \
6 .sync() -> PNFireResultEnvelope

Was this page useful?

Last updated on