C.W.K.
Stream
Lesson 08 of 08 · published

Hybrid Architecture

~12 min · app, rest, sse, websocket

Level 0Poller
0 XP0/60 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

REST + WebSocket is the dominant production pattern

The most common production architecture is not "WebSocket for everything." It is REST for create/read/update/delete + WebSocket for live updates. POST a chat message to /api/messages; the REST handler persists it AND fans out via WebSocket. Reconnecting clients fetch history via GET /api/messages?since=X, then receive new messages via WebSocket.

SSE + WebSocket is the AI-era pattern

For applications with both AI streaming (one-way, high-token-rate) and interactive features (bidirectional), use SSE for the streaming and WebSocket for the rest. cwkPippa lives at this seam: chat replies stream over SSE, council picks and admin actions go through REST, and a single WebSocket carries presence and notifications. Three transports, each picked for what it is good at.

Code

REST endpoint that triggers WebSocket fan-out·python
from fastapi import FastAPI, Depends
from pydantic import BaseModel

class CreateMessage(BaseModel):
    room: str
    text: str

@app.post('/api/messages')
async def create_message(payload: CreateMessage, user = Depends(auth)):
    saved = await db.messages.insert({
        'room': payload.room,
        'text': payload.text,
        'from': user['id'],
    })
    # Source of truth is the database;
    # WebSocket is the liveness layer over it.
    await ws_manager.broadcast(payload.room, {
        'type': 'chat.message',
        'data': saved,
    })
    return saved
Architecture diagram·text
  Client
    |
    +-- POST /api/messages -----> REST API ----> Database
    |   (send a message)              |
    |                                  v
    +-- WS (subscribe room) <------ Manager broadcasts to room
    |   (receive others' messages)
    |
    +-- GET /api/messages?since=X -> REST API
        (history on reconnect)

External links

Exercise

Take any one of your previous chat exercises. Refactor so that send goes through POST /api/messages (which persists AND fans out) instead of through WebSocket. Confirm reconnecting clients can fetch history via GET /api/messages and seamlessly continue. Note where you removed code — the WebSocket handler should be smaller now.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.