FastSocket is an advanced, high-performance, event-driven real-time messaging and logging system architected with a robust integration of FastAPI, Socket.IO, AsyncAPI, Pydantic, Typer, Logfire, and ReactiveX ( RxPY). Designed to facilitate scalable, maintainable, and efficient real-time applications, FastSocket is ideal for complex systems requiring seamless event handling, data validation, and reactive programming paradigms.
In the realm of real-time web applications, efficient event handling and robust data validation are paramount. FastSocket leverages the synergistic capabilities of FastAPI for asynchronous web serving, Socket.IO for real-time bidirectional communication, AsyncAPI for standardized event-driven architecture definitions, Pydantic for stringent data validation, Typer for intuitive CLI interactions, Logfire for comprehensive logging and monitoring, and ReactiveX ( RxPY) for reactive programming paradigms. This README delineates the intricate architecture, underlying technologies, implementation strategies, and operational workflows that collectively empower FastSocket to deliver unparalleled performance and scalability in real-time applications.
FastSocket emerges as a cutting-edge solution for developers seeking to build real-time, event-driven applications with minimal overhead and maximal scalability. By harnessing the power of modern Python frameworks and libraries, FastSocket streamlines the development process, ensuring consistency, reliability, and maintainability across both backend and frontend components.
- Real-Time Communication: Facilitate instant, bidirectional communication between clients and servers.
- Standardized Event Definitions: Utilize AsyncAPI to define and manage event-driven architectures.
- Robust Data Validation: Implement Pydantic models to ensure data integrity and type safety.
- Intuitive CLI Management: Employ Typer to create a user-friendly command-line interface for server and event management.
- Comprehensive Logging: Integrate Logfire for real-time log monitoring and analysis.
- Reactive Programming: Leverage RxPY to handle complex event streams and reactive data flows.
FastSocket's architecture is meticulously designed to promote scalability, maintainability, and efficiency. The system is compartmentalized into distinct modules, each responsible for specific functionalities, ensuring a clean separation of concerns.
-
FastAPI Backend:
- Serves as the asynchronous web server handling API requests.
- Integrates with Socket.IO for real-time event handling.
- Utilizes AsyncAPI for defining event-driven interactions.
-
Socket.IO Integration:
- Facilitates real-time, bidirectional communication between clients and the server.
- Manages WebSocket connections, event broadcasting, and room management.
-
AsyncAPI Specification:
- Serves as the single source of truth for defining event channels, messages, and payloads.
- Drives the automatic generation of event handlers and frontend components.
-
Pydantic Models:
- Ensures rigorous data validation and type enforcement for incoming and outgoing data.
- Enhances data integrity across the application.
-
Typer CLI:
- Provides a command-line interface for managing server operations, event registrations, and log viewing.
- Streamlines administrative tasks and operational workflows.
-
Logfire Integration:
- Offers real-time log monitoring and visualization.
- Enhances observability and debugging capabilities.
-
ReactiveX (RxPY):
- Implements reactive programming paradigms to handle complex event streams.
- Enables responsive and scalable event processing.
+----------------+ +----------------+ +----------------+
| | | | | |
| Frontend | <---> | Socket.IO | <---> | FastAPI |
| (Nuxt.js) | | Server | | Backend |
| | | | | |
+----------------+ +----------------+ +----------------+
| | |
| | |
v v v
+----------------+ +----------------+ +----------------+
| | | | | |
| ReactiveX | | Logfire | | AsyncAPI |
| (RxPY) | | Logging | | Specification |
| | | | | |
+----------------+ +----------------+ +----------------+
- FastAPI: An asynchronous, high-performance web framework for building APIs with Python 3.6+ based on standard Python type hints.
- Socket.IO: A library for real-time, bidirectional, and event-based communication between the browser and the server.
- AsyncAPI: A specification for defining event-driven architectures, providing a standardized way to document and visualize APIs.
- Pydantic: Data validation and settings management using Python type annotations.
- Typer: A library for building command-line interface (CLI) applications with ease.
- Logfire: A logging platform that provides real-time log monitoring and analysis.
- ReactiveX (RxPY): A library for composing asynchronous and event-based programs using observable sequences.
- Nuxt.js: A progressive framework based on Vue.js for building server-side rendered (SSR) applications and static websites.
- Vue.js: A progressive JavaScript framework for building user interfaces.
- Poetry: A tool for dependency management and packaging in Python.
- Docker & Docker Compose: Tools for containerizing applications and orchestrating multi-container Docker applications.
- Pytest: A robust testing framework for Python.
fastsocket/
├── Dockerfile # Docker configuration for containerizing the application
├── README.md # Comprehensive project documentation
├── docker-compose.yml # Docker Compose configuration for multi-service orchestration
├── fastsocket.pid # PID file to track the running server process
├── logs/
│ └── debug.log # Log file generated by Loguru for structured logging
├── poetry.lock # Locked dependencies for Poetry
├── pyproject.toml # Poetry project configuration
├── src/
│ └── fastsocket/
│ ├── __init__.py
│ ├── api.py # FastAPI application setup with event and lifespan handling
│ ├── cli.py # Typer CLI for server and log management
│ ├── commands/
│ │ ├── __init__.py
│ │ └── log_cmds.py # CLI commands for log management
│ ├── event_registry.py # Module for registering and reloading event handlers
│ ├── events/ # Directory containing event handler modules
│ │ ├── __init__.py
│ │ ├── example_event.py
│ │ ├── on_light_measured.py
│ │ ├── send_notification.py
│ │ ├── user_deleted.py
│ │ ├── user_signed_up.py
│ │ └── user_updated.py
│ ├── happy_client.py # Example client implementation for testing
│ ├── logger.py # Configures Loguru for structured logging
│ └── logs/
│ └── debug.log
└── tests/
├── __init__.py
├── test_api.py # Tests for FastAPI endpoints
├── test_cli.py # Tests for CLI commands
└── test_import.py # Tests for module imports and integrity
- Dockerfile & docker-compose.yml: Facilitate containerization and orchestration, enabling seamless deployment across various environments.
- fastsocket.pid: Maintains the Process ID of the running server, aiding in server management operations like stop and status checks.
- logs/debug.log: Centralized log file capturing structured logs from the application, enhancing observability and debugging.
- pyproject.toml & poetry.lock: Manage project dependencies and configurations using Poetry, ensuring reproducible builds.
- src/fastsocket/: Core application logic encompassing API setup, CLI commands, event handling, logging
configuration, and example client implementations.
- api.py: Configures the FastAPI application, integrates Socket.IO, manages lifespan events, and defines API endpoints.
- cli.py: Implements the Typer-powered CLI, offering commands for starting, stopping, reloading the server, and managing logs.
- commands/log_cmds.py: Houses specific CLI commands related to log viewing, tailing, and clearing.
- event_registry.py: Handles dynamic registration and reloading of event handlers based on AsyncAPI specifications.
- events/: Contains individual event handler modules, each corresponding to a specific event type defined in AsyncAPI.
- happy_client.py: Serves as an example client for testing and demonstrating real-time event interactions.
- logger.py: Sets up Loguru with configurations for log rotation, formatting, and log level management.
- tests/: Encompasses a suite of tests ensuring the reliability and correctness of API endpoints, CLI functionalities, and module imports.
- Python 3.13+
- Poetry for dependency management
- Docker & Docker Compose (optional, for containerization)
- Node.js & npm (if integrating with the Nuxt.js frontend)
git clone https://github.com/your-username/fastsocket.git
cd fastsocket
Using Poetry:
poetry install
Activate the virtual environment:
poetry shell
Create a .env
file in the project root with the following variables:
API_KEY=<your_api_key>
LOG_LEVEL=DEBUG
HOST=0.0.0.0
PORT=8000
EVENTS_DIR=src/fastsocket/events
LOG_DIR=logs
Ensure that sensitive information like API_KEY
is securely managed and not exposed publicly.
FastSocket offers a comprehensive Command-Line Interface (CLI) powered by Typer, enabling efficient management of server operations, event handling, and log monitoring.
Ensure that the virtual environment is activated:
poetry shell
Invoke the CLI using the fs7
command:
fs7 --help
Logfire project URL: https://logfire.pydantic.dev/seanchatmangpt/fastsocket
Server initialized for asgi.
Usage: fs7 [OPTIONS] COMMAND [ARGS]...
╭─ Options ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --install-completion Install completion for the current shell. │
│ --show-completion Show completion for the current shell, to copy it or customize the installation. │
│ --help Show this message and exit. │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ asyncapi Manage AsyncAPI files and generate documentation. │
│ list-events List all registered event names and their corresponding handler functions. │
│ log Manage log files and view logs. │
│ register-events-command Register event handlers from a specified directory. │
│ reload Trigger the FastSocket server to reload event handlers. │
│ start Start the FastSocket server. │
│ status Show the current status of the FastSocket server. │
│ stop Stop the FastSocket server gracefully. │
╰───────────────────────────────────────────────────────────────────────
fs7 start
- Description: Initializes and starts the FastAPI server with Socket.IO support. It also writes the server PID
to
fastsocket.pid
for process management. - Behavior:
- Checks if the server is already running.
- Starts the server using Uvicorn.
- Writes the server PID for tracking.
- Provides feedback on server status.
fs7 stop
- Description: Sends a termination signal to the running FastSocket server process, ensuring a graceful shutdown and resource cleanup.
- Behavior:
- Reads the PID from
fastsocket.pid
. - Sends a SIGTERM signal to the process.
- Waits for the process to terminate gracefully.
- Removes the PID file upon successful shutdown.
- Reads the PID from
fs7 status
- Description: Reports the current status of the FastSocket server, including the process ID if running.
- Behavior:
- Checks for the existence of
fastsocket.pid
. - Verifies if the process is active.
- Outputs the server status accordingly.
- Checks for the existence of
fs7 register-events-command --directory src/fastsocket/events
- Description: Scans the provided directory for event handler modules and registers them with the server.
- Behavior:
- Loads and registers all event handlers defined in the specified directory.
- Validates handler integrity and dependencies.
- Logs registration status and any encountered issues.
fs7 reload
- Description: Reloads the event handlers dynamically, allowing updates to event processing logic without server downtime.
- Behavior:
- Sends a POST request to the
/admin/reload-events
endpoint. - Triggers the server to re-import and register event handlers.
- Provides feedback on the success or failure of the reload operation.
- Sends a POST request to the
fs7 log view-logs --lines 100
- Description: Displays the last 100 lines from
logs/debug.log
, providing insights into server operations and event handling. - Options:
--lines
: Number of log lines to display (default: 100).
fs7 log clear-logs
- Description: Empties the
logs/debug.log
file, useful for resetting log data. - Behavior:
- Truncates the log file.
- Confirms the action upon completion.
fs7 log tail-logs
- Description: Streams new log entries as they are written, similar to the
tail -f
command. PressCtrl+C
to stop tailing. - Behavior:
- Continuously reads the log file.
- Outputs new log entries in real-time.
fs7 asyncapi --help
- Description: Commands related to managing AsyncAPI files and generating corresponding documentation.
- Subcommands:
export
: Generate documentation from AsyncAPI specifications.validate
: Validate AsyncAPI YAML files against the specification.
fs7 list-events
- Description: Displays a list of all events currently registered in the FastSocket server along with their corresponding handler functions.
- Behavior:
- Enumerates registered events.
- Outputs event names and associated handlers.
FastSocket exposes several API endpoints for managing events and monitoring server status. Below is a detailed reference for each endpoint, including request parameters, responses, and usage examples.
GET /api/events
- Description: Retrieves a list of all registered events along with their handler functions.
- Parameters:
api_key
(string, required): API access key for authentication.
- Response:
[
{
"event_id": "1",
"name": "user_signed_up",
"handler": "user_signed_up.handle_event"
},
{
"event_id": "2",
"name": "user_deleted",
"handler": "user_deleted.handle_event"
}
// ... other events
]
GET /api/events/{event_id}
- Description: Retrieves details of a specific event by its unique ID.
- Parameters:
event_id
(string, required): Unique identifier of the event.
- Response:
{
"event_id": "1",
"name": "user_signed_up",
"handler": "user_signed_up.handle_event"
}
POST /api/events
- Description: Triggers a specific event with the provided payload.
- Parameters:
name
(string, required): Name of the event to trigger.payload
(json, optional): Data associated with the event.
- Request Body:
{
"name": "user_signed_up",
"payload": {
"user_id": "12345",
"email": "user@example.com"
}
}
- Response:
{
"status": "success",
"message": "user_signed_up handled successfully."
}
POST /admin/reload-events
- Description: Reloads all event handlers without restarting the server.
- Security Note: This endpoint should be secured (e.g., via authentication) to prevent unauthorized access.
- Response:
{
"status": "success",
"message": "Event handlers reloaded successfully."
}
FastSocket employs Loguru for structured and efficient logging, ensuring comprehensive tracking of server operations, event handling, and CLI activities.
- Log File Path:
logs/debug.log
- Rotation Policy: Logs are rotated when they reach 1 MB in size.
- Retention Policy: Logs are retained for 10 days, after which they are automatically deleted.
- Log Levels: Supports standard log levels (DEBUG, INFO, WARNING, ERROR, CRITICAL).
FastSocket's CLI provides commands to manage and view logs efficiently.
fs7 log view-logs --lines 100
- Description: Displays the last 100 lines of the log file.
- Options:
--lines
(int): Number of log lines to display (default: 100).
fs7 log clear-logs
- Description: Clears all contents of the log file, effectively resetting it.
fs7 log tail-logs
- Description: Streams new log entries in real-time, similar to the Unix
tail -f
command. - Usage: Press
Ctrl+C
to stop tailing.
FastSocket integrates with Logfire to provide advanced log management and visualization capabilities.
- **Logfire Project URL **: https://logfire.pydantic.dev/seanchatmangpt/fastsocket
- Features:
- Real-time log streaming.
- Search and filter logs based on severity and keywords.
- Visual dashboards for monitoring log trends and anomalies.
AsyncAPI serves as the cornerstone for defining the event-driven architecture of FastSocket. It provides a standardized specification for asynchronous APIs, facilitating seamless integration between backend event handlers and frontend components.
- Standardization: Ensures consistent event definitions across the system.
- Automated Code Generation: Enables the automatic generation of event handlers and frontend components based on the specification.
- Documentation: Generates comprehensive documentation, enhancing maintainability and developer onboarding.
- Validation: Ensures that event payloads conform to predefined schemas, reducing runtime errors.
- Define Events: Specify event channels, messages, and payloads in the
asyncapi.yaml
file. - Generate Handlers and Components: Utilize the CLI commands to generate Python event handlers and Vue.js frontend components based on the AsyncAPI specification.
- Register Handlers: Automatically register generated handlers with the FastAPI and Socket.IO server.
- Synchronize Frontend: Frontend components interact with the backend via Socket.IO, adhering to the event definitions in AsyncAPI.
asyncapi: '2.6.0'
info:
title: FastSocket API
version: '1.0.0'
channels:
user_signed_up:
publish:
summary: User Signed Up Event
message:
contentType: application/json
payload:
type: object
properties:
user_id:
type: string
email:
type: string
user_deleted:
publish:
summary: User Deleted Event
message:
contentType: application/json
payload:
type: object
properties:
user_id:
type: string
FastSocket dynamically registers event handlers based on the AsyncAPI specification, ensuring that each event has a corresponding backend handler and frontend component.
- Functionality:
- Parses AsyncAPI definitions.
- Dynamically imports and registers event handler modules.
- Supports hot-reloading of event handlers to facilitate real-time updates without server restarts.
- Key Components:
- Dynamic Importing: Uses
importlib
to load event handler modules at runtime. - Pydantic Integration: Utilizes Pydantic models for strict data validation of incoming event payloads.
- Socket.IO Integration: Registers asynchronous event handlers with the Socket.IO server for real-time communication.
- Dynamic Importing: Uses
from pydantic import BaseModel
from loguru import logger
class UserSignedUpPayload(BaseModel):
user_id: str
email: str
async def handle_event(sid: str, data: UserSignedUpPayload, sio):
logger.info(f"Handling user_signed_up for SID '{sid}' with data: {data.model_dump()}")
# Business logic: e.g., store user in database
await sio.emit("user_signed_up_ack", {"status": "success", "message": "User signed up successfully"}, room=sid)
The CLI, built with Typer, offers an intuitive interface for managing server operations, event handlers, and logs.
- Commands:
start
: Launches the FastSocket server.stop
: Gracefully shuts down the server.status
: Checks if the server is running.register-events-command
: Registers event handlers from a specified directory.reload
: Triggers the server to reload event handlers without restarting.log
: Subcommands for managing logs (view-logs
,clear-logs
,tail-logs
).asyncapi
: Manage AsyncAPI files and generate documentation.list-events
: Lists all registered events and their handlers.
@app.command()
def start(host: str = DEFAULT_HOST, port: int = DEFAULT_PORT):
"""
Start the FastSocket server.
"""
if is_server_running():
logger.error("FastSocket server is already running.")
raise typer.Exit(code=1)
logger.info(f"Starting FastSocket server on {host}:{port}")
# Start the server as a subprocess using Uvicorn
process = subprocess.Popen(
[sys.executable, "-m", "uvicorn", "fastsocket.api:app", "--host", host, "--port", str(port)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
# Write the PID to the PID file
write_pid(process.pid)
logger.info(f"FastSocket server started with PID {process.pid}")
# Optionally, wait and check if the server is running
time.sleep(2)
if process.poll() is None:
logger.success(f"FastSocket server is running on {host}:{port}")
else:
stdout, stderr = process.communicate()
logger.error(f"FastSocket server failed to start.\nSTDOUT: {stdout}\nSTDERR: {stderr}")
remove_pid()
raise typer.Exit(code=1)
FastSocket employs Loguru for structured logging, capturing detailed information about server operations, event handling, and CLI activities.
from loguru import logger
import sys
# Remove the default logger to prevent duplicate logs
logger.remove()
# Configure Loguru to log to stdout and a file with rotation and retention
logger.add(sys.stdout, format="{time} {level} {message}", level="INFO")
logger.add("logs/debug.log", rotation="1 MB", level="DEBUG", retention="10 days")
- **Logfire Project URL **: https://logfire.pydantic.dev/seanchatmangpt/fastsocket
- Functionality:
- Real-time log streaming and visualization.
- Search, filter, and analyze logs based on severity and keywords.
- Integration with the CLI for seamless log management.
FastSocket utilizes ReactiveX (RxPY) to handle complex event streams and reactive data flows, enabling responsive and scalable event processing.
- RxPY Subjects: Acts as bridges for event streams, allowing for multicasting and reactive transformations.
- Subscriptions: Frontend components can subscribe to event observables, enabling real-time data updates and interactions.
import reactivex as rx
from loguru import logger
# Subject to act as a bridge for event streams
event_subjects = {}
def create_event_stream(event_name: str):
if event_name not in event_subjects:
event_subjects[event_name] = rx.Subject()
return event_subjects[event_name]
# Example: Emitting an event
def emit_event(event_name: str, data: dict):
logger.debug(f"Emitting event '{event_name}' with data: {data}")
stream = create_event_stream(event_name)
stream.on_next(data)
# Example: Subscribing to an event
def subscribe_to_event(event_name: str, callback):
stream = create_event_stream(event_name)
subscription = stream.subscribe(
on_next=lambda data: callback(data),
on_error=lambda e: logger.error(f"Error in event '{event_name}': {e}")
)
return subscription
FastSocket automates the generation of backend event handlers and frontend Vue components based on AsyncAPI specifications, ensuring consistency and reducing manual coding efforts.
Defines a Pydantic model for data validation and an asynchronous event handler function.
from pydantic import BaseModel
from loguru import logger
# Define Pydantic model for validation
class {{operation_name | camelize}}Payload(BaseModel):
{ %
for field, type in fields.items() %}
{{field}}: {{type}}
{ % endfor %}
# Event handler function
async def handle_event(sid: str, data: {{operation_name | camelize}}Payload, sio
):
logger.info(f"Handling {{ operation_name }} for SID '{sid}' with data: {data.model_dump()}")
# Add your logic here, such as updating database records, etc.
await sio.emit("{{ operation_name }}_ack",
{"status": "success",
"message": "{{ operation_name }} handled successfully"},
room=sid)
Creates a Vue component with a button to trigger events and displays received data.
<template>
<div>
<button @click="handle{{ operation_name | camelize }}">Send {{ operation_name | camelize }}</button>
<div v-if="receivedData">Callback Data: {{ receivedData }}</div>
<div v-if="observedData">Observable Data: {{ observedData }}</div>
</div>
</template>
<script setup>
import {ref, onUnmounted} from 'vue';
import {useOperation} from '@/composables/useOperation';
const {send
{
{
operation_name | camelize
}
}
,
receive
{
{
operation_name | camelize
}
}
,
observe
{
{
operation_name | camelize
}
}
}
= useOperation('{{ operation_name }}');
const receivedData = ref(null);
const observedData = ref(null);
// Callback-based receive example
const unsubscribeCallback = receive
{
{
operation_name | camelize
}
}
((data) => {
receivedData.value = data;
console.log('Received data (callback):', data);
});
// Observable-based receive example
const {observable, unsubscribe: unsubscribeObservable} = observe
{
{
operation_name | camelize
}
}
();
const subscription = observable.subscribe((data) => {
observedData.value = data;
console.log('Received data (observable):', data);
});
// Send data to server
const handle
{
{
operation_name | camelize
}
}
= async () => {
try {
const response = await send
{
{
operation_name | camelize
}
}
({
{%
for field in fields %
}
{
{
field
}
}
:
'{{ field | default }}'
{
{
','
if not loop.last
}
}
{%
endfor %
}
})
;
console.log('Server acknowledgment:', response);
} catch (error) {
console.error('Error:', error);
}
};
// Cleanup on component unmount
onUnmounted(() => {
unsubscribeCallback();
unsubscribeObservable();
subscription.unsubscribe();
});
</script>
fs7 handlers --asyncapi-file path/to/asyncapi.yaml --output-dir src/fastsocket/events
- Description: Parses the specified AsyncAPI YAML file and generates corresponding Python event handler files in the designated directory.
- Behavior:
- Reads the AsyncAPI specification.
- Extracts event definitions and payload schemas.
- Renders Python handler templates with Pydantic models.
- Saves the generated handlers to the output directory.
fs7 pages --asyncapi-file path/to/asyncapi.yaml --output-dir path/to/nuxt/pages
- Description: Parses the specified AsyncAPI YAML file and generates corresponding Vue.js component files in the designated directory.
- Behavior:
- Reads the AsyncAPI specification.
- Extracts event definitions and payload schemas.
- Renders Vue component templates.
- Saves the generated components to the output directory.
FastSocket is designed for seamless deployment using containerization technologies like Docker and orchestration tools like Docker Compose. This ensures consistent environments across development, testing, and production, facilitating scalability and maintainability.
The Dockerfile defines the container image for FastSocket, ensuring all dependencies are included and the application runs consistently.
# Use official Python image as base
FROM python:3.13-slim
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# Set work directory
WORKDIR /app
# Install system dependencies
RUN apt-get update && apt-get install -y build-essential
# Install Poetry
RUN pip install --upgrade pip
RUN pip install poetry
# Copy project files
COPY pyproject.toml poetry.lock /app/
RUN poetry config virtualenvs.create false
RUN poetry install --no-dev
COPY . /app/
# Expose port
EXPOSE 8000
# Start the application
CMD ["uvicorn", "src.fastsocket.api:app", "--host", "0.0.0.0", "--port", "8000"]
Docker Compose orchestrates multiple services, such as the FastSocket server and any additional dependencies like databases or log management services.
version: '3.8'
services:
fastsocket:
build: .
container_name: fastsocket
ports:
- "8000:8000"
volumes:
- .:/app
- ./logs:/app/logs
environment:
- API_KEY=${API_KEY}
- LOG_LEVEL=${LOG_LEVEL}
- HOST=0.0.0.0
- PORT=8000
restart: unless-stopped
# Example: Logfire service (if required)
logfire:
image: logfire/logfire:latest
container_name: logfire
ports:
- "9000:9000"
volumes:
- ./logs:/var/log/fastsocket
environment:
- LOG_DIR=/var/log/fastsocket
restart: unless-stopped
-
Build and Run Containers:
docker-compose up --build
-
Access Services:
- FastAPI Server:
http://localhost:8000
- Logfire Viewer:
http://localhost:9000
(if integrated)
- FastAPI Server:
-
Environment Variables:
Ensure that a
.env
file exists with the necessary environment variables:API_KEY=<your_api_key> LOG_LEVEL=DEBUG
-
Scaling Services:
Docker Compose allows scaling services as needed:
docker-compose up --scale fastsocket=3 --build
This command scales the FastSocket service to three instances, enhancing performance and reliability.
FastSocket incorporates a comprehensive test suite to ensure reliability, correctness, and robustness. Utilizing * Pytest*, the tests cover API endpoints, CLI commands, and module integrity.
Activate the virtual environment and execute the test suite:
poetry shell
pytest tests/
-
API Endpoints (
test_api.py
):- Validates the correctness of API responses.
- Ensures proper handling of event triggers and data validation.
- Tests error responses and edge cases.
-
CLI Commands (
test_cli.py
):- Tests the functionality of each CLI command.
- Verifies server start, stop, reload operations.
- Ensures log management commands work as intended.
-
Module Imports (
test_import.py
):- Ensures all modules and event handlers can be imported without errors.
- Validates the integrity of dynamically generated code.
import pytest
from fastapi.testclient import TestClient
from src.fastsocket.api import app
client = TestClient(app)
def test_get_events():
response = client.get("/api/events", headers={"api_key": "test_key"})
assert response.status_code == 200
assert isinstance(response.json(), list)
def test_trigger_event():
event_payload = {
"name": "user_signed_up",
"payload": {
"user_id": "12345",
"email": "user@example.com"
}
}
response = client.post("/api/events", json=event_payload, headers={"api_key": "test_key"})
assert response.status_code == 200
assert response.json()["status"] == "success"
FastSocket integrates several optimizations to enhance performance, scalability, and developer experience.
Utilizes FastAPI's asynchronous capabilities and Socket.IO's async support to handle multiple concurrent connections efficiently, ensuring non-blocking operations and high throughput.
Implements a mechanism to reload event handlers dynamically without necessitating a server restart. This is achieved
through the /admin/reload-events
endpoint and the reload
CLI command, allowing for rapid development and deployment
cycles.
Employs Loguru's advanced logging features, including log rotation and retention policies, to manage log files effectively, preventing disk space exhaustion and ensuring log data is organized and accessible.
Incorporates ReactiveX (RxPY) to manage complex event streams and reactive data flows, enabling responsive and scalable event processing.
Facilitates consistent and reproducible deployment environments through Docker and Docker Compose, enhancing scalability and simplifying infrastructure management.
Adopts a modular project structure, promoting maintainability and scalability. Event handlers are organized into separate modules, and the CLI commands are compartmentalized, ensuring clean code organization.
FastSocket is an evolving project with plans to incorporate additional features and improvements to enhance its capabilities and usability.
-
Frontend Dashboard:
- Develop a Nuxt.js-based dashboard for monitoring events, managing handlers, and visualizing real-time data streams.
- Integrate with Logfire for advanced log analytics within the dashboard.
-
Authentication and Authorization:
- Implement JWT-based authentication for securing API endpoints and Socket.IO connections.
- Introduce role-based access controls to manage user permissions.
-
Advanced Log Filtering and Search:
- Enhance log viewing capabilities with dynamic filtering by severity levels, keywords, and timestamps.
- Implement full-text search within logs for efficient troubleshooting.
-
Multi-Tenancy Support:
- Allow multiple isolated environments within the same FastSocket instance, catering to different client groups or application segments.
-
Enhanced Event Handling:
- Support for more complex event processing, such as event chaining, transformations, and conditional handling.
- Integrate with external services for enriched event workflows.
-
Performance Monitoring and Metrics:
- Incorporate monitoring tools to track server performance, event processing times, and system health.
- Provide real-time metrics dashboards for operational insights.
-
Automated Documentation Generation:
- Leverage AsyncAPI to automatically generate comprehensive documentation for both backend and frontend components.
- Integrate with documentation platforms like Swagger UI for enhanced accessibility.
Contributions are highly encouraged to help improve FastSocket. Whether it's reporting bugs, suggesting features, or contributing code, your involvement is invaluable.
-
Fork the Repository
Click the "Fork" button at the top-right corner of the repository page to create your own fork.
-
Clone Your Fork
git clone https://github.com/your-username/fastsocket.git cd fastsocket
-
Create a Feature Branch
git checkout -b feature/your-feature-name
-
Commit Your Changes
git commit -m "Add your descriptive commit message"
-
Push to Your Fork
git push origin feature/your-feature-name
-
Open a Pull Request
Navigate to the original repository and click on "Compare & pull request" to submit your changes for review.
Please adhere to the Code of Conduct to ensure a welcoming and respectful environment for all contributors.
If you encounter any issues or have feature requests, please open an issue in the Issue Tracker.
This project is licensed under the MIT License.
Sean Chatman (He/Him)
Full Stack Developer specializing in Front End Generative AI Web Development with expertise in TypeScript, *
React*, Vue, and Python.
- Portfolio: https://seanchatmangpt.com
- Email: sean@example.com
- LinkedIn: linkedin.com/in/seanchatmangpt
- GitHub: github.com/seanchatmangpt
FastSocket integrates with Logfire to provide real-time log management and visualization, enhancing observability and facilitating efficient debugging.
- **Logfire Project URL **: https://logfire.pydantic.dev/seanchatmangpt/fastsocket
- Features:
- Real-time log streaming.
- Search and filter logs based on severity levels and keywords.
- Visual dashboards for monitoring log trends and anomalies.
Navigate to the Logfire project URL to access the log viewer interface. Utilize the CLI commands to manage and interact with logs seamlessly.
fs7 log view-logs --lines 100
FastSocket is engineered for high performance and scalability, ensuring efficient handling of a large volume of real-time events and simultaneous client connections.
Leveraging FastAPI's asynchronous capabilities and Socket.IO's non-blocking event handling, FastSocket can manage numerous concurrent connections without compromising on responsiveness.
Implementing ReactiveX (RxPY) allows FastSocket to handle complex event streams with ease, enabling transformations, filtering, and aggregation of events in a declarative manner. This approach facilitates scalable and maintainable event processing pipelines.
By containerizing the application with Docker and orchestrating services with Docker Compose, FastSocket ensures consistent deployment environments and simplifies horizontal scaling across multiple instances.
For further scalability, integrating load balancers (e.g., Nginx, HAProxy) can distribute incoming traffic across multiple FastSocket instances, enhancing performance and reliability.
Comprehensive documentation is available to guide developers through the setup, usage, and extension of FastSocket.
- Getting Started: Step-by-step instructions to set up and run FastSocket locally.
- API Reference: Detailed documentation of all API endpoints, parameters, and responses.
- CLI Usage: Guide to utilizing the Typer-powered CLI for server and log management.
- Event Handling: Instructions on defining, registering, and managing event handlers using AsyncAPI.
- Logging: Overview of Loguru configuration and Logfire integration for log management.
- Deployment: Strategies and configurations for deploying FastSocket using Docker and Docker Compose.
- Testing: Guidelines for running and extending the test suite to ensure application reliability.
Refer to the Documentation for in-depth guides and references.
Utilize the CLI to automate the generation of backend handlers and frontend components based on your AsyncAPI specification.
# Generate Python event handlers
fs7 handlers --asyncapi-file asyncapi.yaml --output-dir src/fastsocket/events
# Generate Vue.js frontend components
fs7 pages --asyncapi-file asyncapi.yaml --output-dir nuxt-app/pages
Send a POST request to trigger an event and observe the real-time handling.
curl -X POST "http://localhost:8000/api/events" \
-H "Content-Type: application/json" \
-H "api_key: your_api_key" \
-d '{
"name": "user_signed_up",
"payload": {
"user_id": "12345",
"email": "user@example.com"
}
}'
Tail the log file to monitor server operations and event handling in real-time.
fs7 log tail-logs
Developing FastSocket provided profound insights into integrating multiple cutting-edge technologies to build a cohesive, scalable, and efficient real-time application framework.
-
Event-Driven Architecture: The integration of AsyncAPI with Socket.IO and FastAPI underscores the importance of standardized event definitions in maintaining consistency and scalability.
-
Asynchronous Programming: Leveraging Python's
async
andawait
keywords in FastAPI and Socket.IO enhances the application's ability to handle numerous concurrent connections efficiently. -
Reactive Programming Paradigms: Implementing RxPY introduced a robust framework for managing complex event streams, enabling responsive and maintainable data flows.
-
Automated Code Generation: Utilizing templating engines like Jinja2 in conjunction with AsyncAPI automates repetitive coding tasks, reducing errors and accelerating development cycles.
-
Structured Logging: Adopting Loguru for logging provided an effective mechanism for capturing detailed, structured logs, essential for monitoring and debugging.
-
CLI Development: Building a CLI with Typer streamlined server management and operational workflows, enhancing developer productivity and operational efficiency.
-
Containerization: Docker and Docker Compose facilitated consistent deployment environments, simplifying the transition from development to production.
-
Scalability Considerations: Designing FastSocket with scalability in mind ensures that it can handle increased loads and evolving application demands without compromising performance.
-
Dynamic Event Handling: Ensuring that event handlers could be dynamically registered and reloaded required meticulous management of module imports and Socket.IO integrations.
-
Data Validation: Implementing Pydantic models for strict data validation necessitated a thorough understanding of schema definitions and their translation from AsyncAPI specifications.
-
Real-Time Log Management: Integrating Logfire for real-time log viewing involved configuring log streams and ensuring seamless communication between the application and the logging platform.
FastSocket is poised for continual enhancement, with several key areas earmarked for future development:
-
Enhanced Frontend Dashboard:
- Develop a comprehensive Nuxt.js-based dashboard for monitoring events, managing handlers, and visualizing real-time data streams.
- Integrate with Logfire for in-dashboard log analytics and visualization.
-
Advanced Authentication Mechanisms:
- Implement OAuth2 and JWT-based authentication to secure API endpoints and Socket.IO connections.
- Introduce role-based access controls for granular permission management.
-
Distributed Event Handling:
- Scale event processing across multiple server instances using message brokers like RabbitMQ or Kafka.
- Implement load balancing and failover strategies to ensure high availability.
-
GraphQL Integration:
- Introduce GraphQL endpoints to provide flexible and efficient data querying capabilities alongside RESTful APIs.
-
Comprehensive Monitoring and Metrics:
- Integrate monitoring tools (e.g., Prometheus, Grafana) to track application performance, event processing times, and system health.
- Provide real-time metrics dashboards for operational insights.
-
Automated Documentation Generation:
- Leverage AsyncAPI and Swagger/OpenAPI specifications to automatically generate comprehensive documentation for both backend and frontend components.
- Implement interactive API explorers for enhanced developer accessibility.
-
Continuous Integration and Deployment (CI/CD):
- Set up CI/CD pipelines to automate testing, building, and deployment processes, ensuring rapid and reliable delivery of updates.
-
Multi-Tenancy Support:
- Enable support for multiple isolated environments within the same FastSocket instance, catering to diverse client groups or application segments.
The development of FastSocket underscores the intersection of modern web development practices with advanced architectural patterns and programming paradigms. The integration of Reactive Programming through RxPY and * Event-Driven Architecture* via AsyncAPI demonstrates the practical application of theoretical concepts in building scalable and responsive systems.
-
Scalability and Performance: FastSocket exemplifies how asynchronous frameworks and reactive programming can be harnessed to build systems capable of handling high concurrency with minimal latency.
-
Standardization in Event-Driven Systems: The adoption of AsyncAPI as a standard for defining event channels and messages highlights the importance of standardized specifications in facilitating interoperability and reducing integration complexities.
-
Automated Code Generation: Leveraging templating engines for code generation based on formal specifications ( AsyncAPI) bridges the gap between design and implementation, fostering consistency and reducing manual coding errors.
-
Reactive Systems: Incorporating RxPY introduces reactive paradigms into real-time systems, enabling sophisticated event processing capabilities that align with modern software engineering principles.
-
Optimizing Reactive Streams: Investigate methodologies to optimize event stream processing in high-load scenarios using ReactiveX principles.
-
Formal Verification of Event Handlers: Explore techniques for formally verifying the correctness and reliability of dynamically generated event handlers.
-
Enhanced Security Protocols: Assess and implement advanced security measures within event-driven architectures to safeguard against emerging threats.
- FastAPI Documentation: https://fastapi.tiangolo.com/
- Socket.IO Documentation: https://socket.io/docs/v4/
- AsyncAPI Specification: https://www.asyncapi.com/docs
- Pydantic Documentation: https://pydantic-docs.helpmanual.io/
- Typer Documentation: https://typer.tiangolo.com/
- Logfire Documentation: https://logfire.pydantic.dev/docs
- RxPY Documentation: https://rxpy.readthedocs.io/en/latest/
- Poetry Documentation: https://python-poetry.org/docs/
- Docker Documentation: https://docs.docker.com/
- Docker Compose Documentation: https://docs.docker.com/compose/
- Pytest Documentation: https://docs.pytest.org/en/stable/
- Nuxt.js Documentation: https://nuxtjs.org/docs/
- Vue.js Documentation: https://vuejs.org/v2/guide/
- **ReactiveX Documentation **: http://reactivex.io/documentation/operators.html
FastSocket is the culmination of collaborative efforts and the integration of various open-source technologies. Special thanks to the developers and communities behind FastAPI, Socket.IO, AsyncAPI, Pydantic, Typer, Logfire, and RxPY for providing the tools and documentation that made this project possible.
This project is licensed under the MIT License.
Sean Chatman (He/Him)
Full Stack Developer specializing in Front End Generative AI Web Development with expertise in TypeScript, *
React*, Vue, and Python.
- Portfolio: https://seanchatmangpt.com
- Email: sean@example.com
- LinkedIn: linkedin.com/in/seanchatmangpt
- GitHub: github.com/seanchatmangpt
This comprehensive README encapsulates the technical depth and sophisticated architecture of FastSocket, providing a clear roadmap for developers, contributors, and stakeholders to understand, utilize, and extend the project. The emphasis on advanced technologies and architectural principles positions FastSocket as a robust solution for real-time, event-driven applications.