Implement a WebSocket chat server with authenticated handshakes, Origin checks, room broadcast, message limits and stored history, plus a minimal client.
Act as a backend developer. Implement a real-time chat server over WebSockets for the requirements below. ## Inputs Language, runtime and WebSocket library already chosen (for example Node.js with `ws`, Python with `websockets`): [STACK] How users authenticate today (session cookie, bearer token) and how identity reaches the handshake: [AUTH] Allowed browser origins: [ORIGINS] Chat model: rooms or direct messages, who may join, message size and rate limits: [CHAT_RULES] Message history store and retention: [STORAGE] Deployment shape: single instance or several behind a load balancer: [DEPLOYMENT] ## Instructions 1. Authenticate during the handshake using the existing mechanism. Reject unauthenticated or disallowed-Origin handshakes before upgrading (HTTP 401/403). 2. Define a JSON message protocol with a `type` field (for example join, leave, message, history, error) and validate every incoming message: type, room permission, length and rate. 3. Broadcast a message only to members of its room. Decide and document whether the sender receives its own echo (useful as delivery acknowledgement). 4. Persist messages before broadcasting or document the ordering trade-off; serve recent history on join with pagination. 5. Track presence on connect/disconnect and use ping/pong heartbeats to drop dead connections. 6. For multiple instances, describe the fan-out mechanism (for example Redis pub/sub or a message broker) and sticky-session needs; do not claim a concurrency capacity. 7. Escape or treat message text as plain text on the client to avoid script injection. ## Return 1. Architecture summary and message protocol table. 2. Server code for handshake auth, connection lifecycle, validation, broadcast and history. 3. A minimal browser client using the WebSocket API. 4. Security controls mapped to the threats they address. 5. Test plan: unauthenticated handshake rejected, foreign Origin rejected, oversize message rejected, cross-room message not delivered. ## Acceptance checks Use only the libraries named in the stack. Do not invent load-test results or connection counts. Mark untested code paths.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Stack: Node.js 22 with the `ws` package, Express app already serving the site Auth: HttpOnly session cookie validated by existing `getSession(req)` Origins: https://app.example.com Chat rules: project rooms; only project members may join; max 4 KB per message; 20 messages per 10 seconds per user Storage: PostgreSQL table messages(id, room_id, user_id, body, created_at); keep 90 days Deployment: 2 instances behind a load balancer
A `server.on('upgrade')` handler that calls `getSession(req)` and checks the Origin against https://app.example.com before `handleUpgrade`, returning 401/403 otherwise; a protocol table with join/leave/message/history/error; membership checks on join and send; a 4 KB size check and per-user rate limiter; insert-then-broadcast to room members only; history on join paged by created_at; heartbeat cleanup; Redis pub/sub (or equivalent) for the second instance; a client that inserts text with textContent; and the four negative tests. Illustrative expectation, not a recorded model run.Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts: