Before you begin, ensure you have the following installed:
- Elixir 1.14 or later
- Erlang/OTP 25 or later
- MySQL 8.0 or later
- Node.js 16.x or later (for Firebase client)
- Git
- Clone the repository:
git clone https://github.com/your-org/realtime-server.git
cd realtime-server- Install dependencies:
mix deps.get- Set up environment variables:
# Create a .env file from the template
cp .env.example .env
# Edit the .env file with your configurations:
# Database configuration
export DATABASE_URL=mysql://user:pass@localhost/realtime_server_dev
# Firebase configuration
export FIREBASE_PROJECT_ID=your-project-id
export FIREBASE_PRIVATE_KEY=your-private-key
export FIREBASE_CLIENT_EMAIL=your-client-email
# Guardian configuration
export GUARDIAN_SECRET_KEY=your-guardian-secret- Set up the database:
mix ecto.setup- Start the Phoenix server:
mix phx.serverThe server will be available at http://localhost:4000
# Run all tests
mix test
# Run tests with coverage
mix coveralls
# Run specific test file
mix test test/realtime_server/comments_test.exs# Run the linter
mix credo
# Run formatter
mix format- Register a new user:
curl -X POST http://localhost:4000/api/register \
-H "Content-Type: application/json" \
-d '{"user": {"email": "user@example.com", "password": "password123", "username": "testuser"}}'- Login:
curl -X POST http://localhost:4000/api/login \
-H "Content-Type: application/json" \
-d '{"email": "user@example.com", "password": "password123", "device_id": "device123"}'// Connect to WebSocket
let socket = new Socket("ws://localhost:4000/socket", {
params: { token: "your-jwt-token" },
});
socket.connect();
// Join a comment channel
let channel = socket.channel(`comments:${videoId}`, {});
channel
.join()
.receive("ok", (resp) => console.log("Joined successfully", resp))
.receive("error", (resp) => console.log("Unable to join", resp));If you encounter database connection issues:
- Ensure MySQL is running:
mysql.server status-
Verify database credentials in
.env -
Try resetting the database:
mix ecto.reset- Verify Firebase credentials in
.env - Ensure Firebase project has the correct security rules
- Check Firebase Admin SDK permissions
- Create a new branch for your feature:
git checkout -b feature/your-feature-name- Make your changes and ensure all tests pass:
mix test
mix credo --strict
mix format- Commit your changes:
git commit -m "feat: add your feature description"Logs are written to:
- Development: Console output
- Production:
/var/log/realtime_server/
View logs in development:
tail -f log/dev.log- Check application metrics:
mix observer- Monitor Phoenix endpoints:
Visit
http://localhost:4000/dashboardin development
- Read the Architecture Guide
- Review Real-time Features
- Check Firebase Integration
- Explore API Reference
- Review Deployment Guide
Configure your primary database connection:
PRIMARY_DB_USERNAME=your_primary_username
PRIMARY_DB_PASSWORD=your_primary_password
PRIMARY_DB_NAME=your_database_name
PRIMARY_DB_HOST=your_primary_instance.region.cloudsql.com
PRIMARY_DB_PORT=3306Configure your read replicas for better performance:
# First Replica
REPLICA_1_DB_USERNAME=your_replica1_username
REPLICA_1_DB_PASSWORD=your_replica1_password
REPLICA_1_DB_NAME=your_database_name
REPLICA_1_DB_HOST=your_replica1_instance.region.cloudsql.com
REPLICA_1_DB_PORT=3306
# Second Replica
REPLICA_2_DB_USERNAME=your_replica2_username
REPLICA_2_DB_PASSWORD=your_replica2_password
REPLICA_2_DB_NAME=your_database_name
REPLICA_2_DB_HOST=your_replica2_instance.region.cloudsql.com
REPLICA_2_DB_PORT=3306For secure database connections:
DB_CA_CERT=/path/to/server-ca.pem