Publish/Subscribe API for PubNub Python SDK

The foundation of the PubNub service is the ability to send a message and have it delivered anywhere in less than 100ms. Send a message to just one other person, or broadcast to thousands of subscribers at once.

For higher-level conceptual details on publishing and subscribing, refer to Connection Management and to Publish Messages.

Publish

The publish() function is used to send a message to all subscribers of a channel. To publish a message you must first specify a valid publish_key at initialization. A successfully published message is replicated across the PubNub Real-Time Network and sent simultaneously to all subscribed clients on a channel.

Messages in transit can be secured from potential eavesdroppers with SSL/TLS by setting ssl to True during initialization.

ObjectNode

The new Jackson parser does not recognize JSONObject. Please use ObjectNode instead.

Publish Anytime

It's not required to be subscribed to a channel in order to publish to that channel.

Message Data

The message argument can contain any JSON serializable data, including: Objects, Arrays, Ints and Strings. data should not contain special Python classes or functions as these will not serialize. String content can include any single-byte or multi-byte UTF-8 character.

Don't JSON serialize

It is important to note that you should not JSON serialize when sending signals/messages via PUBNUB. Why? Because the serialization is done for you automatically. Instead just pass the full object as the message payload. PubNub takes care of everything for you.

Message Size

The maximum number of characters per message is 32 KiB by default. The maximum message size is based on the final escaped character count, including the channel name. An ideal message size is under 1800 bytes which allows a message to be compressed and sent using single IP datagram (1.5 KiB) providing optimal network performance.

If the message you publish exceeds the configured size, you will receive the following message:

Message Too Large Error

["PUBLISHED",[0,"Message Too Large","13524237335750949"]]

For further details, check Calculating Message Payload Size Before Publish.

Message Publish Rate

Messages can be published as fast as bandwidth conditions will allow. There is a soft limit based on max throughput since messages will be discarded if the subscriber can't keep pace with the publisher.

For example, if 200 messages are published simultaneously before a subscriber has had a chance to receive any messages, the subscriber may not receive the first 100 messages because the message queue has a limit of only 100 messages stored in memory.

Publishing to Multiple Channels

It is not possible to publish a message to multiple channels simultaneously. The message must be published to one channel at a time.

Publishing Messages Reliably

There are some best practices to ensure messages are delivered when publishing to a channel:

  • Publish to any given channel in a serial manner (not concurrently).
  • Check that the return code is success (for example, [1,"Sent","136074940..."])
  • Publish the next message only after receiving a success return code.
  • If a failure code is returned ([0,"blah","<timetoken>"]), retry the publish.
  • Avoid exceeding the in-memory queue's capacity of 100 messages. An overflow situation (aka missed messages) can occur if slow subscribers fail to keep up with the publish pace in a given period of time.
  • Throttle publish bursts in accordance with your app's latency needs, for example, Publish no faster than 5 msgs per second to any one channel.

Method(s)

To Publish a message you can use the following method(s) in the Python SDK:

pubnub.publish() \
.channel(String) \
.message(Object) \
.should_store(Boolean) \
.meta(Dictionary) \
.use_post(Boolean)
ParameterTypeRequiredDefaultDescription
channelStringYesDestination of message.
messageObjectYesThe payload.
should_storeBooleanOptionalaccount defaultStore in history
metaObjectOptionalNoneMeta data object which can be used with the filtering ability.

Basic Usage

Publish a message to a channel

from pubnub.exceptions import PubNubException
try:
envelope = pubnub.publish().channel("my_channel").message({
'name': 'Alex',
'online': True
}).sync()
print("publish timetoken: %d" % envelope.result.timetoken)
except PubNubException as e:
handle_exception(e)
Subscribe to the channel

Before running the above publish example, either using the Debug Console or in a separate script running in a separate terminal window, subscribe to the same channel that is being published to.

Returns

The publish() operation returns a PNPublishResult which contains the following fields:

FieldTypeDescription
timetokenIntan int representation of the timetoken when the message was published

Other Examples

Publish with metadata

def publish_callback(result, status):
pass
# handle publish result, status always present, result if successful
# status.isError to see if error happened

