← All guidesCodex for Backend Engineering · Future with Shivank
Hands-on guide

17. Socket.io & Redis

Start Part 4: ask Codex to copy Part 3 into Part 4 and grill you on real-time bid updates with WebSockets and a Redis backplane. Optionally send the same request in Plan mode in a second chat, so you can compare the two ways of planning with your own eyes.

Part 4 · start≈ 30–45 min hands-onUses grill-with-docsOptional: Plan mode

What you'll do

Before you start

⚙ Codex settings for this guide
Project / folderworkshop
ModelGPT-5.6 Sol
ReasoningMedium
ModeNormal chat for the grill. Plan mode only in the optional comparison chat.
ApprovalsApprove for me
📘 Concept: WebSocket vs HTTP polling

Normal web traffic is HTTP: the browser asks, the server answers, the connection is done. The server cannot speak first. So if a page wants to know about a new bid, it has two options. Polling: ask “anything new?” every second or two. Simple, but wasteful, and still up to two seconds late. WebSocket: open one connection and keep it open, like a phone call instead of a series of text messages. Now either side can talk whenever it likes, so the server can push “new bid on auction X” to the page within milliseconds, and nobody has to keep asking. Our Part 3 pages refresh only after you place a bid or when the auction ends; Part 4 makes them update live, for every watcher, the instant any bid is accepted.

📘 Concept: Socket.IO

