Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions websockets/backend/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ app.listen(port, () => {

app.post("/message", (req, res) => {
const bodyBytes = [];

// It's quite hard to read what this function is doing, because it does lots of things at different levels of abstraction.
// Can you think how to make this function only act at one level of abstraction (e.g. by using a middleware for some of the handling)?
Copy link
Owner

Choose a reason for hiding this comment

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

yes, I can use express.json() to do exactly the same job

req.on("data", (chunk) => bodyBytes.push(...chunk));
req.on("end", () => {
const bodyString = String.fromCharCode(...bodyBytes);
Expand All @@ -56,6 +59,7 @@ app.post("/message", (req, res) => {
}
const newMessage = {
messageText: body.messageText,
// Can you think of any problems with using a user-supplied timestamp here, vs calculating the timestamp on the server?
Copy link
Owner

Choose a reason for hiding this comment

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

The user can modify the timestamp on the client side, so we shouldn't trust any user-provided input for security reasons.

timestamp: body.timestamp,
likes: 0,
dislikes: 0,
Expand Down Expand Up @@ -135,6 +139,7 @@ webSocketServer.on("request", (request) => {
console.log(new Date() + " Connection accepted.");
activeWsConnections.push(connection);
connection.on("close", function (reasonCode, description) {
// This looks like a TODO you meant to do?
Copy link
Owner

Choose a reason for hiding this comment

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

done, now I use splice() to remove connection from array of connections

// put removing connection from active connections array here
console.log(
new Date() + " Peer " + connection.remoteAddress + " disconnected."
Expand Down
4 changes: 4 additions & 0 deletions websockets/frontend/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const websocket = new WebSocket(
"wss://droid-an-chat-application-backend.hosting.codeyourfuture.io"
);

// It looks like you're using websockets just to notify "there's something for you to fetch", which feels slower than including the messages in the websocket itself.
// What do you think?
Copy link
Owner

Choose a reason for hiding this comment

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

wow, I didn’t know that a message can be sent to the connection right after it’s established. Done

websocket.addEventListener("open", () => {
console.log("CONNECTED");
fetchAllMessagesSince();
Expand Down Expand Up @@ -34,6 +36,7 @@ const postMessageToBackend = async () => {
const messageText = inputMessage.value.trim();

if (!messageText) {
// This feels like it would be handy to extract a named function for "show an error message and then remove it".
Copy link
Owner

Choose a reason for hiding this comment

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

Done it

feedbackMessage.textContent = "Text is required.";
setTimeout(() => (feedbackMessage.textContent = ""), 5000);
return;
Expand Down Expand Up @@ -140,6 +143,7 @@ const keepFetchingRatings = async () => {

websocket.addEventListener("message", (mesEvent) => {
const response = JSON.parse(mesEvent.data);
// How unique do you expect timestamps to be? Can you see any risks with this approach to identifying messages?
Copy link
Owner

Choose a reason for hiding this comment

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

yes, it's possible for two users to send message at exactly the same millisecond. I updated the message in state check to compare the entire message instead, but it would probably be better to assign a unique ID to each message.

if (!state.messages.some((mes) => mes.timestamp === response.timestamp)) {
state.messages.push(response);
render();
Expand Down