Skip to content

NW | 26-SDC-Mar | TzeMing Ho | Sprint 2 | Chat App frontend and backend#78

Open
TzeMingHo wants to merge 4 commits into
CodeYourFuture:mainfrom
TzeMingHo:long-polling
Open

NW | 26-SDC-Mar | TzeMing Ho | Sprint 2 | Chat App frontend and backend#78
TzeMingHo wants to merge 4 commits into
CodeYourFuture:mainfrom
TzeMingHo:long-polling

Conversation

@TzeMingHo
Copy link
Copy Markdown

Learners, PR Template

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Changelist

frontend link:
https://tzemingho-chatapp-server-frontend.hosting.codeyourfuture.io

backend link:
https://tzemingho-chatapp-server-backend.hosting.codeyourfuture.io

My prep link:
https://github.com/TzeMingHo/Project-Chat-App

@github-actions

This comment has been minimized.

@TzeMingHo TzeMingHo changed the title added files NW | May 14, 2026
@github-actions

This comment has been minimized.

@TzeMingHo TzeMingHo changed the title NW | NW | 26-SDC-Mar | TzeMing Ho | Sprint 2 | Chat App frontend and backend May 14, 2026
@TzeMingHo TzeMingHo added 📅 Sprint 2 Assigned during Sprint 2 of this module Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels May 14, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions Bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label May 14, 2026
@github-actions

This comment has been minimized.

1 similar comment
@github-actions

This comment has been minimized.

@TzeMingHo TzeMingHo added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label May 14, 2026
Copy link
Copy Markdown

@cjyuan cjyuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code could use some comments.

A useful rule of thumb is to ask yourself whether you could still explain how the code works six months from now. If the answer is uncertain, consider adding a comment to help future readers understand the intent and reasoning behind it.


Can you also include this item in the PR description?

  • A description of the additional features you have implemented.

<h1>Chat channel</h1>
</header>
<main>
<div id="chat-display-area"></div>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently the chat display area and the form can overlap when there are a lot of messages.

Can you make the necessary change or propose a method to overcome this issue?

Comment on lines +6 to +7
// backendURL: "https://tzemingho-chatapp-server-backend.hosting.codeyourfuture.io",
backendURL: "http://localhost:4000",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not look like the deployed version. 😄

Comment on lines +119 to +133
const messageInputElement = document.getElementById("message-input");
messageInputElement.addEventListener("input", (e) => {
state.messageString = e.target.value.trim();
});

const userInputElement = document.getElementById("user-name-input");
userInputElement.addEventListener("input", (e) => {
state.userString = e.target.value.trim();
});

document
.getElementById("message-submit-button")
.addEventListener("click", async (e) => {
await messageSubmitHandler(e, state.messageString, state.userString);
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why immediately update messageString and userString for every "input" event instead of extracting the input values only when the user clicks the submit button?

What do you think of this arrangement?

  • Assign messageSubmitHandler as the callback to the click event
  • Retrieve the two pieces of input in messageSubmitHandler

Comment on lines +110 to +111
console.error(`Message or user cannot be empty.`);
window.alert("Message or user cannot be empty.");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Neither of these approaches is a user-friendly mechanism for showing error messages.

No change needed, but should you need to further improve the app, this is an area to consider.

Comment on lines +102 to +104
} catch (error) {
console.error(`Failed to post message: ${error}`);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job in anticipating potential errors (even if it is using only a placeholder error handling mechanism).

if (response.ok) {
const confirmMessage = await response.text();
if (confirmMessage == "sent") {
chatDisplay();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why call chatDisplay() when state.messages remains unchanged at this point?

Comment thread chat-app/backend/app.js
Comment on lines +11 to +21
const port = 4000;

const waitingRoom = [];

const chatHistory = [
{
message: "Welcome to the channel.",
user: "System",
timestamp: new Date().getTime(),
},
];
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could consider moving these declarations before const app = express();

Comment thread chat-app/backend/app.js
if (isNaN(since)) {
return res.json(chatHistory);
}
const newMessages = chatHistory.filter(({ timestamp }) => timestamp > since);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An approach to reduce the amount of data sent in the response. Nicely done!

const queryString = lastMessageTime ? `?since=${lastMessageTime}` : "";
const url = `${state.backendURL}/messages${queryString}`;
try {
const rawResponse = await fetch(url);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not obvious that this fetch() request may remain pending for up to 25 seconds without examining the server-side implementation.

Consider adding a comment to explain this behavior.

Comment thread chat-app/backend/app.js
Comment on lines +39 to +51
const callback = (message) => res.json([message]);
waitingRoom.push(callback);

const seconds = 25;
const milliseconds = 1000;

const timeout = setTimeout(() => {
const index = waitingRoom.indexOf(callback);
if (index !== -1) {
waitingRoom.splice(index, 1);
res.send([]);
}
}, seconds * milliseconds);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took me quite a while to figure out how the "waiting room" logic works. I think it is over complicated.

These scenarios may be difficult to reproduce consistently, but there is a possibility that the "waiting room" logic could break under the following conditions:

  • HTTP responses arrive out of order due to the asynchronous nature of request processing.
  • Multiple users post messages concurrently, causing two or more messages to receive the same timestamp.

No change needed.

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels May 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Reviewed Volunteer to add when completing a review with trainee action still to take. 📅 Sprint 2 Assigned during Sprint 2 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants