Skip to content

Latest commit

 

History

History
248 lines (171 loc) · 4.51 KB

File metadata and controls

248 lines (171 loc) · 4.51 KB

Getting Started Guide

Prerequisites

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

Installation

  1. Clone the repository:
git clone https://github.com/your-org/realtime-server.git
cd realtime-server
  1. Install dependencies:
mix deps.get
  1. 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
  1. Set up the database:
mix ecto.setup

Running the Server

  1. Start the Phoenix server:
mix phx.server

The server will be available at http://localhost:4000

Development Environment

Running Tests

# Run all tests
mix test

# Run tests with coverage
mix coveralls

# Run specific test file
mix test test/realtime_server/comments_test.exs

Code Quality

# Run the linter
mix credo

# Run formatter
mix format

API Documentation

Authentication

  1. 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"}}'
  1. Login:
curl -X POST http://localhost:4000/api/login \
  -H "Content-Type: application/json" \
  -d '{"email": "user@example.com", "password": "password123", "device_id": "device123"}'

WebSocket Connection

// 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));

Common Issues and Solutions

Database Connection Issues

If you encounter database connection issues:

  1. Ensure MySQL is running:
mysql.server status
  1. Verify database credentials in .env

  2. Try resetting the database:

mix ecto.reset

Firebase Connection Issues

  1. Verify Firebase credentials in .env
  2. Ensure Firebase project has the correct security rules
  3. Check Firebase Admin SDK permissions

Development Workflow

  1. Create a new branch for your feature:
git checkout -b feature/your-feature-name
  1. Make your changes and ensure all tests pass:
mix test
mix credo --strict
mix format
  1. Commit your changes:
git commit -m "feat: add your feature description"

Monitoring and Debugging

Logging

Logs are written to:

  • Development: Console output
  • Production: /var/log/realtime_server/

View logs in development:

tail -f log/dev.log

Performance Monitoring

  1. Check application metrics:
mix observer
  1. Monitor Phoenix endpoints: Visit http://localhost:4000/dashboard in development

Next Steps

Database Configuration

Primary Database (Google Cloud SQL)

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=3306

Read Replicas

Configure 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=3306

SSL Configuration

For secure database connections:

DB_CA_CERT=/path/to/server-ca.pem