Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Show the Answer as It Arrives

In this video, you wrote the club page’s script. It sends a member’s message to the club’s server and shows the partner’s reply as it arrives, while the server keeps the session’s history.

Key Points

  • The page gets a session id once. crypto.randomUUID() returns a new random id as a string. The script calls it once, when the page loads, so every message from this page shares one practice session.
  • The page sends only the session id and the message. When the member sends the form, the handler stops the browser’s own submit, which would reload the page. It takes the message and clears the box.
  • The page shows the turn, then posts it. It adds two paragraphs to the chat panel, one with the member’s message and an empty one for the reply. Then fetch() posts { sessionId, message } as JSON. The history stays on the server.
  • EventSource can’t send the message. The browser’s EventSource, made for reading server-sent events, only makes GET requests, with no body, so the message would never reach the route. Use fetch() with a POST body.
  • await fetch() doesn’t wait for the whole reply. It finishes once the status and headers arrive. With the club’s server, they come together with the first piece, and the rest of the body is still arriving. If the server refused the message, the page puts the server’s text in the reply’s place and stops.
  • A TextDecoderStream turns the body’s bytes into strings. It’s the stream version of TextDecoder, and pipeThrough() sends the bytes through it. A letter like é stays whole even when a chunk splits it.
  • for await reads each piece as it arrives. Each pass adds one piece to the reply’s text. A piece is whatever the network delivered, so it can hold several words. Because you set .textContent instead of .innerHTML, the reply stays plain text, and nothing in it becomes part of the page’s HTML.
  • A dropped connection makes the loop throw. If the connection drops partway, the for await loop throws, and this handler doesn’t catch that yet. The next video catches it.
  • The server remembers the conversation. Mei’s second message didn’t repeat her name, and the page sent no history. The partner could still use her name, because the same session id led the server to send the history holding her first message. In this run, the reply started with “Claro, Mei.” The server’s terminal showed her session at four turns.

The page uses fetch() to post the message. Then it reads the body with for await, as the pieces arrive, and adds each one to the reply as text.

The Code

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 The club’s server now streams the partner’s reply and keeps each session’s history. So far, only curl has read that stream. In this video, the club’s page sends a member’s message and shows the reply as it arrives.

00:14 The starting page has an empty chat panel and a form with a message box and a Send button. Its script, chat.js, is empty. You’ll write it now.

00:26 The script starts by finding the chat panel, the form, and its input. Then it makes a session id. crypto.randomUUID() returns a new random id as a string.

00:38 It runs once, when the page loads, so every message from this page shares one practice session.

00:45 When the member sends the form, the handler first stops the browser’s own submit, which would reload the page. It takes the message and clears the box. Then it adds two paragraphs to the panel, one with the member’s message and an empty one for the reply.

01:00 Next, fetch posts the session id and the message as JSON. That’s all the page sends. The history stays on the server. Watch out: the browser’s EventSource, made for reading server-sent events, can’t send this.

01:15 It only makes GET requests, with no body, so the message would never reach the route. The await on fetch finishes once the status and headers arrive, without waiting for the whole reply.

01:28 With this server, they come together with the first piece. If the server refused the message, the page puts the server’s text in the reply’s place and stops.

01:38 Otherwise, the body is the reply, still arriving, as bytes. pipeThrough() sends those bytes through a TextDecoderStream, the stream version of the TextDecoder you’ve used.

01:49 Out come strings. A letter like e with an accent is kept whole even when a chunk splits it.

01:56 Then a for await loop takes each piece as it arrives and adds it to the reply’s text. Because you set textContent, not innerHTML, the reply stays plain text, so nothing in it becomes part of the page’s HTML.

02:12 If the connection drops partway, the loop throws, and the next video catches that.

02:19 Start the server with your key, as before, and open the page. Mei types her first message and sends it. After three and a half seconds in this run, the first piece arrives, and the reply grows, one piece per pass of the loop.

02:32 A piece can hold several words. Your run’s words will differ.

02:38 Her second message orders a green tea and asks for her name on the cup, without saying her name again. The page sends only the message and the session id, no history.

02:49 Can the partner use her name? Your turn: pause the video and predict.

03:02 It can. The session id is the same, so the server added this message to the history that holds her first message, with her name. In this run, the reply starts with Claro, Mei.

03:14 The page kept nothing. The server’s terminal shows this session at four turns.

03:22 So the page uses fetch to post the message. Then it reads the body with for await, as the pieces arrive, and adds each one to the reply as text. Mei now sees each reply grow piece by piece.

03:36 But she has to wait for the end of every reply. Next, in Stop the Answer and the Bill, you’ll add a Stop button for a member who doesn’t want to wait, and see what stopping only the page would cost.

Become a Member to join the conversation.