WebSocket Server Deployment Strategy
This document outlines our branching strategy and deployment workflow for the WebSocket server on Digital Ocean App Platform. We use separate branches to manage each environment, allowing independent deployments decoupled from our Vercel frontend.
Branch Strategy
We maintain three parallel branches for our websocket server:
| Branch | Environment | Purpose |
|---|---|---|
server-main | Production | Live environment for end users |
server-development | Staging | Testing environment for QA and validation |
server-beta | Beta | Early access features and experimental changes |
This separation allows us to:
- Deploy websocket server changes independently from the main application
- Test new websocket functionality without affecting production
- Roll back server changes without disrupting the frontend
Deployment Architecture
Each branch is configured to deploy to a separate Digital Ocean App Platform application:
GitHub Digital Ocean Vercel Frontend
--------- ------------ ---------------
server-main -------> Production Server <---> Production (main)
imagineo-websocket-production
server-development --> Staging Server <---> Staging (development)
imagineo-websocket-staging
server-beta --------> Beta Server <---> Beta (beta)
imagineo-websocket-betaConfiguration Files
We use Digital Ocean App Spec files to define the deployment configuration for each environment:
app.production.yaml- Production environment (server-main)app.development.yaml- Staging environment (server-development)app.beta.yaml- Beta environment (server-beta)
Common Configuration Pattern
Each environment follows this common pattern:
name: imagineo-websocket-[environment]
services:
- build_command: |
curl -fsSL https://bun.sh/install | bash
export PATH=$HOME/.bun/bin:$PATH
cd websocket-server || exit 1
echo "Installing dependencies..."
bun install || exit 1
echo "Building application..."
bun run build || exit 1
cd ..
environment_slug: node-js
github:
branch: server-[environment]
deploy_on_push: true
repo: baudevs/app.imagineoai.com
http_port: 8080
instance_count: 1
instance_size_slug: apps-s-1vcpu-0.5gb
name: app-imagineoai-com
run_command: |
curl -fsSL https://bun.sh/install | bash
export PATH=$HOME/.bun/bin:$PATH
cd websocket-server || exit 1
echo "Starting application..."
bun src/server.ts
source_dir: /Environment Variables
Each environment requires these environment variables:
| Variable | Description | Example |
|---|---|---|
PORT | Port the server listens on | 8080 |
NODE_ENV | Environment name | production, staging, beta |
FRONTEND_URL | Vercel frontend URL | https://app.imagineoai.com |
CLERK_SECRET_KEY | Clerk authentication key | clerk_key_xxxx |
WEBHOOK_SECRET | Secret for webhook validation | ws_secret_xxxx |
DATABASE_URL | Turso database URL | libsql://xxxx.turso.io |
DATABASE_AUTH_TOKEN | Turso database token | xxxx |
Deployment Workflow
Initial Setup
- Create each branch from the corresponding main branch:
# Create server-main from main
git checkout main
git checkout -b server-main
git push -u origin server-main
# Create server-development from development
git checkout development
git checkout -b server-development
git push -u origin server-development
# Create server-beta from beta
git checkout beta
git checkout -b server-beta
git push -u origin server-beta- Create Digital Ocean App Platform applications:
- Create a new app for each environment
- Upload the corresponding app spec YAML file
- Configure the environment variables
Ongoing Deployment Process
- Make changes to the websocket server on the appropriate branch
- Push changes to trigger automatic deployment:
git checkout server-development
# Make changes to websocket-server directory
git add .
git commit -m "Add new websocket feature"
git push origin server-development- Digital Ocean automatically deploys the changes
- Update Vercel environment variables if needed:
NEXT_PUBLIC_WEBSOCKET_URL=https://app-imagineoai-com-xxxx.ondigitalocean.app
WEBSOCKET_URL=https://app-imagineoai-com-xxxx.ondigitalocean.app
WEBSOCKET_API_KEY=same-as-webhook-secretPromotion Workflow
To promote changes through environments:
# Promote from development to beta
git checkout server-beta
git merge server-development
git push origin server-beta
# Promote from beta to production
git checkout server-main
git merge server-beta
git push origin server-mainTroubleshooting
Build Failures
Common issues and solutions:
-
Dependency version conflicts:
- Check
websocket-server/package.jsonfor outdated dependencies - Update versions to match the main project’s dependencies
- Check
-
Bun installation issues:
- Verify the Bun installation commands in app spec files
- Check Digital Ocean logs for specific errors
-
Environment variable errors:
- Ensure all required environment variables are set in Digital Ocean
- Check for typos in variable names
Runtime Issues
-
Connection failures:
- Verify CORS settings in the websocket server
- Check that Vercel environment variables point to the correct URL
- Ensure the websocket server is running (
curl https://app-url/health)
-
Authentication errors:
- Verify Clerk credentials are correct
- Check that webhook secrets match between Digital Ocean and Vercel
-
Database errors:
- Verify Turso database credentials
- Check database connection in logs
Monitoring and Maintenance
-
Performance monitoring:
- Use Digital Ocean’s built-in metrics
- Set up alerts for high CPU/memory usage
- Monitor connection counts and response times
-
Log analysis:
- Review logs regularly for errors or warnings
- Set up log-based alerts for critical issues
-
Scaling:
- Monitor resource usage and scale as needed
- Consider upgrading instance size for increased traffic
Rollback Procedure
If a deployment introduces issues:
- Revert to the previous commit:
git checkout server-main
git revert HEAD~1
git push origin server-main- Use Digital Ocean’s rollback feature:
- Navigate to the App Platform dashboard
- Select the affected app
- Go to “Deployments” tab
- Click “Revert” on the last working deployment
Database Considerations
Each environment uses its own Turso database instance:
- Production:
DATABASE_URL=libsql://production-db-xxxx.turso.io - Staging:
DATABASE_URL=libsql://staging-db-xxxx.turso.io - Beta:
DATABASE_URL=libsql://beta-db-xxxx.turso.io
Schema changes should be propagated through environments in the same order as code changes.
Security Considerations
-
Authentication:
- Use different Clerk keys for each environment
- Regularly rotate webhook secrets
-
CORS configuration:
- Configure CORS to only allow requests from the corresponding frontend
- Use environment variables to specify allowed origins
-
API access:
- Limit access to admin endpoints with authentication
- Use rate limiting for public endpoints
Future Improvements
- Implement CI/CD pipeline for automated testing before deployment
- Add automated database migrations
- Set up monitoring dashboards for each environment
- Implement canary deployments for production