![]() 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/public_html/ |
// Simple Express Server for lavocat.quebec
// Serves Next.js static export with WebSocket support
require('dotenv').config({ path: '.env.production' });
const express = require('express');
const path = require('path');
const WebSocket = require('ws');
const { createServer } = require('http');
const app = express();
const server = createServer(app);
const port = process.env.PORT || 3443;
const hostname = process.env.HOSTNAME || '0.0.0.0';
// Global variables for WebSocket management
global.wsClients = new Map();
global.wsRoomSubscriptions = new Map();
global.wsUserPresence = new Map();
// Serve static files from Next.js export
app.use(express.static(path.join(__dirname, '.next/export')));
// Handle all routes by serving index.html (SPA routing)
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '.next/export/index.html'));
});
// WebSocket server
const wss = new WebSocket.Server({
server,
path: '/_ws'
});
console.log('🚀 WebSocket server initialized for production');
// WebSocket connection handling
wss.on('connection', (ws, req) => {
console.log('✅ WebSocket connection established');
// Message handling
ws.on('message', (data) => {
try {
const message = JSON.parse(data.toString());
console.log('📨 WebSocket message received:', message.type);
// Handle different message types
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);
console.log(`User joined room ${chatRoomId}`);
}
break;
}
case 'LEAVE_ROOM': {
const { chatRoomId } = message.data;
if (chatRoomId && global.wsRoomSubscriptions.has(chatRoomId)) {
global.wsRoomSubscriptions.get(chatRoomId).delete(ws);
console.log(`User left room ${chatRoomId}`);
}
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);
}
});
// Connection close handling
ws.on('close', (code, reason) => {
console.log(`🔌 WebSocket connection closed - Code: ${code}, Reason: ${reason}`);
});
// Error handling
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 server
server.listen(port, hostname, () => {
console.log(`🚀 Express server running on http://${hostname}:${port}`);
console.log(`🌐 Domain: lavocat.quebec (HTTPS handled by DirectAdmin)`);
console.log(`📊 Environment: Production`);
console.log(`🔌 WebSocket endpoint: ws://${hostname}:${port}/_ws`);
console.log(`📁 Serving static files from: .next/export`);
});