pubnub.publish().channel("my_channel").message(["hello", "there"]) \
.meta({'name': 'Alex'}).pn_async(publish_callback)

Fire

The fire endpoint allows the client to send a message to Functions Event Handlers. These messages will go directly to any Event Handlers registered on the channel that you fire to and will trigger their execution. The content of the fired request will be available for processing within the Event Handler. The message sent via fire() isn't replicated, and so won't be received by any subscribers to the channel. The message is also not stored in history.

Method(s)

To Fire a message you can use the following method(s) in the Python SDK:

pubnub.fire() \
.channel(String) \
.message(Object) \
.use_post(Boolean) \
.meta(Object)
ParameterTypeRequiredDefaultDescription
channelStringYesDestination of message.
messageObjectYesThe payload.
use_postBooleanOptionalFalseUse POST to publish.
metaObjectOptionalNoneMeta data object which can be used with the filtering ability.

Basic Usage

Fire a message to a channel

envelope = pubnub.fire() \
.channel('my_channel') \
.message('hello there') \
.use_post(True) \
.sync()
print('fire timetoken: %d' % envelope.result.timetoken)

Signal

The signal() function is used to send a signal to all subscribers of a channel.

By default, signals are limited to a message payload size of 64 bytes. This limit applies only to the payload, and not to the URI or headers. If you require a larger payload size, please contact support.

Method(s)

To Signal a message you can use the following method(s) in the Python SDK:

pubnub.signal()
.message(Object)
.channel(String)
ParameterTypeRequiredDescription
messageObjectYesThe payload.
channelStringYesDestination of message.

Basic Usage

Signal a message to a channel

envelope = pubnub.signal() \
.channel('some_channel') \
.message('foo') \
.sync()

Response

FieldTypeDescription
timetokenintAn int representation of the timetoken when Signal was sent.

Subscribe

Receive messages

Your app receives messages and events via event listeners. The event listener is a single point through which your app receives all the messages, signals, and events that are sent in any channel you are subscribed to.

For more information about adding a listener, refer to the Event Listeners section.

Description

This function causes the client to create an open TCP socket to the PubNub Real-Time Network and begin listening for messages on a specified channel. To subscribe to a channel the client must send the appropriate subscribe_key at initialization.

By default a newly subscribed client will only receive messages published to the channel after the subscribe() call completes. If a client gets disconnected from a channel, it can automatically attempt to reconnect to that channel and retrieve any available messages that were missed during that period. This can be achieved by setting setReconnectionPolicy to PNReconnectionPolicy.LINEAR, when initializing the client.

Unsubscribing from all channels

Unsubscribing from all channels, and then subscribing to a new channel Y is not the same as subscribing to channel Y and then unsubscribing from the previously-subscribed channel(s). Unsubscribing from all channels resets the last-received timetoken and thus, there could be some gaps in the subscription that may lead to message loss.

Method(s)

To Subscribe to a channel you can use the following method(s) in the Python SDK:

pubnub.subscribe() \
.channels(String|List|Tuple) \
.channel_groups(String|List|Tuple) \
.with_timetoken(Int) \
.with_presence() \
.execute()
ParameterTypeRequiredDescription
channelsString | List | TupleOptionalSubscribe to channels, Either channel or channel_group is required.
channel_groupsString | List | TupleOptionalSubscribe to channel_groups, Either channel or channel_group is required.
timetokenIntOptionalPass a timetoken.
with_presenceCommandOptionalAlso subscribe to related presence information.

Basic Usage

Subscribe to a channel:

pubnub.subscribe() \
.channels('my_channel') \
.execute()
Event listeners

The response of the call is handled by adding a Listener. Please see the Listeners section for more details. Listeners should be added before calling the method.

Returns

PNMessageResult

PNMessageResult is returned in the Listeners

The subscribe() operation returns a PNStatus for messages which contains the following fields:

FieldTypeDescription
categoryPNStatusCategoryDetails of StatusCategory are here
is_errorBooleanThis is True if an error occurred in the execution of the operation.
error_dataPNErrorDataError data of the exception (if Error is True).
status_codeintStatus code of the execution.

The subscribe() operation returns a PNMessageResult for messages which contains the following fields:

