Skip to main content

Real-time WebSocket client for AmarWave servers

Project description

amarwave

Official Python client for AmarWave real-time messaging — async, typed, zero boilerplate.

PyPI version Python License


Installation

pip install amarwave

Quick Start

import asyncio
from amarwave import AmarWave

async def main():
    aw = AmarWave(
        app_key    = "YOUR_APP_KEY",
        app_secret = "YOUR_APP_SECRET",
    )

    ch = await aw.subscribe("public-chat")
    ch.bind("message", lambda data: print(data["user"], data["text"]))

    await ch.publish("message", {"user": "Ali", "text": "Hello!"})
    await aw.listen()  # keep alive forever

asyncio.run(main())

Configuration

Parameter Type Default Description
app_key str Your app key (required)
app_secret str "" App secret for HMAC channel auth
cluster str "default" "default" | "eu" | "us" | "ap1" | "ap2"
auth_endpoint str "/broadcasting/auth" Server auth URL for private/presence channels
auth_headers dict {} Headers sent to the auth endpoint
reconnect_delay float 1.0 Base reconnect delay in seconds
max_reconnect_delay float 30.0 Max reconnect delay in seconds
max_retries int 5 Max reconnect attempts (0 = infinite)
activity_timeout float 120.0 Seconds between keepalive pings
pong_timeout float 30.0 Seconds to wait for pong before reconnecting

Clusters

All clusters connect to amarwave.com. The cluster parameter is reserved for future regional routing.

Cluster WebSocket API
default wss://amarwave.com https://amarwave.com
eu wss://amarwave.com https://amarwave.com
us wss://amarwave.com https://amarwave.com
ap1 wss://amarwave.com https://amarwave.com
ap2 wss://amarwave.com https://amarwave.com
aw = AmarWave(app_key="KEY", app_secret="SECRET", cluster="eu")

Channel API

ch = await aw.subscribe("public-chat")

ch.bind("message", handler)           # listen for event
ch.bind_global(lambda e, d: ...)      # listen for all events on this channel
ch.unbind("message", handler)         # remove listener
await ch.publish("message", data)     # publish via HTTP API → bool
await aw.publish("ch", "ev", data)    # top-level publish shortcut

ch.name        # "public-chat"
ch.subscribed  # True when server confirmed subscription

Connection Events

aw.bind("connecting",   lambda _: print("Connecting…"))
aw.bind("connected",    lambda _: print(f"Connected: {aw.socket_id}"))
aw.bind("disconnected", lambda _: print("Disconnected"))
aw.bind("error",        lambda e: print(f"Error: {e}"))

Private & Presence Channels

# Client-side HMAC auth (app_secret required)
aw = AmarWave(app_key="KEY", app_secret="SECRET")
ch = await aw.subscribe("private-orders")    # auto-signed
ch = await aw.subscribe("presence-room-1")  # auto-signed

# Server-side auth (omit app_secret, provide auth_endpoint)
aw = AmarWave(
    app_key       = "KEY",
    auth_endpoint = "https://yourapp.com/api/broadcasting/auth",
    auth_headers  = {"Authorization": f"Bearer {token}"},
)
ch = await aw.subscribe("private-orders")

Django Integration

import asyncio
from amarwave import AmarWave

# One-shot publish from a sync Django view
def notify_user(user_id: int, message: str) -> bool:
    async def _publish() -> bool:
        aw = AmarWave(app_key="KEY", app_secret="SECRET")
        return await aw.publish(f"private-user-{user_id}", "notification", {"message": message})
    return asyncio.run(_publish())

FastAPI Integration

from contextlib import asynccontextmanager
from fastapi import FastAPI
from amarwave import AmarWave

aw = AmarWave(app_key="KEY", app_secret="SECRET")

@asynccontextmanager
async def lifespan(app: FastAPI):
    ch = await aw.subscribe("public-updates")
    ch.bind("message", lambda d: print(d))
    yield
    await aw.disconnect()

app = FastAPI(lifespan=lifespan)

@app.post("/notify")
async def notify(message: str):
    await aw.publish("public-updates", "message", {"text": message})
    return {"ok": True}

Requirements

  • Python 3.10+
  • websockets >= 12.0
  • httpx >= 0.27.0

License

MIT © AmarWave

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

amarwave-2.0.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

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

amarwave-2.0.0-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file amarwave-2.0.0.tar.gz.

File metadata

  • Download URL: amarwave-2.0.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for amarwave-2.0.0.tar.gz
Algorithm Hash digest
SHA256 911e9aa5992e0a1aa89dc46c92db1c7959c3fc60b42b40df2a89271a3f098466
MD5 6d845ce5aa45ffb840d91502fe748a2b
BLAKE2b-256 95efa71d33baffc689520b907cd47e58519b4ce0fca973fb3cdac32efc079b49

See more details on using hashes here.

File details

Details for the file amarwave-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: amarwave-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for amarwave-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d600c7c679fec1837d4ca00bb0a5582d3fa6f8fd394432e84a33280b01510f2a
MD5 acc8f98b2cda32f33d20ddd869c24f36
BLAKE2b-256 eb09f6da94f13c62f8b2d2dc0aed062f7248518e9d6471e9b6b8ca6a2cf98104

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