|
1 | 1 | # PubNub Python SDK (V4) |
| 2 | + |
2 | 3 | [](https://travis-ci.org/pubnub/python) |
3 | 4 | [](https://codecov.io/gh/pubnub/python) |
4 | 5 | [](https://pypi.python.org/pypi/pubnub/) |
5 | 6 | [](https://pypi.python.org/pypi/pubnub/) |
6 | 7 | [](https://www.pubnub.com/docs/python/pubnub-python-sdk-v4) |
7 | 8 |
|
8 | | -The SDK supports Python 2.7, 3.4, 3.5, 3.6, 3.7 and pypy. |
| 9 | +This is the official PubNub Python SDK repository. |
| 10 | + |
| 11 | +PubNub takes care of the infrastructure and APIs needed for the realtime communication layer of your application. Work on your app's logic and let PubNub handle sending and receiving data across the world in less than 100ms. |
| 12 | + |
| 13 | +## Get keys |
| 14 | + |
| 15 | +You will need the publish and subscribe keys to authenticate your app. Get your keys from the [Admin Portal](https://dashboard.pubnub.com/login). |
| 16 | + |
| 17 | +## Configure PubNub |
| 18 | + |
| 19 | +1. Integrate the Python SDK into your project using `pip`: |
| 20 | + |
| 21 | + ```bash |
| 22 | + pip install pubnub |
| 23 | + ``` |
| 24 | + |
| 25 | +2. Configure your keys: |
| 26 | + |
| 27 | + ```python |
| 28 | + pnconfig = PNConfiguration() |
| 29 | +
|
| 30 | + pnconfig.subscribe_key = 'mySubscribeKey' |
| 31 | + pnconfig.publish_key = 'myPublishKey' |
| 32 | + pnconfig.uuid = 'myUniqueUUID' |
| 33 | + pubnub = PubNub(pnconfig) |
| 34 | + ``` |
| 35 | + |
| 36 | +## Add event listeners |
| 37 | + |
| 38 | +```python |
| 39 | +class SubscribeHandler(SubscribeCallback): |
| 40 | + def status(self, pubnub, event): |
| 41 | + print("Is there an error? ", event.is_error()) |
| 42 | + print("Status value for category: %s" % event.category) |
| 43 | + print("Status value for error_data: %s" % event.error_data) |
| 44 | + print("Status value for error: %s" % event.error) |
| 45 | + print("Status value for status_code: %s" % event.status_code) |
| 46 | + print("Status value for operation: %s" % event.operation) |
| 47 | + print("Status value for tls_enabled: %s" % event.tls_enabled) |
| 48 | + print("Status value for uuid: %s" % event.uuid) |
| 49 | + print("Status value for auth_key: %s" % event.auth_key) |
| 50 | + print("Status value for origin: %s" % event.origin) |
| 51 | + print("Status value for client_request: %s" % event.client_request) |
| 52 | + print("Status value for client_response: %s" % event.client_response) |
| 53 | + print("Status value for original_response: %s" % event.original_response) |
| 54 | + print("Status value for affected_channels: %s" % event.affected_channels) |
| 55 | + print("Status value for affected_groups: %s" % event.affected_groups) |
| 56 | +
|
| 57 | + def presence(self, pubnub, presence): |
| 58 | + pass # Handle incoming presence data |
| 59 | +
|
| 60 | + def message(self, pubnub, message): |
| 61 | + pass # Handle incoming messages |
| 62 | +
|
| 63 | + def signal(self, pubnub, signal): |
| 64 | + pass # Handle incoming signals |
| 65 | +
|
| 66 | +pubnub.add_listener(SubscribeHandler()) |
| 67 | +``` |
| 68 | +
|
| 69 | +## Publish/subscribe |
| 70 | +
|
| 71 | +```python |
| 72 | +def my_publish_callback(envelope, status): |
| 73 | + if status.is_error(): |
| 74 | + ... #handle error here |
| 75 | + else: |
| 76 | + ... #handle result here |
| 77 | +
|
| 78 | +pubnub.publish().channel('my_channel').message('Hello world!').pn_async(my_publish_callback) |
| 79 | +
|
| 80 | +pubnub.subscribe().channels('my_channel').execute() |
| 81 | +``` |
9 | 82 |
|
10 | 83 | ## Documentation |
11 | 84 |
|
12 | | -Please review our documentation and examples on the [PubNub Website](https://www.pubnub.com/docs/python/pubnub-python-sdk-v4) |
| 85 | +* [Build your first realtime Python app with PubNub](https://www.pubnub.com/docs/platform/quickstarts/python) |
| 86 | +* [API reference for Python](https://www.pubnub.com/docs/python/pubnub-python-sdk) |
| 87 | +* [API reference for Python (Tornado)](https://www.pubnub.com/docs/python-tornado/pubnub-python-sdk) |
| 88 | +* [API reference for Python (asyncio)](https://www.pubnub.com/docs/python-aiohttp/pubnub-python-sdk) |
13 | 89 |
|
14 | | -## Communication |
| 90 | +## Support |
15 | 91 |
|
16 | | -- If you **need help ** or have a **general question **, contact <[email protected]> |
| 92 | +If you **need help** or have a **general question**, contact [email protected]. |
0 commit comments