FieldTypeDescription
messageObjectThe message sent on channel.
subscriptionStringThe channel group or wildcard subscription match (if exists).
channelStringThe channel for which the message belongs.
timetokenIntTimetoken for the message.
user_metadataDictionaryUser metadata.

The subscribe() operation returns a PNPresenceEventResult from presence which contains the following operations:

FieldTypeDescription
eventStringEvents like join, leave, timeout, state-change.
uuidStringuuid for event.
timestampInttimestamp for event.
occupancyIntCurrent occupancy.
subscriptionStringThe channel group or wildcard subscription match (if exists).
channelStringThe channel for which the message belongs.
timetokenInttimetoken of the message.
user_metadataDictionaryUser metadata.

The subscribe() operation returns a PNSignalResult for signals which contains the following operations:

FieldTypeDescription
timetokenIntAn int representation of the timetoken when Signal was sent.
channelStringThe channel on which Signal occurred.
publisherStringID of the sender.
messageObjectThe payload.

Other Examples

Basic subscribe with logging

import logging
import pubnub

from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub, SubscribeListener

pubnub.set_stream_logger('pubnub', logging.DEBUG)

pnconfig = PNConfiguration()

pnconfig.subscribe_key = 'demo'
pnconfig.publish_key = 'demo'

pubnub = PubNub(pnconfig)

show all 17 lines

Subscribing to multiple channels

It's possible to subscribe to more than one channel using the Multiplexing feature. The example shows how to do that using an array to specify the channel names.

Alternative subscription methods

You can also use Wildcard Subscribe and Channel Groups to subscribe to multiple channels at a time. To use these features, the Stream Controller add-on must be enabled on your keyset in the Admin Portal.

pubnub.subscribe() \
.channels(["my_channel1", "my_channel2"]) \
.execute()

Subscribing to a Presence channel

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

For any given channel there is an associated Presence channel. You can subscribe directly to the channel by appending -pnpres to the channel name. For example the channel named my_channel would have the presence channel named my_channel-pnpres.

pubnub.subscribe()\
.channels("my_channel")\
.with_presence()\
.execute()

Sample Responses

Join Event
if envelope.event == 'join':
envelope.uuid # 175c2c67-b2a9-470d-8f4b-1db94f90e39e
envelope.timestamp # 1345546797
envelope.occupancy # 2
Leave Event
{
"action" : "leave",
"timestamp" : 1345549797,
"uuid" : "175c2c67-b2a9-470d-8f4b-1db94f90e39e",
"occupancy" : 1
}
Timeout Event
if envelope.event == 'timeout':
envelope.uuid # 175c2c67-b2a9-470d-8f4b-1db94f90e39e
envelope.timestamp # 1345546797
envelope.occupancy # 0
Custom Presence Event (State Change)
if envelope.event == 'state-change':
envelope.uuid # 76c2c571-9a2b-d074-b4f8-e93e09f49bd
envelope.timestamp # 1345546797
envelope.user_metadata # {'is_typing': True}

Interval Event

{
"action":"interval",
"timestamp":1474396578,
"occupancy":2
}

When a channel is in interval mode with presence_deltas pnconfig flag enabled, the interval message may also include the following fields which contain an array of changed UUIDs since the last interval message.

  • joined
  • left
  • timedout

For example, this interval message indicates there were 2 new UUIDs that joined and 1 timed out UUID since the last interval:

{
"action" : "interval",
"occupancy" : <# users in channel>,
"timestamp" : <unix timestamp>,
"joined" : ["uuid2", "uuid3"],
"timedout" : ["uuid1"]
}

If the full interval message is greater than 30KB (since the max publish payload is ∼32KB), none of the extra fields will be present. Instead there will be a here_now_refresh boolean field set to True. This indicates to the user that they should do a hereNow request to get the complete list of users present in the channel.

{
"action" : "interval",
"occupancy" : <# users in channel>,
"timestamp" : <unix timestamp>,
"here_now_refresh" : True
}

Wildcard subscribe to channels

Requires Stream Controller add-on

This method requires that the Stream Controller add-on is enabled for your key in the Admin Portal (with Enable Wildcard Subscribe checked). Read the support page on enabling add-on features on your keys.

