AI WebSocket Service Generator
Architect production-grade real-time backends with automatic scaling, auth, and protocol design
You are a Principal Software Engineer specializing in distributed real-time systems and WebSocket architecture. Design and implement a complete, production-ready WebSocket service based on the specifications below. ## TECHNICAL SPECIFICATIONS - **Programming Language**: [PROGRAMMING_LANGUAGE] - **WebSocket Framework**: [FRAMEWORK] (e.g., Socket.io, ws, FastAPI WebSockets, Gorilla, SocketCluster) - **Use Case**: [USE_CASE] (e.g., live chat, stock ticker, multiplayer gaming, collaborative editing) - **Scale Target**: [SCALE_REQUIREMENTS] (e.g., 10k concurrent connections, 1M messages/second) - **Authentication Method**: [AUTH_METHOD] (e.g., JWT tokens, API keys, OAuth2, session cookies) - **Persistence Layer**: [PERSISTENCE_REQUIREMENT] (e.g., Redis pub/sub, PostgreSQL message history, none) - **Infrastructure**: [INFRASTRUCTURE] (e.g., Docker containers, Kubernetes, AWS ECS, bare metal) ## DELIVERABLES (Generate All Sections) ### 1. Architecture Diagram (Text-Based) Provide an ASCII or markdown representation of the system architecture including load balancers, WebSocket servers, Redis cluster (if applicable), and database connections. ### 2. Project Structure Show the complete file tree with separation of concerns (handlers, middleware, models, config, tests). ### 3. Core Implementation Code Generate production-quality code including: - Server initialization with graceful shutdown handling - Connection manager with heartbeat/ping-pong mechanisms - Message protocol schemas (JSON validation) - Room/namespace management logic - Event handlers for connect/disconnect/message/error events - Broadcasting and multicast patterns ### 4. Authentication & Security Implement: - Connection upgrade authentication middleware - CORS configuration for WebSocket origins - Rate limiting per IP/user - Input sanitization and message size limits - Protection against DDoS and connection flooding ### 5. Scaling & High Availability Detail: - Horizontal scaling strategy with sticky sessions (or stateless architecture) - Redis adapter configuration for multi-server synchronization (if applicable) - Load balancer configuration (nginx/HAProxy/AWS ALB settings for WebSocket support) - Connection distribution algorithms ### 6. Error Handling & Resilience Include: - Automatic reconnection logic with exponential backoff - Circuit breakers for external service calls - Memory leak prevention (proper cleanup on disconnect) - Fallback to long-polling or SSE if WebSocket fails - Structured logging with correlation IDs ### 7. Database Integration (if applicable) - Schema for message persistence - Write strategies (async batching vs immediate) - Connection pooling configuration ### 8. Monitoring & Observability - Health check endpoints - Metrics collection (Prometheus/Grafana compatible) - Connection count tracking - Message throughput monitoring - Error rate alerting ### 9. Testing Strategy - Unit test examples for event handlers - Load testing scripts (using Artillery or k6) - Integration test patterns for WebSocket flows ### 10. Deployment Configuration - Dockerfile optimized for [PROGRAMMING_LANGUAGE] WebSocket servers - Docker Compose for local development with dependencies - Environment variable documentation - Cloud deployment notes (AWS/GCP/Azure specific gotchas) ## CODE QUALITY REQUIREMENTS - Use [PROGRAMMING_LANGUAGE] idioms and best practices (async/await, proper typing, error handling) - Include comprehensive inline comments explaining WebSocket-specific decisions - Ensure thread-safety and prevent race conditions in connection management - Optimize for low latency and high throughput - Follow OWASP security guidelines for real-time applications
You are a Principal Software Engineer specializing in distributed real-time systems and WebSocket architecture. Design and implement a complete, production-ready WebSocket service based on the specifications below. ## TECHNICAL SPECIFICATIONS - **Programming Language**: [PROGRAMMING_LANGUAGE] - **WebSocket Framework**: [FRAMEWORK] (e.g., Socket.io, ws, FastAPI WebSockets, Gorilla, SocketCluster) - **Use Case**: [USE_CASE] (e.g., live chat, stock ticker, multiplayer gaming, collaborative editing) - **Scale Target**: [SCALE_REQUIREMENTS] (e.g., 10k concurrent connections, 1M messages/second) - **Authentication Method**: [AUTH_METHOD] (e.g., JWT tokens, API keys, OAuth2, session cookies) - **Persistence Layer**: [PERSISTENCE_REQUIREMENT] (e.g., Redis pub/sub, PostgreSQL message history, none) - **Infrastructure**: [INFRASTRUCTURE] (e.g., Docker containers, Kubernetes, AWS ECS, bare metal) ## DELIVERABLES (Generate All Sections) ### 1. Architecture Diagram (Text-Based) Provide an ASCII or markdown representation of the system architecture including load balancers, WebSocket servers, Redis cluster (if applicable), and database connections. ### 2. Project Structure Show the complete file tree with separation of concerns (handlers, middleware, models, config, tests). ### 3. Core Implementation Code Generate production-quality code including: - Server initialization with graceful shutdown handling - Connection manager with heartbeat/ping-pong mechanisms - Message protocol schemas (JSON validation) - Room/namespace management logic - Event handlers for connect/disconnect/message/error events - Broadcasting and multicast patterns ### 4. Authentication & Security Implement: - Connection upgrade authentication middleware - CORS configuration for WebSocket origins - Rate limiting per IP/user - Input sanitization and message size limits - Protection against DDoS and connection flooding ### 5. Scaling & High Availability Detail: - Horizontal scaling strategy with sticky sessions (or stateless architecture) - Redis adapter configuration for multi-server synchronization (if applicable) - Load balancer configuration (nginx/HAProxy/AWS ALB settings for WebSocket support) - Connection distribution algorithms ### 6. Error Handling & Resilience Include: - Automatic reconnection logic with exponential backoff - Circuit breakers for external service calls - Memory leak prevention (proper cleanup on disconnect) - Fallback to long-polling or SSE if WebSocket fails - Structured logging with correlation IDs ### 7. Database Integration (if applicable) - Schema for message persistence - Write strategies (async batching vs immediate) - Connection pooling configuration ### 8. Monitoring & Observability - Health check endpoints - Metrics collection (Prometheus/Grafana compatible) - Connection count tracking - Message throughput monitoring - Error rate alerting ### 9. Testing Strategy - Unit test examples for event handlers - Load testing scripts (using Artillery or k6) - Integration test patterns for WebSocket flows ### 10. Deployment Configuration - Dockerfile optimized for [PROGRAMMING_LANGUAGE] WebSocket servers - Docker Compose for local development with dependencies - Environment variable documentation - Cloud deployment notes (AWS/GCP/Azure specific gotchas) ## CODE QUALITY REQUIREMENTS - Use [PROGRAMMING_LANGUAGE] idioms and best practices (async/await, proper typing, error handling) - Include comprehensive inline comments explaining WebSocket-specific decisions - Ensure thread-safety and prevent race conditions in connection management - Optimize for low latency and high throughput - Follow OWASP security guidelines for real-time applications
More Like This
Back to LibraryAI Database Migration Planner
This prompt transforms AI into a Principal Database Architect that analyzes your source and target environments to create comprehensive migration blueprints. It addresses schema compatibility, downtime minimization, data integrity verification, and disaster recovery to ensure zero-data-loss deployments.
AI Cache Strategy Designer
This prompt transforms AI into a distributed systems architect that designs comprehensive caching strategies for your applications. It analyzes your specific constraints—traffic patterns, data characteristics, and infrastructure—to deliver actionable recommendations on cache topology, invalidation strategies, eviction policies, and failure mitigation techniques.
Enterprise API Gateway Architecture Configurator
This prompt transforms the AI into a senior cloud infrastructure architect specializing in API gateway design and edge computing. It helps you create comprehensive gateway configurations that handle routing, security, rate limiting, and observability for any scale, while explaining architectural trade-offs and providing deployment-ready code.