Skip to main content

Kryten-Robot

Python Version License PyPI Version

Kryten-Robot is a CyTube to NATS bridge connector that connects to CyTube chat servers via Socket.IO and publishes all events to a NATS message bus. This enables building distributed microservice architectures around CyTube channels.

Overview

Kryten-Robot acts as the central bridge between CyTube and your microservices:

  • Connects to CyTube servers via Socket.IO
  • Publishes all CyTube events to NATS with structured subjects
  • Subscribes to command subjects to control CyTube
  • Maintains connection health with automatic reconnection
  • Tracks channel state (users, playlist, emotes)
  • Exposes state query API via NATS request/reply

Architecture

┌─────────────┐         ┌──────────────┐         ┌─────────────────┐
│   CyTube    │◄───────►│ Kryten-Robot │◄───────►│   NATS Server   │
│   Server    │ Socket  │   (Bridge)   │  Pub/   │                 │
└─────────────┘  .IO    └──────────────┘  Sub    └─────────────────┘
                                                          ▲
                                                          │
                    ┌─────────────────────────────────────┴──────────┐
                    │                                                 │
              ┌─────▼──────┐    ┌──────────────┐    ┌──────────────┐
              │ kryten-cli │    │kryten-       │    │  Your Custom │
              │  (Control) │    │userstats     │    │ Microservice │
              └────────────┘    └──────────────┘    └──────────────┘

Features

  • ✅ Full CyTube Event Coverage: All Socket.IO events published to NATS
  • ✅ Unified Command Pattern: Single subject per service (kryten.robot.command)
  • ✅ State Management: Real-time tracking of users, playlist, emotes
  • ✅ State Query API: Request/reply for current channel state
  • ✅ Connection Resilience: Automatic reconnection with exponential backoff
  • ✅ Health Monitoring: HTTP health endpoint for orchestration
  • ✅ Correlation IDs: Distributed tracing support
  • ✅ Structured Logging: JSON logs with correlation context

Installation

From PyPI

pip install kryten-robot

As systemd Service (Linux)

For production deployments on Linux with systemd:

# Clone the repository
git clone https://github.com/grobertson/kryten-robot.git
cd kryten-robot

# Run installation script (requires root)
sudo bash install.sh

The installer will:

  • Create system user and directories
  • Set up Python virtual environment
  • Install kryten-robot from PyPI
  • Configure systemd service
  • Create example config file

See systemd/README.md for detailed setup instructions.

From Source

git clone https://github.com/grobertson/kryten-robot.git
cd kryten-robot
pip install -e .

With Poetry

uv add kryten-robot

Quick Start

1. Create Configuration File

Create config.json:

{
  "cytube": {
    "domain": "cytu.be",
    "channel": "your-channel",
    "user": "your-bot-username",
    "password": "your-bot-password"
  },
  "nats": {
    "servers": ["nats://localhost:4222"],
    "user": null,
    "password": null,
    "max_reconnect_attempts": 60,
    "reconnect_time_wait": 2
  },
  "health": {
    "enabled": true,
    "host": "0.0.0.0",
    "port": 28080
  },
  "commands": {
    "enabled": true
  },
  "logging": {
    "format": "text",
    "correlation_id": true
  },
  "log_level": "INFO"
}

2. Run Kryten-Robot

# Using the installed command
kryten-robot config.json

# Or with Python module
python -m kryten config.json

# With custom log level
kryten-robot config.json --log-level DEBUG

3. Verify Operation

Check health endpoint:

curl http://localhost:28080/health

Expected response:

{
  "status": "healthy",
  "cytube_connected": true,
  "nats_connected": true,
  "channel": "your-channel",
  "uptime_seconds": 42.5
}

NATS Subject Structure

Event Publishing

All CyTube events are published to:

kryten.events.cytube.{channel}.{event_name}

Examples:

  • kryten.events.cytube.420grindhouse.chatmsg - Chat messages
  • kryten.events.cytube.420grindhouse.adduser - User joins
  • kryten.events.cytube.420grindhouse.userleave - User leaves
  • kryten.events.cytube.420grindhouse.changeMedia - Video changes

Command Subscription

Kryten-Robot accepts commands on:

kryten.robot.command

Command payload:

{
  "service": "robot",
  "command": "state.userlist",
  "correlation_id": "optional-trace-id"
}

Available commands:

  • state.emotes - Get all channel emotes
  • state.playlist - Get current playlist
  • state.userlist - Get all users in channel
  • state.user - Get specific user info (requires username param)
  • state.profiles - Get all user profiles
  • state.all - Get complete channel state
  • system.health - Get system health status
  • system.channels - Get list of connected channels
  • system.version - Get Kryten-Robot version

Configuration Reference

CyTube Section

Field Type Required Description
domain string Yes CyTube server domain (e.g., "cytu.be")
channel string Yes Channel name to connect to
user string Yes Bot account username
password string Yes Bot account password

NATS Section

Field Type Required Default Description
servers array Yes - NATS server URLs
user string No null NATS authentication user
password string No null NATS authentication password
max_reconnect_attempts int No 60 Max reconnection attempts
reconnect_time_wait int No 2 Seconds between reconnect attempts

Health Section

Field Type Required Default Description
enabled bool No true Enable HTTP health endpoint
host string No "0.0.0.0" Health endpoint bind address
port int No 28080 Health endpoint port

Commands Section

Field Type Required Default Description
enabled bool No true Enable command subscriber