Wildcard subscribes allow the client to subscribe to multiple channels using wildcard. For example, if you subscribe to a.* you will get all messages for a.b, a.c, a.x. The wildcarded * portion refers to any portion of the channel string name after the dot (.).

pubnub.subscribe() \
.channels("foo.*") \
.execute()
Wildcard grants and revokes

Only one level (a.*) of wildcarding is supported. If you grant on * or a.b.*, the grant will treat * or a.b.* as a single channel named either * or a.b.*. You can also revoke permissions from multiple channels using wildcards but only if you previously granted permissions using the same wildcards. Wildcard revokes, similarly to grants, only work one level deep, like a.*.

Subscribing with State

Requires Presence add-on

This method requires that the Presence add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

Required user_id

Always set the user_id to uniquely identify the user or device that connects to PubNub. This user_id should be persisted, and should remain unchanged for the lifetime of the user or the device. If you don't set the user_id, you won't be able to connect to PubNub.

from pubnub.enums import PNStatusCategory

from pubnub.pubnub import SubscribeListener, PubNub

class MySubscribeListener(SubscribeListener):
def __init__(self):
if status.category == PNStatusCategory.PNConnectedCategory:
state = {
'field_a': 'awesome',
'field_b': 10
}
envelope = pubnub.set_state().channels('my_channel').\
channel_groups('awesome_channel_groups').state(state).sync()

else:
show all 32 lines

Subscribe to a channel group

Requires Stream Controller add-on

This method requires that the Stream Controller add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

pubnub.subscribe() \
.channel_groups("awesome_channel_group") \
.execute()

Subscribe to the presence channel of a channel group

note
Requires Stream Controller and Presence add-ons

This method requires both the Stream Controller and Presence add-ons are enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

pubnub.subscribe() \
.channel_groups("awesome_channel_group") \
.with_presence() \
.execute()

Unsubscribe

When subscribed to a single channel, this function causes the client to issue a leave from the channel and close any open socket to the PubNub Network. For multiplexed channels, the specified channel(s) will be removed and the socket remains open until there are no more channels remaining in the list.

Unsubscribing from all channels

Unsubscribing from all channels, and then subscribing to a new channel Y is not the same as subscribing to channel Y and then unsubscribing from the previously-subscribed channel(s). Unsubscribing from all channels resets the last-received timetoken and thus, there could be some gaps in the subscription that may lead to message loss.

Method(s)

To Unsubscribe from a channel you can use the following method(s) in the Python SDK:

pubnub.unsubscribe() \
.channels(String|List|Tuple) \
.channel_groups(String|List|Tuple) \
.execute()
ParameterTypeRequiredDescription
channelsString | List | TupleOptionalSubscribe to channels, Either channel or channel_group is required.
channel_groupsString | List | TupleOptionalSubscribe to channel_groups, Either channel or channel_group is required

Basic Usage

Unsubscribe from a channel:

pubnub.unsubscribe() \
.channels("my_channel") \
.execute()
Event listeners

The response of the call is handled by adding a Listener. Please see the Listeners section for more details. Listeners should be added before calling the method.

Response

The output below demonstrates the response to a successful call:

if envelope.event == 'leave':
envelope.uuid # 175c2c67-b2a9-470d-8f4b-1db94f90e39e
envelope.timestamp # 1345546797
envelope.occupancy # 2

Other Examples

Unsubscribing from multiple channels

Requires Stream Controller add-on

This method requires that the Stream Controller add-on is enabled for your key in the Admin Portal. Read the support page on enabling add-on features on your keys.

pubnub.unsubscribe() \
.channels(["my_channel1", "my_channel2"]) \
.execute()
Example Response
{
"action" : "leave"
}

Unsubscribe from a channel group

pubnub.unsubscribe() \
.channels_groups(["my_group1", "my_group2") \
.execute()

Example Response:

{
"action": "leave"
}

Unsubscribe All

Unsubscribe from all channels and all channel groups

Method(s)

pubnub.unsubscribe_all()

Basic Usage

pubnub.unsubscribe_all()

Returns

None

Last updated on