Getting started: Pub/Sub in Python

This guide will get you started with Ably Pub/Sub in Python.

You'll establish a realtime connection to Ably and learn to publish and subscribe to messages. You'll also implement presence to track other online clients, and learn how to retrieve message history.

Prerequisites

  1. Sign up for an Ably account.
  2. Create a new app, and create your first API key in the API Keys tab of the dashboard.
  3. Your API key will need the publish, subscribe, presence and history capabilities.
  4. Install Python version 3.8 or greater.
  5. Create a new project and install the Ably Pub/Sub Python SDK:

(Optional) Install Ably CLI

Use the Ably CLI as an additional client to quickly test Pub/Sub features. It can simulate other clients by publishing messages, subscribing to channels, and managing presence states.

  1. Install the Ably CLI:
  1. Run the following to log in to your Ably account and set the default app and API key:

Step 1: Connect to Ably

Clients establish a connection with Ably when they instantiate an SDK instance. This enables them to send and receive messages in realtime across channels.

Create a get_started.py file in your project and add the following function to instantiate the SDK and establish a connection to Ably. At the minimum you need to provide an authentication mechanism. Use an API key for simplicity, but you should use token authentication in production environments. A client_id ensures the client is identified, which is required to use certain features, such as presence:

The Python SDK uses asyncio to provide asynchronous operations. This allows the client to handle events such as connection state changes, message receipt, and presence updates without blocking the main thread.

You can monitor the lifecycle of clients' connections by registering a listener that will emit an event every time the connection state changes. For now, run the function with python get_started.py to log a message to the console to know that the connection attempt was successful. You'll see the message printed to your console, and you can also inspect the connection event in the dev console of your app.

Step 2: Subscribe to a channel and publish a message

Messages contain the data that a client is communicating, such as a short 'hello' from a colleague, or a financial update being broadcast to subscribers from a server. Ably uses channels to separate messages into different topics, so that clients only ever receive messages on the channels they are subscribed to.

Add the following lines to your get_started function before the await asyncio.Event().wait() line to create a channel instance and register a listener to subscribe to the channel. Then run it with python get_started.py:

Use the Ably CLI to publish a message to your first channel. The message will be received by the client you've subscribed to the channel, and be logged to the console.

In a new terminal tab, subscribe to the same channel using the CLI:

Publish another message using the CLI and you will see that, in your other Terminal window, it's received instantly by the client you have running locally, as well as the subscribed terminal instance.

To publish a message with the client running in your IDE, you can add the following line to your get_started function before the await asyncio.Event().wait() line:

Step 3: Presence

Presence enables clients to be aware of one another if they are present on the same channel. You can then show clients who else is online, provide a custom status update for each, and notify the channel when someone goes offline. With the Python SDK you can retrieve the presence set but cannot enter it. This means you can see who is present on a channel, but you cannot announce your own presence from a Python client.

Have a client join the presence set using the Ably CLI:

Add the following lines to your get_started function before the await asyncio.Event().wait() line to retrieve the list of members in the presence set. Then run it with python get_started.py:

Step 4: Retrieve message history

You can retrieve previously sent messages using the history feature. Ably stores all messages for 2 minutes by default in the event a client experiences network connectivity issues. You can extend the storage period of messages if required.

If more than 2 minutes has passed since you published a regular message (excluding the presence events), then you can publish some more before trying out history. You can use the Pub/Sub SDK, Ably CLI or the dev console to do this.

For example, using the Ably CLI to publish 5 messages:

Add the following lines to your get_started function before the await asyncio.Event().wait() line to retrieve any messages that were recently published to the channel. Then run it with python get_started.py:

The output will look similar to the following:

Next steps

Continue to explore the documentation with Python as the selected language:

You can also explore the Ably CLI further, or visit the Pub/Sub API references.