Logging Section

Field Type Required Default Description
format string No "text" Log format: "text" or "json"
correlation_id bool No true Include correlation IDs

Root Level

Field Type Required Default Description
log_level string No "INFO" Log level: DEBUG, INFO, WARNING, ERROR, CRITICAL

Running as a Service

Systemd (Linux)

See systemd/README.md for complete systemd service configuration.

Quick setup:

sudo cp systemd/kryten-robot.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable kryten-robot
sudo systemctl start kryten-robot

Windows Service

Use NSSM (Non-Sucking Service Manager):

nssm install kryten-robot "C:\Python311\python.exe" "-m kryten config.json"
nssm set kryten-robot AppDirectory "C:\opt\kryten-robot"
nssm start kryten-robot

Development

Setup Development Environment

git clone https://github.com/grobertson/kryten-robot.git
cd kryten-robot

# Create virtual environment
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install in editable mode with dev dependencies
pip install -e ".[dev]"

Running Tests

pytest

With coverage:

pytest --cov=kryten --cov-report=html

Code Quality

# Format code
black kryten/

# Lint
ruff check kryten/

# Type checking
mypy kryten/

Integration Examples

Using kryten-py Library

from kryten import KrytenClient, KrytenConfig

config = KrytenConfig.from_json("config.json")

async with KrytenClient(config) as client:
    # Query current userlist
    response = await client.nats_request(
        "kryten.robot.command",
        {"service": "robot", "command": "state.userlist"}
    )
    
    if response["success"]:
        users = response["data"]["users"]
        print(f"Users online: {len(users)}")

Using kryten-cli

# Get current playlist
kryten list queue

# Get all users
kryten list users

# Get channel emotes
kryten list emotes

Architecture Documentation

Troubleshooting

Connection Issues

Problem: Kryten-Robot won't connect to CyTube

  • Verify credentials in config.json
  • Check if channel name is correct
  • Ensure CyTube server is accessible

Problem: NATS connection failures

  • Verify NATS server is running: nats-server -v
  • Check NATS server URL in config
  • Test NATS connectivity: nats-cli pub test "hello"

Performance Issues

Problem: High memory usage

  • Check for excessive event backlog
  • Verify NATS consumers are processing events
  • Monitor with health endpoint: curl http://localhost:28080/health

Problem: Events not being published

  • Check log level is INFO or DEBUG
  • Verify NATS subjects with nats-cli sub "kryten.events.>"
  • Check correlation logs for event flow

Requirements

  • Python 3.11 or higher
  • NATS server 2.9.0 or higher
  • CyTube server (any version with Socket.IO support)

Related Projects

Contributing

Contributions welcome! Please:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes with tests
  4. Run tests and linting (pytest && black . && ruff check .)
  5. Commit your changes (git commit -m 'Add amazing feature')
  6. Push to branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

Changelog

v0.5.0 (2025-12-08)

Major Changes:

  • ✅ Unified command pattern: All services now use kryten.{service}.command
  • ✅ State query API: Request/reply interface for channel state
  • ✅ Fixed startup banner bug with wildcard normalization
  • ✅ PyPI packaging: Now installable via pip install kryten-robot

API Updates:

  • Changed: Command subjects from kryten.commands.cytube.* to kryten.robot.command
  • Added: State query commands (state.emotes, state.playlist, etc.)
  • Added: System health command via NATS

Documentation:

  • Added: KRYTEN_ARCHITECTURE.md with comprehensive architecture overview
  • Updated: All examples to use unified command pattern
  • Added: PyPI publication workflow

Built with ❤️ for the CyTube community

Release files for kryten-robot 1.12.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kryten-robot 1.12.5
File Size Uploaded
kryten_robot-1.12.5.tar.gz 319.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kryten-robot 1.12.5
File Interpreter ABI Platform
kryten_robot-1.12.5-py3-none-any.whl Python 3 none any Details

Total release size: 439.6 kB

Release files / kryten_robot-1.12.5.tar.gz

Download URL kryten_robot-1.12.5.tar.gz
Size 319.5 kB
Tags Source
SHA-256 checksum
How to use checksums
e43007511cb1372ecc44b11cd054b7d2f5201234dd58998d4002e48d028fbc6f
BLAKE2b-256 checksum
How to use checksums
dd70cd8175dbcd28406bf9af30843d39854544211dd8b37424969ce3904d9b92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / kryten_robot-1.12.5-py3-none-any.whl

Download URL kryten_robot-1.12.5-py3-none-any.whl
Size 120.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a76024e749644e679a1690c9552e9ba507a27ccd1ead0142d2d747cb39af8641
BLAKE2b-256 checksum
How to use checksums
6aca96e5ce26c337e7cb47986c65bc9645171bafa2202a9a98368cc3364220b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

1.12.5 This release

2 release files

1.11.0

2 release files

1.10.0

2 release files

1.9.0

2 release files

1.8.0

2 release files

1.7.0

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

0.9.1

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.5

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.6.19

2 release files

0.6.18

2 release files

0.6.17

2 release files

0.6.16

2 release files

0.6.15

2 release files

0.6.14

2 release files

0.6.13

2 release files

0.6.12

2 release files

0.6.11

2 release files

0.6.10

2 release files

0.6.9

2 release files

0.6.8

2 release files

0.6.7

2 release files

0.6.6

2 release files

0.6.5

2 release files

0.6.3

2 release files

0.6.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page