Skip to main content

Modern WebSocket framework inspired by aiogram

Project description

wsogram

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 wsogram
"""

# Import main wsogram classes
from wsogram 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 wsogram

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

wsogram-0.0.1.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

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

wsogram-0.0.1-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file wsogram-0.0.1.tar.gz.

File metadata

  • Download URL: wsogram-0.0.1.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

Hashes for wsogram-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a7d39c41e5f6f282f97f3df314327cd9cf035db73aa32c34047567241f66f434
MD5 74bf42926c36a10dc3293fefdd8ce551
BLAKE2b-256 b21ab7f9af91af6a8efb08447dc218a638c2ee12d759abc633a05670177be2ff

See more details on using hashes here.

File details

Details for the file wsogram-0.0.1-py3-none-any.whl.

File metadata

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

File hashes

Hashes for wsogram-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6f12ff5ab6bafadeec8632bee258ffc002ec2485684c3057ae66ae8747769222
MD5 c0cb0c7db016e80cccf86c81bc46cd5c
BLAKE2b-256 9684ff78bfb8f0a8eae516f879a1d25ad9866f4e281e93506b39ae32adf52293

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