Skip to main content

Async Python SDK for the Metered Realtime Messaging service — WebSocket pub/sub + WebRTC (aiortc).

Project description

metered-realtime

Async Python SDK for the Metered Realtime Messaging service. The Python counterpart to the JavaScript SDK — a Python peer can share a room with browser peers.

Built for server-side and edge use cases the browser can't reach: AI voice agents that join a room to run STT→LLM→TTS, IoT camera/telemetry bridges, recording bots, and headless automation.

Building with AI? Point Claude Code, Cursor, or Copilot at the SDK reference file — llms-realtime-messaging-python-sdk.txt — and describe what you want to build. More prompts: Build with AI.

Install

pip install metered-realtime             # pub/sub only (SignallingClient)
pip install "metered-realtime[webrtc]"   # + the WebRTC peer layer (MeteredPeer)

Requires Python 3.10+.

Quick start — rooms with WebRTC (MeteredPeer)

import asyncio
from metered_realtime import MeteredPeer, PeerJoined, Data, Track

async def main() -> None:
    async with MeteredPeer(api_key="pk_live_...") as peer:
        @peer.on(PeerJoined)
        def _(ev: PeerJoined) -> None:
            print("peer joined:", ev.peer.id)

            @ev.peer.on(Track)
            def _(t: Track) -> None:
                print("receiving", t.track.kind, "stream:", [s.id for s in t.streams])

        @peer.on(Data)
        def _(ev: Data) -> None:
            print(ev.sender_peer_id, "says", ev.data)

        await peer.join("room-42")
        await peer.send({"hello": True})          # broadcast to the room
        # Attach media to every peer (current + future). add_track takes a track;
        # add_stream takes a MediaStream grouping. Any aiortc track works, e.g.:
        #     from aiortc.contrib.media import MediaPlayer
        #     peer.add_track(MediaPlayer("clip.mp3").audio)
        await asyncio.sleep(30)

asyncio.run(main())

Quick start — pub/sub only (SignallingClient)

import asyncio
from metered_realtime import SignallingClient, Message

async def main() -> None:
    async with SignallingClient(api_key="pk_live_...") as client:
        @client.on(Message)
        def _(ev: Message) -> None:
            print(ev.sender_peer_id, ev.data)

        await client.subscribe("room-42")
        await client.publish("room-42", {"hello": True})
        await asyncio.sleep(5)

asyncio.run(main())

Media input

Pass any aiortc-compatible track to add_track, or use the built-in helpers (metered-realtime[webrtc]):

from metered_realtime import AudioSource, from_file, from_rtsp, iter_frames

# Push synthesized audio (e.g. an AI agent's speech) into the room:
source = AudioSource(input_rate=16_000)        # 16 kHz mono PCM in
peer.add_track(source)
await source.push(pcm_bytes)                    # emitted as real-time 48 kHz audio
source.end()

# Or feed from a file / IP camera (.audio / .video is None if the source lacks it):
peer.add_track(from_file("clip.mp4").audio)
peer.add_track(from_rtsp("rtsp://cam:554/stream").video)

# Consume a peer's incoming media (e.g. for speech-to-text):
async for frame in iter_frames(track):          # ends cleanly when the track stops
    ...

from_camera / from_microphone / screen_share capture from local devices (platform-dependent). See the documentation for auth (publishable keys vs. token providers), presence, direct messages, media, and data channels.

Provisional: the media-input helpers (AudioSource, iter_frames, from_file / from_rtsp / from_camera / from_microphone / screen_share) may change in a minor release. The core media API on MeteredPeeradd_stream / add_track / replace_track, and MediaStream — is stable.

Documentation

Full Python SDK docs: metered.ca/docs/realtime-messaging/sdk-python

  • Getting started — install, auth, your first peer
  • API referenceMeteredPeer, RemotePeer, DataChannel, media, every event/error/code
  • Guides — authentication, reconnect best-practices, AI agent communication, IoT telemetry
  • Errors & codes — close codes, exception types, how to react

Examples

Runnable end-to-end demos, each walked through in the examples docs (each takes METERED_KEY=pk_live_…):

  • data_channel_echo.py — open a peer-to-peer data channel and echo messages.
  • audio_agent.py — stream synthesized audio into a room and consume a peer's audio (the "agent in the call" shape).

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

metered_realtime-1.0.0.tar.gz (61.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

metered_realtime-1.0.0-py3-none-any.whl (73.4 kB view details)

Uploaded Python 3

File details

Details for the file metered_realtime-1.0.0.tar.gz.

File metadata

  • Download URL: metered_realtime-1.0.0.tar.gz
  • Upload date:
  • Size: 61.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for metered_realtime-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9ac546c09eab34e3fde48614ff03b952d0755ee55d3264a866b3c678ae25c80e
MD5 29615f276c46229d1afba12c0767582a
BLAKE2b-256 47ee82d6018103c1318a26374e578794fa8a7ddc54e221c191f58ca1e1f39dc0

See more details on using hashes here.

File details

Details for the file metered_realtime-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: metered_realtime-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 73.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for metered_realtime-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2224beb66d3aab42357676d44e9032ed2d8b505300ec35abaaee0b326a5ad926
MD5 8d9404c8735d9c23c839a668cd99a52a
BLAKE2b-256 80a0949106dda404f79404760e3d2ac2c59ab722e8174cebac2e29d982481a46

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page