aethercall
Version:
A scalable WebRTC video calling API built with Node.js and OpenVidu
164 lines (131 loc) • 5.61 kB
JavaScript
/**
* Example: Using AetherCall as a Module
* This shows how to integrate AetherCall into your existing Node.js application
*/
// Import AetherCall
const { startServer } = require('aethercall');
// Or if you want more control, import the components directly
const HTTPServer = require('aethercall/src/interfaces/http/server');
const OpenViduAPI = require('aethercall/src/core/openvidu/api');
const StorageAdapter = require('aethercall/src/core/storage/adapter');
const TokenManager = require('aethercall/src/core/auth/tokens');
async function simpleUsage() {
console.log('🎥 Starting AetherCall with default configuration...');
// Simple way - uses environment variables or defaults
await startServer();
}
async function advancedUsage() {
console.log('🔧 Starting AetherCall with custom configuration...');
// Advanced way - full control over configuration
const storage = new StorageAdapter({
type: 'memory', // or 'postgres', 'mongodb', 'filesystem'
// Additional config based on storage type
});
await storage.initialize();
const tokenManager = new TokenManager('your-jwt-secret', '24h');
const openviduAPI = new OpenViduAPI(
'http://localhost:4443', // OpenVidu URL
'MY_SECRET' // OpenVidu secret
);
const httpServer = new HTTPServer({
openviduAPI,
storage,
tokenManager
});
// Start on custom port
await httpServer.start(3001, '0.0.0.0');
console.log('✅ AetherCall server started on port 3001');
}
async function expressIntegration() {
console.log('🌐 Integrating AetherCall with existing Express app...');
const express = require('express');
const app = express();
// Your existing routes
app.get('/', (req, res) => {
res.json({ message: 'My App with Video Calling!' });
});
// Initialize AetherCall components
const storage = new StorageAdapter({ type: 'memory' });
await storage.initialize();
const tokenManager = new TokenManager('your-jwt-secret', '24h');
const openviduAPI = new OpenViduAPI('http://localhost:4443', 'MY_SECRET');
// Create AetherCall routes
const authRoutes = require('aethercall/src/interfaces/http/routes/auth');
const connectionRoutes = require('aethercall/src/interfaces/http/routes/connections');
const recordingRoutes = require('aethercall/src/interfaces/http/routes/recordings');
const dependencies = { openviduAPI, storage, tokenManager };
// Mount AetherCall routes under /video
app.use('/video/auth', authRoutes(dependencies));
app.use('/video/connections', connectionRoutes(dependencies));
app.use('/video/recordings', recordingRoutes(dependencies));
// Start your combined server
app.listen(3000, () => {
console.log('✅ Combined server running on port 3000');
console.log('🎥 Video API available at /video/*');
});
}
// Example with environment-based configuration
async function productionUsage() {
console.log('🚀 Starting AetherCall for production...');
// Load configuration from environment variables
require('dotenv').config();
const config = {
port: process.env.PORT || 3000,
host: process.env.HOST || '0.0.0.0',
openViduUrl: process.env.OPENVIDU_URL || 'http://localhost:4443',
openViduSecret: process.env.OPENVIDU_SECRET || 'MY_SECRET',
jwtSecret: process.env.JWT_SECRET || 'your-jwt-secret',
dbType: process.env.DB_TYPE || 'memory',
dbUrl: process.env.DATABASE_URL
};
// Initialize with production configuration
const storage = new StorageAdapter({
type: config.dbType,
url: config.dbUrl
});
await storage.initialize();
const tokenManager = new TokenManager(config.jwtSecret, '24h');
const openviduAPI = new OpenViduAPI(config.openViduUrl, config.openViduSecret);
const httpServer = new HTTPServer({
openviduAPI,
storage,
tokenManager
});
await httpServer.start(config.port, config.host);
console.log(`✅ Production AetherCall server running on ${config.host}:${config.port}`);
}
// Choose which example to run
async function main() {
const mode = process.argv[2] || 'simple';
switch (mode) {
case 'simple':
await simpleUsage();
break;
case 'advanced':
await advancedUsage();
break;
case 'express':
await expressIntegration();
break;
case 'production':
await productionUsage();
break;
default:
console.log('Usage: node usage-example.js [simple|advanced|express|production]');
console.log('');
console.log('Examples:');
console.log(' node usage-example.js simple # Simple usage with defaults');
console.log(' node usage-example.js advanced # Advanced configuration');
console.log(' node usage-example.js express # Integration with Express app');
console.log(' node usage-example.js production # Production configuration');
}
}
if (require.main === module) {
main().catch(console.error);
}
module.exports = {
simpleUsage,
advancedUsage,
expressIntegration,
productionUsage
};