Skip to main content
WebSocket

Send & Receive Messages

SubscribeValidateEasy

What you will learn

  • Assert message ordering โ€” WebSocket guarantees order per connection, not globally
  • Handle partial messages: some proxies fragment frames โ€” your parser must reassemble
  • Test close codes (1000 normal, 1006 abnormal, 1011 server error) โ€” clients behave differently per code

Description

Send a text message to the echo server and verify you receive the same message back.

Test Scenario

// Test: Echo message verification
const ws = new WebSocket('wss://echo.websocket.org');
const received: string[] = [];
ws.onmessage = (e) => received.push(e.data);

await waitFor(() => ws.readyState === WebSocket.OPEN);
ws.send('Hello, WebSocket!');
await waitFor(() => received.length > 0);
expect(received[0]).toBe('Hello, WebSocket!');

Testing Tips

  • โ€ขUse ws.send() to send messages
  • โ€ขListen for onmessage event
  • โ€ขEcho server returns exactly what you send
// Playwright API Testing - WebSocket
import { test, expect } from '@playwright/test';
test('WebSocket connection', async ({ page }) => {
// Listen for WebSocket connections
const wsPromise = page.waitForEvent('websocket');
await page.goto('/websocket-demo');
const ws = await wsPromise;
expect(ws.url()).toContain('ws://');
// Wait for message
const framePromise = ws.waitForEvent('framereceived');
const frame = await framePromise;
expect(frame.payload).toBeDefined();
});

Challenge ID: ws-send-receive

2 frameworks available