---
source_url: https://www.pubnub.com/docs/sdks/twisted/api-reference/configuration
title: Configuration API for Python-Twisted SDK
updated_at: 2026-05-29T11:12:26.337Z
---

> 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


# Configuration API for Python-Twisted SDK

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

Complete API reference for building real-time applications on PubNub with the Python-Twisted Software Development Kit (SDK). This page covers configuration, initialization, and event handling with concise, working examples.

## Configuration

`PNConfiguration` instance is storage for user-provided information which describe further PubNub client behavior. Configuration instance contain additional set of properties which allow to perform precise PubNub client configuration.

### Method(s)

To create `configuration` instance you can use the following function in the Python-Twisted SDK:

```python
pnconfig = PNConfiguration()
```

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| subscribe_key | String | Yes |  | `subscribe_key` from Admin Portal. |
| publish_key | String | Optional | `None` | `publish_key` from Admin Portal (only required if publishing). |
| secret_key | String | Optional | `None` | `secret_key` (only required for modifying/revealing access permissions) |
| cipher_key | String | Optional | `None` | If `cipher_key` is passed, all communications to/from PubNub will be encrypted. |
| uuid | String | Yes |  | `UUID` to use. You should set a unique `UUID` to identify the user or the device that connects to PubNub. If you don't set the `UUID`, you won't be able to connect to PubNub. |
| auth_key | String | Optional | `None` | If Access Manager is utilized, client will use this `authKey` in all restricted requests. |
| ssl | Boolean | Optional | `False` | Use `SSL`. |
| connect_timeout | Int | Optional | `5` | How long to wait before giving up connection to client. The value is in seconds. |
| subscribe_timeout | Int | Optional | `310` | How long to keep the `subscribe` loop running before disconnect. The value is in seconds. |
| non_subscribe_request_timeout | Int | Optional | `10` | On `non subscribe` operations, how long to wait for server response. The value is in seconds. |
| filter_expression | String | Optional | `None` | Feature to subscribe with a custom filter expression. |
| heartbeat_notification_options | PNHeartbeatNotificationOptions | Optional | `PNHeartbeatNotificationOptions.FAILURES` | `Heartbeat` notifications, by default, the SDK will alert on failed heartbeats (equivalent to: `PNHeartbeatNotificationOptions.FAILURES`). Other options such as all heartbeats (`PNHeartbeatNotificationOptions.ALL`) or no heartbeats (`PNHeartbeatNotificationOptions.NONE`) are supported. |
| reconnect_policy | PNReconnectionPolicy | Optional | `PNReconnectionPolicy.NONE` | Set to `PNReconnectionPolicy.LINEAR` for automatic reconnects. Use option `PNReconnectionPolicy.NONE` to disable automatic reconnects.Use option `PNReconnectionPolicy.EXPONENTIAL` to set exponential retry interval. |
| origin | String | Optional | `ps.pndsn.com` | Custom `origin` if needed. To request a custom domain, contact support and follow the [request process](https://www.pubnub.com/docs/general/setup/data-security#request-process). |
| suppress_leave_events | Boolean | Optional | `False` | If `True`, the client doesn't send presence leave events during the unsubscribe process. |
| enable_subscribe | Boolean | Optional | `True` | You can disable the `subscribe loop` if you don't need to perform subscribe operations. By default, `subscribe loop` is enabled and extra threads/loops are started. They should be explicitly stopped by `pubnub.stop()` method invocation. |
| daemon | Boolean | Optional | `False` | When set to `True`, spawned thread won't keep the application running after SIGTERM. (ctrl-c from command line, for example) |
| disable_token_manager | Boolean | Optional | `False` | When set to `True`, the Token Manager System (TMS) will be disabled. Even if there are applicable tokens, no requests will be authorized. |

### Sample code

:::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.pnconfiguration import PNConfiguration

pn_configuration = PNConfiguration()
# subscribe_key from Admin Portal
pn_configuration.subscribe_key = "my_subscribe_key" // required
# publish_key from Admin Portal (only required if publishing)
pn_configuration.publish_key = "my_publish_key"
# secret_key (only required for modifying/revealing access permissions)
pn_configuration.secret_key = "my_secret_key"
# if cipher_key is passed, all communicatons to/from pubnub will be encrypted
pn_configuration.cipher_key = "my_cipher_key"
# UUID to be used as a device identifier, won't connect if not set
pn_configuration.uuid = "my_custom_uuid"
# if Access Manager is utilized, client will use this authKey in all restricted
# requests
pn_configuration.auth_key = "my_aut_key"
# use SSL
pn_configuration.ssl = True
# how long to wait before giving up connection to client
pn_configuration.connect_timeout = 100
# how long to keep the subscribe loop running before disconnect
pn_configuration.subscribe_timeout = 310
# on non subscribe operations, how long to wait for server response
pn_configuration.non_subscribe_timeout = 300
# PSV2 feature to subscribe with a custom filter expression
pn_configuration.filter_expression = "such=wow"
# heartbeat notifications, by default, the SDK will alert on failed heartbeats.
# other options such as all heartbeats or no heartbeats are supported.
pn_configuration.heartbeat_notification_options = PNHeartbeatNotificationOptions.All
```

## Initialization

Add PubNub to your project using one of the procedures defined in the [Getting Started guide](https://www.pubnub.com/docs/sdks/twisted).

#### Dependency

* PyOpenSSL
* Service-Identity

#### Include the Python SDK in your code

```python
from pubnub.pubnub_twisted import PubNubTwisted
```

### Description

This function is used for initializing the PubNub Client API context. This function must be called before attempting to utilize any API functionality in order to establish account level credentials such as `publish_key` and `subscribe_key`.

:::note
You must call `start()` to initiate the PubNub Client API in Python-Twisted.
:::

### Method(s)

To `Initialize` PubNub you can use the following method(s) in the Python-Twisted SDK:

```python
pubnub = PubNubTwisted(my_pnconfig)
```

| Parameter | Description |
| --- | --- |
| `pn_configuration` *Type: PNConfiguration | Goto [Configuration](#configuration) for more details. |

### Sample code

#### Initialize the PubNub client API

:::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_twisted import PubNubTwisted
from pubnub.pnconfiguration import PNConfiguration

pnconfig = PNConfiguration()
pnconfig.subscribe_key = "my_subkey"
pnconfig.publish_key = "my_pubkey"
pnconfig.ssl = True
pnconfig.uuid = "my_custom_uuid"
pubnub = PubNubTwisted(pnconfig)
```

### Returns

It returns the PubNub instance for invoking PubNub APIs like `publish()`, `subscribe()`, `history()`, `here_now()`, etc.

### Other examples

#### Initialize a non-secure client

:::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_twisted import PubNubTwisted
from pubnub.pnconfiguration import PNConfiguration

pnconfig = PNConfiguration()
pnconfig.subscribe_key = "my_subkey"
pnconfig.publish_key = "my_pubkey"
pnconfig.ssl = False
pnconfig.uuid = "my_custom_uuid"
pubnub = PubNubTwisted(pnconfig)
```

#### Initialization for a Read-Only client

In the case where a client will only read messages and never publish to a channel, you can simply omit the `publish_key` when initializing the client:

:::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_twisted import PubNubTwisted
from pubnub.pnconfiguration import PNConfiguration

pnconfig = PNConfiguration()
pnconfig.subscribe_key = "my_subkey"

pubnub = PubNubTwisted(pnconfig)
```

#### Use a custom UUID

Set a custom `UUID` to identify your users.

:::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_twisted import PubNubTwisted
from pubnub.pnconfiguration import PNConfiguration

pnconfig = PNConfiguration()
pnconfig.subscribe_key = 'mySubscribeKey'
pnconfig.publish_key = 'myPublishKey'
pnconfig.uuid = 'myUniqueUUID'

pubnub = PubNubTwisted(pnconfig)
```

#### Initializing with SSL enabled

This examples demonstrates how to enable PubNub Transport Layer Encryption with `SSL`. Just initialize the client with `ssl` set to `True`. The hard work is done, now the PubNub API takes care of the rest. Just subscribe and publish as usual and you are good to go.

:::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_twisted import PubNubTwisted
from pubnub.pnconfiguration import PNConfiguration

pnconfig = PNConfiguration()
pnconfig.subscribe_key = "my_subkey"
pnconfig.publish_key = "my_pubkey"
pnconfig.ssl = True
pnconfig.uuid = "my_custom_uuid"
pubnub = PubNubTwisted(pnconfig)
```

#### Initializing with Access Manager

:::note Requires Access Manager add-on
This method requires that the *Access Manager* 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.
:::

:::note
Anyone with the `secret_key` can grant and revoke permissions to your app. Never let your `secret_key` be discovered, and to only exchange it / deliver it securely. Only use the `secret_key` on secure server-side platforms.
When you init with `secret_key`, you get root permissions for the Access Manager. With this feature you don't have to grant access to your servers to access channel data. The servers get all access on all channels.
:::

For applications that will administer Access Manager permissions, the API is initialized with the `secret_key` as in the following example:

:::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_twisted import PubNubTwisted
from pubnub.pnconfiguration import PNConfiguration

pnconfig = PNConfiguration()
pnconfig.subscribe_key = "my_subkey"
pnconfig.publish_key = "my_pubkey"
pnconfig.secret_key = "my_secretkey"
pnconfig.uuid = "my_custom_uuid"
pnconfig.ssl = True

pubnub = PubNubTwisted(pnconfig)
```

Now that the pubnub object is instantiated the client will be able to access the Access Manager functions. The pubnub object will use the `secret_key` to sign all Access Manager messages to the PubNub Network.

#### Initializing with a custom event loop

:::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_twisted import PubNubTwisted
from pubnub.pnconfiguration import PNConfiguration

pnconfig = PNConfiguration()
pnconfig.subscribe_key = "my_subkey"
pnconfig.publish_key = "my_pubkey"

pubnub = PubNubTwisted(pnconfig, custom_ioloop=my_event_loop)
```

## Filter expression

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

Stream filtering allows a subscriber to apply a filter to only receive messages that satisfy the conditions of the filter. The message filter is set by the subscribing client(s) but it is applied on the server side thus preventing unwanted messages (those that do not meet the conditions of the filter) from reaching the subscriber.

To set or get message filters, you can use the following methods. To learn more about filtering, refer to the [Publish Messages](https://www.pubnub.com/docs/general/messages/publish) documentation.

### Method(s)

```python
Set Filter Expression
```

The property accepts a `string`.

```python
Get Filter Expression
```

The property returns a `string`.

### Sample code

**Set Filter Expression** [](#set-filter-expression-usage)

:::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.pnconfiguration import PNConfiguration

pnconfig = PNConfiguration()
pnconfig.filter_expression = "such=wow"
```

**Get Filter Expression** [](#get-filter-expression-usage)

```python
filter = pnconfig.filter_expression
```

## Terms in this document

* **PubNub** - PubNub is a real-time messaging platform that provides APIs and SDKs for building scalable applications. It handles the complex infrastructure of real-time communication, including: Message delivery and persistence, Presence detection, Access control, Push notifications, File sharing, Serverless processing with Functions and Events & Actions, Analytics and monitoring with BizOps Workspace, AI-powered insights with Illuminate.
