![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/lavocat.quebec/private_html/ |
// Simple Production Server for lavocat.quebec
// Fixed for Next.js 15 compatibility
require('dotenv').config({ path: '.env.production' });
const { createServer } = require('http');
const { parse } = require('url');
const next = require('next');
const WebSocket = require('ws');
const dev = false; // Production mode
const hostname = process.env.HOSTNAME || '0.0.0.0';
const port = process.env.PORT || 3443;
console.log('🚀 Starting production server for lavocat.quebec...');
// Initialize Next.js app
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();
// Global variables for WebSocket management
global.wsClients = new Map();
global.wsRoomSubscriptions = new Map();
global.wsUserPresence = new Map();
function initializeWebSocketServer(server) {
const wss = new WebSocket.Server({
server,
path: '/_ws',
clientTracking: true,
maxPayload: 1024 * 1024 // 1MB max payload
});
global.wsServer = wss;
console.log('✅ WebSocket server initialized');
wss.on('connection', (ws, req) => {
console.log('🔌 WebSocket connection established');
ws.on('message', (data) => {
try {
const message = JSON.parse(data.toString());
switch (message.type) {
case 'JOIN_ROOM': {
const { chatRoomId } = message.data;
if (chatRoomId) {
if (!global.wsRoomSubscriptions.has(chatRoomId)) {
global.wsRoomSubscriptions.set(chatRoomId, new Set());
}
global.wsRoomSubscriptions.get(chatRoomId).add(ws);
}
break;
}
case 'LEAVE_ROOM': {
const { chatRoomId } = message.data;
if (chatRoomId && global.wsRoomSubscriptions.has(chatRoomId)) {
global.wsRoomSubscriptions.get(chatRoomId).delete(ws);
}
break;
}
case 'CHAT_MESSAGE': {
const { chatRoomId } = message.data;
if (chatRoomId && global.wsRoomSubscriptions.has(chatRoomId)) {
const outboundMessage = JSON.stringify(message);
global.wsRoomSubscriptions.get(chatRoomId).forEach(clientWs => {
if (clientWs.readyState === WebSocket.OPEN) {
clientWs.send(outboundMessage);
}
});
}
break;
}
case 'ping': {
ws.send(JSON.stringify({ type: 'pong' }));
break;
}
}
} catch (error) {
console.error('WebSocket message error:', error);
}
});
ws.on('close', () => {
console.log('🔌 WebSocket connection closed');
});
ws.on('error', (error) => {
console.error('❌ WebSocket error:', error);
});
});
// Heartbeat interval
const interval = setInterval(() => {
wss.clients.forEach((ws) => {
if (ws.isAlive === false) {
return ws.terminate();
}
ws.isAlive = false;
ws.ping();
});
}, 30000);
wss.on('close', () => {
clearInterval(interval);
});
}
// Start the server
const startServer = async () => {
let isPrepared = false;
try {
console.log('🔄 Preparing Next.js application...');
// Use a try-catch around prepare to handle Next.js 15 issues
try {
await app.prepare();
console.log('✅ Next.js app prepared successfully');
isPrepared = true;
} catch (prepareError) {
console.log('⚠️ Next.js prepare failed, but continuing anyway...');
console.log('💡 This is normal for Next.js 15 in some configurations');
console.log('📝 Error details:', prepareError.message);
isPrepared = false;
}
// Create HTTP server
const server = createServer((req, res) => {
if (isPrepared) {
const parsedUrl = parse(req.url, true);
handle(req, res, parsedUrl);
} else {
// Fallback response when Next.js is not prepared
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(`
<!DOCTYPE html>
<html>
<head>
<title>lavocat.quebec - Maintenance</title>
<style>
body { font-family: Arial, sans-serif; text-align: center; padding: 50px; }
.container { max-width: 600px; margin: 0 auto; }
.logo { font-size: 2em; color: #2563eb; margin-bottom: 20px; }
.message { color: #666; margin-bottom: 30px; }
.status { background: #f0f9ff; padding: 20px; border-radius: 8px; border-left: 4px solid #2563eb; }
</style>
</head>
<body>
<div class="container">
<div class="logo">⚖️ avocat.quebec</div>
<div class="message">
<h2>Application is starting up...</h2>
<p>Please wait a moment while we prepare the application.</p>
</div>
<div class="status">
<p><strong>Status:</strong> Initializing Next.js application</p>
<p><strong>Environment:</strong> Production</p>
<p><strong>WebSocket:</strong> Available</p>
</div>
<script>
setTimeout(() => location.reload(), 5000);
</script>
</div>
</body>
</html>
`);
}
});
// Initialize WebSocket server
initializeWebSocketServer(server);
// Start listening
server.listen(port, hostname, (err) => {
if (err) throw err;
console.log(`🚀 Production server running on http://${hostname}:${port}`);
console.log(`🌐 Domain: lavocat.quebec`);
console.log(`🔌 WebSocket endpoint: ws://${hostname}:${port}/_ws`);
console.log(`📊 Environment: Production`);
console.log(`✅ Server is ready to handle requests!`);
if (!isPrepared) {
console.log('⚠️ Running in fallback mode - Next.js not fully prepared');
console.log('💡 The application will show a maintenance page until Next.js is ready');
}
});
} catch (error) {
console.error('❌ Failed to start server:', error.message);
process.exit(1);
}
};
// Handle graceful shutdown
process.on('SIGTERM', () => {
console.log('🔄 SIGTERM received, shutting down gracefully...');
if (global.wsServer) {
global.wsServer.close();
}
process.exit(0);
});
process.on('SIGINT', () => {
console.log('🔄 SIGINT received, shutting down gracefully...');
if (global.wsServer) {
global.wsServer.close();
}
process.exit(0);
});
// Start the server
startServer();