Modern WebSocket framework inspired by aiogram
Project description
aiows
Modern WebSocket framework for Python inspired by aiogram
Features
- Declarative approach with decorators
- Typed messages with Pydantic
- Simple routing like in aiogram
- Async/await support
Quick Start
"""
Simple chat example using aiows
"""
# Import main aiows classes
from aiows import WebSocketServer, Router, ChatMessage, WebSocket
# Create router for handling events
router = Router()
@router.connect()
async def handle_connect(websocket: WebSocket):
"""Connection handler - send welcome message"""
print(f"New connection!")
# Send welcome message
welcome_data = {
"type": "chat",
"text": "Welcome to chat!",
"user_id": 0
}
await websocket.send_json(welcome_data)
@router.message("chat")
async def handle_chat_message(websocket: WebSocket, message: ChatMessage):
"""Chat message handler - send echo response"""
print(f"Received message from user {message.user_id}: {message.text}")
# Create response message (echo)
response_data = {
"type": "chat",
"text": f"Echo: {message.text}",
"user_id": 999 # Bot ID
}
await websocket.send_json(response_data)
@router.disconnect()
async def handle_disconnect(websocket: WebSocket, reason: str):
"""Disconnect handler - log the event"""
print(f"User disconnected. Reason: {reason}")
def main():
"""Main function - create and run server"""
# Create server
server = WebSocketServer()
# Connect router to server
server.include_router(router)
# Run server on localhost:8000
print("Starting chat server...")
print("Connect via WebSocket at ws://localhost:8000")
server.run(host="localhost", port=8000)
if __name__ == "__main__":
main()
Installation
pip install aiows
Requirements
- Python 3.8+
- pydantic>=2.0.0
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.0.4.tar.gz
(9.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
aiows-0.0.4-py3-none-any.whl
(8.3 kB
view details)
File details
Details for the file aiows-0.0.4.tar.gz.
File metadata
- Download URL: aiows-0.0.4.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89147f225f85f4a5451caec3af59cf0ccc52338082c8656c1410fa30ffb81ca6
|
|
| MD5 |
61aed82f0176ce76b6185632fd063f1b
|
|
| BLAKE2b-256 |
55cfde4fd89116af178a45a251435387dd494c97b28a6ba218fbce7938e46e86
|
File details
Details for the file aiows-0.0.4-py3-none-any.whl.
File metadata
- Download URL: aiows-0.0.4-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba8282f98489a0c72f689cc5bf842380250723eb2e4cea7a9131e42f611a5216
|
|
| MD5 |
e4bc1b9cd17158af493df109f780b052
|
|
| BLAKE2b-256 |
13478c2ce7722922be2824fc813ea6dd8f2ff22cc5af172abe3c0b62a94204cf
|