Skip to main content

Quickstart: Python

Build a real-time pub/sub app in 5 minutes.

Prerequisites

Install

pip install relayx_py

Publisher

Create publisher.py:

import asyncio
from relayx_py import Realtime

async def main():
client = Realtime({
"api_key": "your-api-key",
"secret": "your-secret"
})

client.init({})
await client.connect()

await client.publish("chat.room1", {
"user": "alice",
"text": "Hello from Python!"
})

print("Message sent!")
await client.close()

asyncio.run(main())

Subscriber

Create subscriber.py:

import asyncio
from relayx_py import Realtime

async def main():
client = Realtime({
"api_key": "your-api-key",
"secret": "your-secret"
})

client.init({})

async def on_message(msg):
print(f"{msg['data']['user']}: {msg['data']['text']}")

await client.on("chat.room1", on_message)
await client.connect()

print("Listening for messages...")
# Keep running
while True:
await asyncio.sleep(1)

asyncio.run(main())

Run It

# Terminal 1 - start subscriber
python subscriber.py

# Terminal 2 - send a message
python publisher.py

You should see alice: Hello from Python! appear in the subscriber terminal.