Socket.IO is a Node.js library that makes WebSockets easy to use. Raw WebSockets give you a pipe for messages and nothing else. Socket.IO adds the things every real app needs: named events (“auction:changed”), rooms (a browser joins the room for one auction and only gets that auction's events), automatic reconnection when the network drops, and a fallback to HTTP long-polling for networks that block WebSockets. It has a server part that plugs into our Fastify API and a client part that runs in the React page. It also has an official adapter for Redis, which is how the backplane in the next box is wired up.

📘 Concept: Redis and why a backplane is needed

Redis is a very fast in-memory data store that runs as its own server (for us, in a Docker container next to Postgres). It is often used as a cache, and it has a built-in messaging feature we use here. Now the problem it solves. Picture the app in production with two API replicas, two identical copies of the server behind a load balancer that spreads users across them. Bidder A's browser is connected to replica 1; bidder B's to replica 2. B places a bid, which lands on replica 2. Replica 2 pushes “auction changed” to its connected browsers, but replica 1 never hears about it, so A's page stays stale. The fix is a backplane: every replica publishes each event to Redis, and Redis forwards it to every replica, which then pushes it to its own browsers. Redis is the shared channel behind all the replicas. With a single replica you do not need it, but we build for the distributed case from the start, because retrofitting it later is painful.

1

Commit everything so far

Before I start a new part I always make sure the previous one is committed. A clean commit boundary lets you see exactly what Codex changes for Part 4, nothing mixed in from before. You committed Part 3 in guide 15, so this is most likely a no-op; run it anyway, in the Terminal app.

❯Run in TerminalTerminal app · workshop folder
cd ~/codex-course/workshop && git add -A && git commit -m "Part 3 done"

cd moves into the workshop; git add -A stages anything that changed; git commit -m "Part 3 done" saves a snapshot with that message. If there is nothing to save, Git simply tells you so.

✓ You should see

Either a short commit summary (a line starting with [main …] Part 3 done and a file count) or nothing to commit, working tree clean. Both are fine; both mean your Part 3 is safely saved.

2

Start Part 4 with the grilling prompt

In Codex, click New chat in the sidebar under the workshop project. New part, new chat: the Part 3 chat is full of locking and clock details that would only distract here. Check that the picker under the message box reads 5.6 Sol Medium, then paste this prompt and send it.

A confession first. This is my second version of the prompt. My first version said the app needs to be distributed “because we have 100000 users on our waitlist”. It was a joke. Codex took it literally and started asking about capacity targets, connections per second and scaling plans, none of which we want. So I threw that chat away and started again with the cleaner prompt below. Use this one.

✦Paste into CodexNew chat in workshop
start in directory 4 and copy files from directory 3 for this slice

we want to make the frontend use websockets to update as soon as bids come through. we want to protect against when this app is distributed that the websockets will succeed. grill me with docs!!!!!!!

Three parts to this prompt. The first line is the housekeeping: copy Part 3 into the Part 4 folder and work there. The middle is the actual feature request in two sentences: live updates over WebSockets, and it must keep working when the app runs as several copies (“distributed”). The last line triggers the grill-with-docs skill, which makes Codex question you about the design and ground its questions in the existing code and docs before writing anything.

✓ You should see

Codex names the chat something like “Grill WebSocket bid updates”. It reads the root and part-level AGENTS.md / CONTEXT.md, then reports something similar to: “Part 3's source, tests, manifests, and documentation are now copied into part-4-realtime-socketio-redis; generated dependencies and test output were excluded.” (Generated dependencies means the node_modules folder, which it reinstalls rather than copies.)

Then it splits “WebSockets succeed when distributed” into two guarantees (events cross API replicas; clients recover after interruptions) and starts Grill — Round 1: Product guarantees. You get about 10 numbered questions. Each has a ❓ question and a ➡️ “I recommend…” line, and the round ends with “Answer Q1–Q10 in order.”

💡 Tip

Everything you write biases the model. A joke became a requirement. And Codex will probably pick Redis without being asked, because the root AGENTS.md already mentions it as the stack for this part. My habit: leave out explanations, jokes and background, and just ask for what you want. If the model needs context, it will ask.

⚠ Heads up

A grilling session reads a lot of files and writes long answers, so expect a noticeable token cost on your API key. Two rounds for Part 4 is a normal-sized run; do not start extra grills “to see what happens”.

3

Optional: send the same request in Plan mode

While the grill runs, I want to show you Codex's built-in Plan mode in a second chat, with the same request, so you can compare the two approaches side by side. It is only a comparison and does not change your code. Skip Steps 3 and 4 if you want to save tokens.

  1. Click New chat under workshop.
  2. In the message box type /plan and pick Plan mode – Turn plan mode on from the menu.
  3. Paste the prompt below and send it. It is the same request as Step 2, minus the copy instruction (Part 4 already exists now) and minus “grill me with docs”.
⚙ Codex settings
ModePlan mode on (this chat only)
✦Paste into Codex (Plan mode chat)Second new chat in workshop
start in directory 4

we want to make the frontend use websockets to update as soon as bids come through. we want to protect against when this app is distributed that the websockets will succeed.
📘 Concept: Plan mode vs grilling

Plan mode is built into Codex. You describe a task, Codex reads the code, asks you a few multiple-choice questions in a card, and then writes one long plan document that you read and accept before any code is written. It is read-only until you approve. Grilling is a skill we installed in guide 5. It also asks before building, but differently: 8–10 pointed questions per round, up to two rounds, each with a recommendation, and it records every answer in GRILL.md and the agreed vocabulary in CONTEXT.md. Plan mode is quick and good for small features that fit the existing patterns. Grilling is slower and surfaces the decisions you did not know you had to make, which is what you want for anything with concurrency, time or infrastructure in it.

✓ You should see

A Plan chip appears next to “Approve for me” under the message box, and the empty box says “Describe your task to generate a plan…”. After you send the prompt, Codex reads files, says it has reduced things to a few product choices, then shows a question card labelled “1 of 3”.

💡 Tip

If /plan does not show Plan mode in your Codex version, skip this step and the next one. They do not change your code.

4

Optional: answer Plan mode's 3 questions

Plan mode asks multiple-choice questions in a card, each with one option marked Recommended. Click an option to answer and the next card appears. Notice how few questions there are compared with the grill running in the other chat. I picked:

  1. Which screens should react to accepted bids? → Auction detail only. The homepage and search cards can stay as they are; live-updating a whole list is more work for little gain.
  2. How should an API instance behave when Redis is unavailable? → Stay HTTP-ready (Recommended). Bidding must keep working even if live updates are down.
  3. Should users see realtime connection status? → Subtle status. A tiny indicator, not a banner.

Then I sent one more line. Both chats are working on the same Part 4 folder, and I did not want the plan chat to write files while the grill chat was also writing there. Two agents editing one folder at the same time is a recipe for confusion.

✦Paste into Codex (Plan mode chat)Plan mode chat
continue without editing files plz
✓ You should see

“Absolutely — this remains read-only planning. I won't edit or generate any project files.” Plan mode keeps working and ends with one long plan document that you are asked to read and accept.

💡 Tip

My view after using both: Plan mode is fine for small, native features, but you end up reading one big plan at the end, and big plans get skimmed. Grilling asks better questions one at a time, and I actually read each one because I have to answer it. You do not need to accept this plan. Just go back to the grill chat.

5

Answer grill round 1

Go back to the grill chat. Codex writes its own questions each run, so the wording, order and count may differ from mine (the root AGENTS.md caps it at 8–10 questions per round, 2 rounds max). Each question ends with a recommendation. Writing “take recommend” accepts it. Read each question properly before answering; this is where you shape the design.

What my answers decided, and why. Match these to your question numbers by topic:

  1. What counts as real-time success → take recommend: pages normally update within about a second, and always end up matching the database. “Eventually correct” is the real guarantee; “instant” is the goal.
  2. Which screens go live → only the auction detail page, not the homepage or search cards. Smallest useful scope.
  3. Distribution boundary → several API replicas in one region, one Redis server as the backplane. We are not designing for multiple data centres.
  4. Restrictive networks / long-polling / sticky sessions → keep it super simple, only Redis as backplane. (Long-polling is the HTTP fallback for networks that block WebSockets; sticky sessions make a load balancer send the same user to the same replica every time. Both are real production concerns, and both are out of scope today.)
  5. Where bid writes live → take recommend: bids stay on HTTP, sockets only push updates. The write path we hardened in Part 3 does not change at all; the socket is read-only notification.
  6. Redis unavailable → take recommend: keep accepting bids, Postgres is the source of truth. Live updates are a nice-to-have; accepting bids is the business.
  7. How interrupted clients recover → take recommend if it is simple. A laptop sleeps, the Wi-Fi drops; the page must catch up when it comes back.
  8. Who may subscribe → take recommend: public, read-only, same as the REST API. There is no login in this app, so there is nothing to protect.
  9. Show connection health in the UI → no. (Different from the Plan-mode answer, on purpose; I wanted even less UI here.)
  10. Prove distribution with a multi-replica demo → don't add that right now. Building and running two replicas locally is a project of its own.
✦My answers (round 1), adjust the numbers to match your questionsGrill chat
1. take recommend
2. only the auction detail page
3. take recommend, just one Redis server as the backplane plz
4. just keep it super simple, don't worry too much about distributed systems EXCEPT redis as backplane
5. take recommend
6. take recommend
7. take recommend if it's simple enough
8. take recommend
9. no
10. don't add that right now

Why this matters: look at how many of these answers cut scope. Grilling is not only about getting the design right; it is about saying no to everything you do not need yet. The model will happily build sticky sessions and a replica demo if you let it.

✓ You should see

Codex replies with something similar to: “Round 1 is recorded in GRILL.md and the resolved domain language in CONTEXT.md.” Then Grill — Round 2: Technical shape starts, with about 9 more questions. Round 1 was about what the product guarantees; round 2 is about how to build it.

💡 Tip

If Codex keeps asking about scale or extra infrastructure, steer it the way I do: “just keep it super simple” or “don't add that right now”. Real-world questions like sticky sessions matter, but not for this slice.

6

Answer grill round 2

Round 2 is about the technical shape. The questions get more specific, and a couple of them hide important ideas. What my answers decided, and why:

  1. Recovery architecture → take recommend: the ordinary Redis Pub/Sub adapter for Socket.IO, plus an automatic HTTP refetch after every reconnect. After a disconnect the page simply asks the API for the current state once, instead of trying to replay missed events.
  2. WebSocket-only transport (no long-polling fallback) → yes. One transport, less to test.
  3. Event = notification or full state → take recommend: a small auction:changed notice (slug + bid ID), then the browser refetches the auction over HTTP. The socket says “something changed”; the API, which already has the correct, tested read logic, says what. This keeps the socket payload tiny and keeps one source of truth.
  4. Subscription rooms → take recommend: one room per auction slug. A page watching auction A never receives events about auction B.
  5. Emission boundary → take recommend: broadcast only after the bid transaction commits. If we emitted before the commit and the transaction then rolled back, every watcher would see a bid that does not exist. This connects straight back to guide 14.
  6. Should the bidder get their own event → yes. Simpler than special-casing the sender, and harmless.
  7. Redis failure at startup → don't start the API unless Redis is running. I chose this on purpose: when Redis is down, Codex notices immediately in the startup error and starts the container itself, instead of the app half-working and us hunting for why live updates are silent.
  8. Silent refresh failures → take recommend: keep the last good snapshot on screen, no error UI. A failed background refetch should not scare the user.
  9. Verification → yes to focused tests and a normal two-window check, but no multi-replica demo.
📘 Concept: pub/sub

Publish/subscribe is a messaging pattern. A sender publishes a message to a named channel without knowing or caring who is listening. Any number of receivers subscribe to that channel and get a copy of every message. The sender and receivers never talk to each other directly; the channel decouples them. Redis has this built in. In our design every API replica subscribes to the Socket.IO channel in Redis; when a bid commits on any replica, that replica publishes “auction:changed” once, Redis delivers it to all replicas, and each replica forwards it to the browsers in that auction's room. Socket.IO's Redis adapter does all of this for us behind one line of configuration.

✦My answers (round 2), adjust the numbers to match your questionsGrill chat
1. take recommend
2. yes
3. take recommend
4. take recommend
5. take recommend
6. yes
7. don't start unless redis is also started
8. take recommend
9. yes, also we're not doing any multi-replica demonstration at all, don't overcomplicate things plz
✓ You should see

An “Edited 2 files” card: part-4-realtime-socketio-redis/GRILL.md and CONTEXT.md. Open CONTEXT.md in VS Code if you like: it now defines terms like Accepted bid, Auction watcher and Authoritative auction state. This shared vocabulary is what Codex will use consistently in code, tests and messages in guide 18. Codex then shows “Preparing feature slices proposal…”. This guide ends here. Leave this chat open; guide 18 continues in it with the slice list and the build.

💡 Tip

Why I stay on Medium reasoning for grilling: a model that thinks longer also tends to design longer, adding layers we never asked for. LLMs can absolutely overthink a small slice. Medium gives good questions without the gold-plating.

Stuck or got a very different result?

Codex went off on scale and capacity questions? You probably added extra context to the prompt. Start a new chat and send the Step 2 prompt exactly as written.

Part 3 is not finished? Replace your Part 3 with the finished reference version, then start this guide at Step 1. The reference folder is the end state of the whole part (all slices done). The command deletes your Part 3 folder (cannot be undone), copies the reference in, and commits.

If you would rather skip Part 4 entirely, use the same command with part-4-realtime-socketio-redis in place of the Part 3 folder name. Be aware that this copies the finished real-time code from guide 18, not just this guide's grilling docs, so you would then skip guide 18 as well.

❯Catch up: Part 3Terminal app
cd ~/codex-course/workshop && rm -rf part-3-business-rules-concurrency && cp -R ../reference/part-3-business-rules-concurrency ./ && git add -A && git commit -m "Catch up: part 3 from reference"

Key takeaways