Skip to main content

Modern WebSocket framework inspired by aiogram

Project description

aiows

Modern WebSocket framework inspired by aiogram. Built for developers who ship fast.

Install

pip install aiows

Quick Start

from aiows import WebSocketServer, Router, ChatMessage, WebSocket

router = Router()

@router.connect()
async def on_connect(websocket: WebSocket):
    await websocket.send_json({"type": "welcome"})

@router.message("chat")
async def handle_chat(websocket: WebSocket, message: ChatMessage):
    await websocket.send_json({
        "type": "response", 
        "text": f"Echo: {message.text}"
    })

server = WebSocketServer()
server.include_router(router)
server.run("localhost", 8000)

That's it. Your WebSocket server is running.

Real Features

Authentication

from aiows import AuthMiddleware

auth = AuthMiddleware("your-secret-key")
server.add_middleware(auth)

# Connect with: ws://localhost:8000?token=user123your-secret-key

Rate Limiting

from aiows import RateLimitingMiddleware

rate_limit = RateLimitingMiddleware(max_messages_per_minute=60)
server.add_middleware(rate_limit)

SSL/TLS Support

server = WebSocketServer()
server.run("localhost", 8443, ssl=True)  # Auto-generates dev certs

Production Config

from aiows import create_production_server

server = create_production_server()
server.include_router(router)
server.run()

Message Types

from aiows import BaseMessage, ChatMessage, JoinRoomMessage

@router.message("join")
async def handle_join(websocket: WebSocket, message: JoinRoomMessage):
    room_id = message.room_id
    user_name = message.user_name
    # Handle room join logic

Health Checks

from aiows import setup_health_checks

health_checker = setup_health_checks(server, http_port=9000)
# GET /health returns server status

Connection Stats

@router.message("stats")
async def get_stats(websocket: WebSocket, message):
    stats = server.get_connection_stats()
    await websocket.send_json(stats)

Graceful Shutdown

Built-in. Press Ctrl+C and all connections close properly.

# Or programmatically
await server.shutdown(timeout=30)

Configuration

Environment variables or config objects:

from aiows import AiowsSettings

settings = AiowsSettings(profile="production")
server = WebSocketServer.from_settings(settings)

Middleware Stack

Order matters:

server.add_middleware(LoggingMiddleware())
server.add_middleware(ConnectionLimiterMiddleware())  
server.add_middleware(AuthMiddleware("secret"))
server.add_middleware(RateLimitingMiddleware(60))

Error Handling

from aiows.exceptions import MessageValidationError

@router.message("data")
async def handle_data(websocket: WebSocket, message):
    try:
        result = process_data(message)
        await websocket.send_json({"result": result})
    except MessageValidationError:
        await websocket.send_json({"error": "Invalid data"})

Examples

Check /examples for:

  • Secure chat with SSL
  • Authentication flow
  • Middleware configuration
  • Production deployment

Why aiows?

  • Fast setup: 5 lines to working server
  • Security features: SSL, auth, rate limiting, health checks
  • Type safe: Full type hints, Pydantic validation
  • Async native: Built on asyncio, handles thousands of connections
  • Middleware system: Compose functionality like Express.js
  • Zero config: Works out of box, configurable when needed

Roadmap

  • Simplify middleware execution system
  • Fix connection management race conditions
  • Add rooms/channels functionality
  • Optimize WebSocket performance locks
  • Add connection pooling support
  • Improve error handling consistency
  • Add broadcasting capabilities
  • Performance benchmarking suite
  • Enhanced documentation

License

MIT

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

aiows-0.1.2.tar.gz (113.6 kB view details)

Uploaded Source

Built Distribution

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

aiows-0.1.2-py3-none-any.whl (54.9 kB view details)

Uploaded Python 3

File details

Details for the file aiows-0.1.2.tar.gz.

File metadata

  • Download URL: aiows-0.1.2.tar.gz
  • Upload date:
  • Size: 113.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for aiows-0.1.2.tar.gz
Algorithm Hash digest
SHA256 bb743eec4c65ada6bd0d16ea376298cfe4138ea5502af0da4be62f67debe1cb1
MD5 944b1360e84ed1b0d81721325e1eba61
BLAKE2b-256 bca4b3d9274d8b00c9dd53687736ec10b354060289f0056bf888d6cea1884fbb

See more details on using hashes here.

File details

Details for the file aiows-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: aiows-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 54.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for aiows-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 101aed64ff2970da9ad3f7b6e518eb9b9a9d0ba49bbb683ed6cbdd19df9e9862
MD5 e26f9b7c7cd528a44b2d29d0d7fbc0fc
BLAKE2b-256 b7ff1120cc62339457677dcee189a6b793531124f89037f6774430acbc8a9883

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