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
  • Implement message filters
  • 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.1.tar.gz (95.8 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.1-py3-none-any.whl (47.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiows-0.1.1.tar.gz
  • Upload date:
  • Size: 95.8 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.1.tar.gz
Algorithm Hash digest
SHA256 73c4d46d1a29379464222612af4766bf2d03f1409ab2d678d3017a4e04b99945
MD5 0cdd99345a44049478ab0c08a51a5375
BLAKE2b-256 399782e249f5f1aa779e5da4df61f158a9079ab330e5a5928af9e3fd364ad905

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiows-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 47.2 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6154213a4883d3e3f28873d0e19515658be44bb6447c75b4b275e9409e328864
MD5 c5fbd065abc2e0722df0e718bcbccf38
BLAKE2b-256 0d3bb44f16efc50722315d53d1bcbc33f1a7b8a53f015a9c474dea930a06dc3d

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