Skip to main content
WebSocket

Chat Room Simulation

SubscribeRequestValidateHard

What you will learn

  • Room isolation: joining room A must NOT receive messages from room B over the same connection โ€” a classic bug
  • Broadcast fan-out: one publisher โ†’ N subscribers, assert all receive within <100ms on local
  • Leave semantics: after explicit leave, the next message in that room must NOT arrive โ€” latency here is a leak

Description

Simulate a multi-user chat room: join, send messages, see other users' messages.

Test Scenario

// Test: Chat room interaction
// 1. Connect to chat server
// 2. Join a room
// 3. Send a message
// 4. Verify broadcast to other clients
// 5. Leave room gracefully

const ws = new WebSocket('wss://your-chat-server.com');
ws.send(JSON.stringify({ action: 'join', room: 'test-room', user: 'Bot1' }));
ws.send(JSON.stringify({ action: 'message', text: 'Hello everyone!' }));
ws.send(JSON.stringify({ action: 'leave' }));

Testing Tips

  • โ€ขUse room-based message routing
  • โ€ขTrack user presence (join/leave)
  • โ€ขHandle message ordering
// Playwright - WebSocket Chat
import { test, expect } from '@playwright/test';
test('Chat room messaging', async ({ page }) => {
await page.goto('/websocket/chat');
const wsPromise = page.waitForEvent('websocket');
await page.fill('#username', 'TestUser');
await page.click('#join-btn');
const ws = await wsPromise;
// Send a message
await page.fill('#message-input', 'Hello World');
await page.click('#send-btn');
// Wait for message to appear
const message = page.locator('.chat-message').filter({ hasText: 'Hello World' });
await expect(message).toBeVisible();
});

Challenge ID: ws-chat-room

2 frameworks available