![]() 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/scripts/ |
const WebSocket = require('ws');
async function testChatRealTime() {
console.log('š Testing Chat Real-Time Messaging...\n');
try {
// Create two WebSocket connections to simulate two users
const ws1 = new WebSocket('ws://localhost:3000?userId=user1&userRole=USER');
const ws2 = new WebSocket('ws://localhost:3000?userId=admin1&userRole=ADMIN');
let ws1Connected = false;
let ws2Connected = false;
let messageReceived = false;
let roomJoined = false;
ws1.on('open', () => {
console.log('ā
User 1 connected');
ws1Connected = true;
if (ws2Connected) {
// Join a test room
setTimeout(() => {
ws1.send(JSON.stringify({ type: 'JOIN_ROOM', data: { chatRoomId: 'test-room-1' } }));
console.log('š¤ User 1 joined room test-room-1');
}, 500);
}
});
ws2.on('open', () => {
console.log('ā
Admin 1 connected');
ws2Connected = true;
if (ws1Connected) {
// Join the same test room
setTimeout(() => {
ws2.send(JSON.stringify({ type: 'JOIN_ROOM', data: { chatRoomId: 'test-room-1' } }));
console.log('š¤ Admin 1 joined room test-room-1');
}, 1000);
}
});
ws1.on('message', (data) => {
const message = JSON.parse(data);
console.log('šØ User 1 received:', message.type);
if (message.type === 'CHAT_MESSAGE') {
messageReceived = true;
console.log('ā
User 1 received chat message:', message.data.content);
}
});
ws2.on('message', (data) => {
const message = JSON.parse(data);
console.log('šØ Admin 1 received:', message.type);
if (message.type === 'CHAT_MESSAGE') {
messageReceived = true;
console.log('ā
Admin 1 received chat message:', message.data.content);
}
});
// Send a test message after both users have joined the room
setTimeout(() => {
const testMessage = {
type: 'CHAT_MESSAGE',
data: {
id: Date.now().toString(),
content: 'Hello from User 1!',
type: 'TEXT',
chatRoomId: 'test-room-1',
senderName: 'User 1',
senderId: 'user1',
createdAt: new Date().toISOString(),
user: {
id: 'user1',
name: 'User 1',
email: 'user1@test.com',
role: 'USER'
}
}
};
ws1.send(JSON.stringify(testMessage));
console.log('š¤ User 1 sent message to room test-room-1');
}, 2000);
// Test private message
setTimeout(() => {
const privateMessage = {
type: 'PRIVATE_MESSAGE',
data: {
registrationId: 'test-registration-1',
message: {
id: Date.now().toString(),
content: 'Private message from User 1',
type: 'TEXT',
createdAt: new Date().toISOString(),
sender: {
id: 'user1',
name: 'User 1',
email: 'user1@test.com',
role: 'USER'
}
}
}
};
ws1.send(JSON.stringify(privateMessage));
console.log('š¤ User 1 sent private message');
}, 3000);
// Wait for test to complete
setTimeout(() => {
if (messageReceived) {
console.log('\nš Real-time chat messaging is working!');
} else {
console.log('\nā Real-time chat messaging failed - no messages received');
}
ws1.close();
ws2.close();
process.exit(0);
}, 5000);
} catch (error) {
console.error('ā Test failed:', error);
process.exit(1);
}
}
testChatRealTime();