System Architecture
The microservice consists of two primary components:- Express Server: Handles API requests for manually triggering account processing
- Cron Job: Automatically processes all connected accounts at regular intervals
Technology Stack
The microservice leverages a modern Node.js ecosystem:Core Technologies
- Node.js (v18+): JavaScript runtime environment
- TypeScript (v5.5+): Typed superset of JavaScript for robust code
- Express.js (v4.19+): Fast, unopinionated web framework
- PM2 (v5+): Advanced production process manager with integrated load balancer
Web Scraping & Automation
- Puppeteer (v22+): Headless Chrome API for automated web interactions
- Puppeteer-core: Lightweight version with reduced footprint
Database & Storage
- Supabase: Open source Firebase alternative
- PostgreSQL: Advanced relational database for structured data storage
Queue & Task Management
- Better-Queue: Priority queue with concurrency control
- Multi-Queue System: Load balancing across parallel queues
Logging & Monitoring
- Winston: Versatile logging library with multiple transports
- Morgan: HTTP request logger middleware for Express
Data Flow Process
The microservice collects data through the following process:Implementation Details
Authentication Mechanism
The microservice uses browser storage data (cookies, localStorage, sessionStorage) collected from users’ devices to authenticate with OnlyFans:Queue Management
The service implements a multi-queue system for concurrent processing:API Endpoints
Endpoint | Method | Description |
---|---|---|
/api/test | GET | Health check endpoint to verify service status |
/api/process-account/:accountId | POST | Manually trigger processing for a specific account |
Environment Variables
The microservice relies on the following environment variables for configuration:Development Environment Setup
To set up the microservice for local development:-
Clone the repository
-
Install dependencies
-
Configure environment variables
Create a
.env
file with the required variables: -
Generate Supabase type definitions
-
Build the TypeScript code
-
Start the service in development mode
Production Deployment
For production deployment, we use PM2 to manage the application processes:-
Build the application
-
Configure PM2
The
ecosystem.config.js
file contains the PM2 configuration: -
Start the application with PM2
-
Configure PM2 to start on system boot
-
Monitor the application
Cron Job Configuration
The cron job automatically processes all accounts at regular intervals. The schedule is configured inecosystem.config.js
:
cron_restart
parameter using standard cron syntax:
*/30 * * * *
: Every 30 minutes0 */6 * * *
: Every 6 hours0 0 * * *
: Once a day at midnight0 0 * * 0
: Once a week on Sunday
Scaling Considerations
Vertical Scaling
- Increase server resources (CPU/RAM) to handle more concurrent processes
- Adjust Node.js memory limits with
--max-old-space-size
flag:
Horizontal Scaling
- Deploy multiple instances behind a load balancer
- Use PM2 cluster mode for the Express server:
Queue Optimization
- Adjust the number of concurrent queues based on server capacity:
- Implement priority queueing for critical accounts:
Data Extraction Process
The microservice extracts several types of data from OnlyFans:-
User Statistics
- Subscriber count and growth rate
- Revenue metrics
- Content engagement metrics
-
Engagement Metrics
- Message response rates
- Fan interaction frequency
- Content popularity analytics
-
Fan Data
- Active subscribers list
- Spending patterns
- Engagement behavior
Troubleshooting
Issue | Solution |
---|---|
Connection timeout | Increase Puppeteer navigation timeout: page.setDefaultNavigationTimeout(60000); |
Memory leaks | Ensure Puppeteer instances are closed properly in finally blocks |
Database errors | Verify Supabase credentials and check database schema compatibility |
Queue bottlenecks | Increase the number of concurrent queues and monitor queue statistics |
Cron job failures | Check logs with pm2 logs cron-job and verify environment variables |
Security Best Practices
- Environment Variables: All sensitive credentials stored in environment variables
- API Authentication: API endpoints secured with API_SECRET token validation
- CORS Policy: Configured for specific origins in production:
- Helmet.js: Secures Express app with various HTTP headers:
- Input Validation: All API inputs validated before processing
- Regular Updates: Dependencies kept updated to patch security vulnerabilities
Integration with Other Components
- Web Application: Displays data collected by the microservice through Supabase real-time subscriptions
- Chrome Extension: Provides authentication data (cookies, localStorage, sessionStorage) for the microservice
- Supabase Database: Stores all processed data and enables real-time updates through PostgreSQL change data capture