Overview
WSX is designed with a modular architecture that separates concerns and enables easy integration with different frameworks. This document explains the core components and how they work together.Core Components
WSX Client
The browser-side JavaScript library that handles:- WebSocket connection management
- DOM event listening and processing
- HTML swapping and updates
- Reconnection logic
- Trigger processing (throttling, debouncing, etc.)
WSX Server
The Node.js server component that manages:- WebSocket connections
- Request routing and handling
- Response generation
- Broadcasting to clients
- Connection lifecycle management
WSX Adapter
Framework-specific adapters that provide:- WebSocket server setup
- Framework integration
- Request/response handling
- Connection management hooks
Request Flow
Here’s how a typical WSX request flows through the system:Component Details
WSX Client Architecture
The client is built around several key classes:WSX Main Class
- Manages WebSocket connection
- Handles reconnection logic
- Processes incoming messages
- Manages pending requests
Event System
- Listens for DOM events
- Processes trigger specifications
- Handles modifier logic (throttle, debounce, etc.)
- Manages element state
Swap Engine
- Parses swap specifications
- Handles DOM manipulation
- Manages timing and animation
- Processes out-of-band updates
WSX Server Architecture
The server uses a clean, event-driven architecture:WSXServer Class
- Central connection manager
- Handler registration and routing
- Broadcasting capabilities
- Connection lifecycle management
Connection Management
- Each WebSocket connection is wrapped in a
WSXConnection
object - Connections are tracked in a Map for efficient lookup
- Session data can be attached to connections
Handler System
- Handlers are registered with names or as catch-all functions
- Handlers receive request and connection objects
- Handlers can return single responses or arrays
Adapter System
Adapters provide framework-specific integration:Express Adapter
Hono Adapter
Data Flow
Request Processing
- Client Event: User interacts with an element that has WSX attributes
- Trigger Processing: Client evaluates trigger conditions and modifiers
- Request Construction: Client builds a WSXRequest object
- WebSocket Send: Request is serialized and sent over WebSocket
- Server Routing: Server routes request to appropriate handler
- Handler Execution: Handler processes request and returns response
- Response Processing: Server processes response and sends back to client(s)
- DOM Update: Client applies response to DOM using swap specifications
Broadcasting
WSX supports several broadcasting patterns:Broadcast to All
Send to Specific Connection
Out-of-Band Updates
Security Considerations
Connection Security
- All WebSocket connections are validated
- Connection IDs are generated securely
- Session data is isolated per connection
Input Validation
- All client requests should be validated on the server
- HTML content should be sanitized when necessary
- Rate limiting can be implemented at the handler level
CORS and Origins
- WebSocket connections respect same-origin policies
- Configure allowed origins in production environments
- Use secure WebSocket (wss://) in production
Performance Characteristics
Client Performance
- Minimal JavaScript footprint (~15KB gzipped)
- Efficient DOM manipulation
- Smart reconnection logic
- Optimized event handling
Server Performance
- Lightweight connection management
- Fast request routing
- Efficient broadcasting
- Minimal memory footprint per connection
Scalability
- Horizontal scaling through load balancing
- Connection state is per-server instance
- Broadcasting can be extended with Redis/message queues
Extension Points
Custom Adapters
Create adapters for other frameworks:Middleware
Add middleware to the request pipeline:Custom Triggers
Extend the client with custom trigger types:Best Practices
Architecture Patterns
- Handler Organization: Group related handlers logically
- State Management: Use connection session data for user state
- Error Handling: Implement comprehensive error handling
- Broadcasting Strategy: Use targeted broadcasting when possible
Performance Optimization
- Connection Pooling: Reuse connections efficiently
- Response Caching: Cache responses when appropriate
- Batch Updates: Use out-of-band updates for multiple DOM changes
- Resource Cleanup: Properly clean up connections and timers
Security Hardening
- Input Validation: Validate all client inputs
- Rate Limiting: Implement per-connection rate limits
- Content Security: Sanitize HTML content
- Authentication: Implement proper authentication flows