---
source_url: https://www.pubnub.com/docs/sdks/twisted/api-reference/publish-and-subscribe
title: Publish/Subscribe API for Python-Twisted SDK
updated_at: 2026-06-19T11:38:51.414Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Publish/Subscribe API for Python-Twisted SDK

PubNub delivers messages worldwide in less than 30 ms. Send a message to one recipient or broadcast to thousands of subscribers.

For higher-level conceptual details on publishing and subscribing, refer to [Connection Management](https://www.pubnub.com/docs/general/setup/connection-management) and to [Publish Messages](https://www.pubnub.com/docs/general/messages/publish).

:::note Deprecated
**NOTICE:** Based on current web trends and our own usage data, PubNub's Python Twisted SDK is **deprecated** as of May 1, 2019. Deprecation means we will no longer be updating the Python Twisted SDK but will continue to support users currently using it. Please feel free to use our other Python SDK offerings as they will continue to be supported and maintained. If you would like to use the Python Twisted SDK specifically, we would love to work with you on keeping this project alive!
:::

## Publish

The `publish()` function sends a message to all channel subscribers. PubNub replicates the message and delivers it to all subscribed clients on that channel. To publish a message you must first specify a valid `publish_key` at initialization.

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

:::warning Important
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-Twisted classes or functions as these will not serialize. String content can include any single-byte or multi-byte UTF-8 character.

:::warning 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

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

See [Message size limits](https://support.pubnub.com/hc/en-us/articles/360051495932-Calculating-Message-Payload-Size-Before-Publish).

:::tip Need larger messages?
Our platform is optimized for payloads up to 32 KiB. PubNub supports larger messages, but increasing the limit requires a verification of compatibility with your use case.
Talk to [our team](https://www.pubnub.com/company/contact-sales/) to discuss increasing the message size limit for your use case.
:::

##### 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 (e.g. `[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 e.g. 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-Twisted SDK:

```python
pubnub.publish().channel(String).message(Object).should_store(Boolean).meta(dict).use_post(Boolean)
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| channel | String | Yes |  | Destination of `message`. |
| message | Object | Yes |  | The payload. |
| should_store | Boolean | Optional | `account default` | Store in history |
| meta | Object | Optional | `None` | Meta data object which can be used with the filtering ability. |

### Sample code

#### Publish a message to a channel

```python
def callback(envelope):
    print("publish timetoken: %d" % envelope.result.timetoken)

d = pubnub.publish().channel("my_channel").message({
    'name': 'Alex',
    'online': True
}).future()
d.addCallback(callback)
```

:::note
Before running the above publish example, either using the [Debug Console](https://www.pubnub.com/docs/console/) or in a separate script running in a separate terminal window, [subscribe to the same channel](#subscribe) that is being published to.
:::

### Returns

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

| Field | Type | Description |
| --- | --- | --- |
| timetoken | `Int` | an `int` representation of the timetoken when the message was published |

### Other examples

#### Publish with metadata

```python
d = pubnub.publish()\
    .channel("my_channel")\
    .message(["hello", "there"])\
    .meta({'name': 'Alex'})\
    .deferred()
d.addCallback(callback)

# handle publish result, status always present, result if successful
# envelope.status.is_error() to see if error happened
```

## Subscribe

This function creates an open TCP socket to PubNub and begins listening for messages and events on a specified channel. To subscribe, 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](https://www.pubnub.com/docs/sdks/twisted/api-reference/configuration#initialization) the client.

:::warning
**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-Twisted SDK:

```python
pubnub.subscribe().channels(String|List|Tuple).channel_groups(String|List|Tuple).with_timetoken(Int).with_presence().execute()
```

| Parameter | Description |
| --- | --- |
| `channels`Type: String | List | Tuple | Subscribe to `channels`, Either `channel` or `channel_group` is required. |
| `channel_groups`Type: String | List | Tuple | Subscribe to `channel_groups`, Either `channel` or `channel_group` is required. |
| `timetoken`Type: Int | Pass a `timetoken`. |
| `with_presence`Type: Command | Also subscribe to related presence information. |

### Sample code

Subscribe to a channel:

```python
pubnub.subscribe().channels('my_channel').execute()
```

:::note
The response of the call is handled by adding a Listener. Please see the [Listeners section](#listeners) for more details. Listeners should be added before calling the method.
:::

### Returns

:::note
`PNMessageResult` is returned in the [Listeners](#listeners)
:::

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

| Field | Type | Description |
| --- | --- | --- |
| `category` | PNStatusCategory | See the [Python SDK listener categories](https://www.pubnub.com/docs/sdks/python/status-events). |
| `is_error` | Boolean | This is `true` if an error occurred in the execution of the operation. |
| `error_data` | PNErrorData | Error data of the exception (if `Error` is `true`). |
| `status_code` | int | Status code of the execution. |

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

| Field | Type | Description |
| --- | --- | --- |
| `message` | Object | The `message` sent on `channel`. |
| `subscription` | String | The channel group or wildcard `subscription` match (if exists). |
| `channel` | String | The `channel` for which the message belongs. |
| `timetoken` | Int | `Timetoken` for the message. |
| `user_metadata` | Dict | User `metadata`. |

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

| Field | Type | Description |
| --- | --- | --- |
| `event` | String | Events like `join`, `leave`, `timeout`, `state-change`. |
| `uuid` | String | `uuid` for event. |
| `timestamp` | Int | `timestamp` for event. |
| `occupancy` | Int | Current `occupancy`. |
| `subscription` | String | The channel group or wildcard `subscription` match (if exists). |
| `channel` | String | The `channel` for which the message belongs. |
| `timetoken` | Int | `timetoken` of the message. |
| `user_metadata` | Dict | User `metadata`. |

### Other examples

#### Basic subscribe with logging

```python
import logging
import pubnub

from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub_twisted import PubNubTwisted as PubNub
from pubnub.pubnub_twisted import SubscribeListener

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

pnconfig = PNConfiguration()

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

pubnub = PubNub(pnconfig)

pubnub.add_listener(SubscribeListener())
pubnub.subscribe().channels("my_channel").execute()
```

#### Subscribing to multiple channels

It's possible to subscribe to more than one channel using the [Multiplexing](https://www.pubnub.com/docs/general/channels/subscribe#channel-multiplexing) feature. The example shows how to do that using an array to specify the channel names.

:::note Alternative subscription methods
You can also use [Wildcard Subscribe](https://www.pubnub.com/docs/general/channels/subscribe#wildcard-subscribe) and [Channel Groups](https://www.pubnub.com/docs/general/channels/subscribe#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](https://admin.pubnub.com).
:::

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

#### Subscribing to a Presence channel

:::warning Requires Presence
This method requires that the Presence add-on is [enabled](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) for your key in the [Admin Portal](https://admin.pubnub.com/).
:::

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`.

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

#### Sample Responses

##### Join event

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

##### Leave event

```json
{
    "action" : "leave",
    "timestamp" : 1345549797,
    "uuid" : "175c2c67-b2a9-470d-8f4b-1db94f90e39e",
    "occupancy" : 1
}
```

#### Timeout event

```python
if envelope.event == 'timeout':
    envelope.uuid # 175c2c67-b2a9-470d-8f4b-1db94f90e39e
    envelope.timestamp # 1345546797
    envelope.occupancy # 0
```

#### State change event

```python
if envelope.event == 'state-change':
    envelope.uuid # 76c2c571-9a2b-d074-b4f8-e93e09f49bd
    envelope.timestamp # 1345546797
    envelope.user_metadata # {'is_typing': True}
```

#### Interval event

```json
{
    "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:

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

If the full interval message is greater than `30 KB` (since the max publish payload is `∼32 KiB`), 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.

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

#### Wildcard subscribe to channels

:::note Requires Stream Controller add-on
This method requires that the *Stream Controller* add-on is enabled for your key in the [Admin Portal](https://admin.pubnub.com/) (with Enable Wildcard Subscribe checked). Read the [support page](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) 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 (.)`.

```python
pubnub.subscribe().channels("foo.*").execute();
```

:::note 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

:::warning Requires Presence
This method requires that the Presence add-on is [enabled](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) for your key in the [Admin Portal](https://admin.pubnub.com/).
:::

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

```python
from pubnub.pubnub_tornado import PubNubTornado
from pubnub.pubnub_tornado import SubscribeListener

class MySubscribeListener(SubscribeListener):
    def __init__(self):
        pass

    def status(self, pubnub, status):
        if status.category == PNStatusCategory.PNConnectedCategory:
            state = {
                'field_a': 'awesome',
                'field_b': 10
            }
            d = pubnub.set_state().channels('awesome_channel').\
                channel_groups('awesome_channel_groups').state(state).deferred()

        else:
            pass

    def message(self, pubnub, message):
        pass

    def presence(self, pubnub, presence):
        pass

pubnub = PubNubTwisted(pnconfig)

my_listener = MySubscribeListener()
pubnub.add_listener(my_listener)

pubnub.subscribe().channels("my_channel").execute()
```

#### Subscribe to a channel group

:::note Requires Stream Controller add-on
This method requires that the *Stream Controller* add-on is enabled for your key in the [Admin Portal](https://admin.pubnub.com/). Read the [support page](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) on enabling add-on features on your keys.
:::

```python
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](https://admin.pubnub.com/). Read the [support page](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) on enabling add-on features on your keys.
:::

```python
pubnub.subscribe().channel_groups(["cg1", "cg2"]).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.

:::warning
**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-Twisted SDK:

```python
pubnub.unsubscribe().channels(String|List|Tuple).channel_groups(String|List|Tuple).execute()
```

| Parameter | Description |
| --- | --- |
| `channels`Type: String | List | Tuple | Subscribe to `channels`, Either `channel` or `channel_group` is required. |
| `channel_groups`Type: String | List | Tuple | Subscribe to `channel_groups`, Either `channel` or `channel_group` is required |

### Sample code

Unsubscribe from a channel:

```python
pubnub.unsubscribe().channels("my_channel").execute()
```

:::note
The response of the call is handled by adding a Listener. Please see the [Listeners section](#listeners) for more details. Listeners should be added before calling the method.
:::

### Response

The output below demonstrates the response to a successful call:

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

### Other examples

#### Unsubscribing from multiple channels

:::note Requires Stream Controller add-on
This method requires that the *Stream Controller* add-on is enabled for your key in the [Admin Portal](https://admin.pubnub.com/). Read the [support page](https://support.pubnub.com/hc/en-us/articles/360051974791-How-do-I-enable-add-on-features-for-my-keys-) on enabling add-on features on your keys.
:::

```python
pubnub.unsubscribe().channels(["my_channel1", "my_channel2"]).execute();
```

##### Example response

```json
{
    "action" : "leave"
}
```

#### Unsubscribe from a channel group

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

##### Example response

```javascript
{
    "action": "leave"
}
```

## Unsubscribe all

Unsubscribe from all channels and all channel groups

### Method(s)

```python
pubnub.unsubscribe_all()
```

### Sample code

```python
pubnub.unsubscribe_all();
```

### Returns

None

## Listeners

You can be notified of connectivity status, message and presence notifications via the listeners.

Listeners should be added before calling the method.

#### Add listeners

```python
from pubnub.callbacks import SubscribeCallback

class MySubscribeCallback(SubscribeCallback):
    def status(self, pubnub, status):
        pass
        # The status object returned is always related to subscribe but could contain
        # information about subscribe, heartbeat, or errors
        # use the operationType to switch on different options
        if status.operation == PNOperationType.PNSubscribeOperation \
                or status.operation == PNOperationType.PNUnsubscribeOperation:
            if status.category == PNStatusCategory.PNConnectedCategory:
                pass
                # This is expected for a subscribe, this means there is no error or issue whatsoever
            elif status.category == PNStatusCategory.PNReconnectedCategory:
                pass
                # This usually occurs if subscribe temporarily fails but reconnects. This means
                # there was an error but there is no longer any issue
            elif status.category == PNStatusCategory.PNDisconnectedCategory:
                pass
                # This is the expected category for an unsubscribe. This means there
                # was no error in unsubscribing from everything
            elif status.category == PNStatusCategory.PNUnexpectedDisconnectCategory:
                pass
                # This is usually an issue with the internet connection, this is an error, handle
                # appropriately retry will be called automatically
            elif status.category == PNStatusCategory.PNAccessDeniedCategory:
                pass
                # This means that Access Manager does not allow this client to subscribe to this
                # channel and channel group configuration. This is another explicit error
            else:
                pass
                # This is usually an issue with the internet connection, this is an error, handle appropriately
                # retry will be called automatically
        elif status.operation == PNOperationType.PNSubscribeOperation:
            # Heartbeat operations can in fact have errors, so it is important to check first for an error.
            # For more information on how to configure heartbeat notifications through the status
            # PNObjectEventListener callback, consult http://www.pubnub.com/docs/sdks/twisted/api-reference/configuration#configuration
            if status.is_error():
                pass
                # There was an error with the heartbeat operation, handle here
            else:
                pass
                # Heartbeat operation was successful
        else:
            pass
            # Encountered unknown status type

    def presence(self, pubnub, presence):
        pass  # handle incoming presence data

    def message(self, pubnub, message):
        pass  # handle incoming messages

pubnub.add_listener(MySubscribeCallback())
```

#### Remove listeners

```python
my_listener = MySubscribeCallback()

pubnub.add_listener(my_listener)

# some time later
pubnub.remove_listener(my_listener)
```

#### Handling disconnects

```python
from pubnub.callbacks import SubscribeCallback
from pubnub.enums import PNStatusCategory

class HandleDisconnectsCallback(SubscribeCallback):
    def status(self, pubnub, status):
        if status.category == PNStatusCategory.PNUnexpectedDisconnectCategory:
            # internet got lost, do some magic and call reconnect when ready
            pubnub.reconnect()
        elif status.category == PNStatusCategory.PNTimeoutCategory:
            # do some magic and call reconnect when ready
            pubnub.reconnect()
        else:
            logger.debug(status)

    def presence(self, pubnub, presence):
        pass

    def message(self, pubnub, message):
        pass

    def signal(self, pubnub, signal):
        pass

disconnect_listener = HandleDisconnectsCallback()

pubnub.add_listener(disconnect_listener)
```

#### Listener status events

| Category | Description |
| --- | --- |
| `PNTimeoutCategory` | Failure to establish a connection to PubNub due to a timeout. |
| `PNBadRequestCategory` | The server responded with a bad response error because the request is malformed. |
| `PNNetworkIssuesCategory` | A subscribe event experienced an exception when running. The SDK isn't able to reach PubNub servers. This may be due to many reasons, such as: the machine or device isn't connected to the internet; the internet connection has been lost; your internet service provider is having trouble; or, perhaps the SDK is behind a proxy. |
| `PNReconnectedCategory` | The SDK was able to reconnect to PubNub. |
| `PNConnectedCategory` | SDK subscribed with a new mix of channels. This is fired every time the `channel` or `channel group` mix changes. |
| `PNUnexpectedDisconnectCategory` | Previously started subscribe loop did fail and at this moment client disconnected from real-time data channels. |
| `PNUnknownCategory` | Returned when the subscriber gets a non-200 HTTP response code from the server. |

:::note
`SubscribeListener` should not be used with high-performance sections of your app.
:::

## Terms in this document

* **Channel** - A pathway for sending and receiving messages between devices, created automatically when you first use it, that can handle any number of users and messages for different communication needs, like 1-1 text chats, group conversations, and other data streaming.
* **Channel pattern** - A way to group and analyze channel data to track performance metrics like message counts and user engagement over time with PubNub Insights.
