-
-
Notifications
You must be signed in to change notification settings - Fork 62
London | 26-SDC-March | Zobeir Rigi | Sprint 2 | Chat app #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 23 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
54f1c77
Setup backend project
Zobeir-Rigi 26e8f49
nodemon, express installed, get, and post endpoint created
Zobeir-Rigi f5d7cb9
saving messages with the same structure
Zobeir-Rigi c7c5214
key , and val are same so allowed to name, message
Zobeir-Rigi c88dbfd
Implement WebSocket server and broadcast messages to connected clients
Zobeir-Rigi f245b51
add timestamp
Zobeir-Rigi cfcaa1f
i also add timestamp to the res, and deleted an unnecessary line
Zobeir-Rigi 389432d
fetching data from backend
Zobeir-Rigi 9072ec2
cors installed and imported
Zobeir-Rigi f7a111a
fetching data from backend to frontend
Zobeir-Rigi 25014c9
frontend can send message
Zobeir-Rigi f2aa893
reload messages, and inputs clear
Zobeir-Rigi e260d79
when key, and value are same u are alowed to do name in js
Zobeir-Rigi 7770879
websocket added to show messages instantly, and broadcasting
Zobeir-Rigi 34c512d
changed tags name, and added a simple css
Zobeir-Rigi 9f87449
added validation
Zobeir-Rigi d9e903b
added validation
Zobeir-Rigi 28f4ebd
deleted dead code and comments
Zobeir-Rigi b2ff7a9
deleted comment to be ready to deploy
Zobeir-Rigi 3259f33
Dockerfile added, start added to package, and const Port
Zobeir-Rigi d285aa0
port updated
Zobeir-Rigi f87b360
updated urls
Zobeir-Rigi 0bbd61d
updated urls
Zobeir-Rigi b8b5e35
checking if our value coming from a js file so we check the type, al…
Zobeir-Rigi 4f7b85f
removed console.log
Zobeir-Rigi 13f4d5e
addEventListener added
Zobeir-Rigi 33d3107
used constants for the clean and easy code
Zobeir-Rigi 468a96d
i separeted rendering and fetch
Zobeir-Rigi c2f616d
frontend validation added + handling fetch err
Zobeir-Rigi c151bc0
i use init() to start the app
Zobeir-Rigi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| node_modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Use Node | ||
| FROM node:18 | ||
|
|
||
| # Create app folder | ||
| WORKDIR /app | ||
|
|
||
| # Copy package files | ||
| COPY package*.json ./ | ||
|
|
||
| # Install dependencies | ||
| RUN npm install | ||
|
|
||
| # Copy all files | ||
| COPY . . | ||
|
|
||
| # Expose port | ||
| EXPOSE 3000 | ||
|
|
||
| # Start server | ||
| CMD ["node", "app.js"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| const WebSocket = require("ws"); | ||
| const express = require("express"); | ||
| const cors = require('cors'); | ||
|
|
||
| const app = express(); | ||
| app.use(express.json()) | ||
| app.use(cors()); | ||
|
|
||
| const allMessages = [] | ||
|
|
||
| const fetchAllMessages = () => { | ||
| return allMessages | ||
| } | ||
|
|
||
| app.get("/messages", (req, res) => { | ||
| const messages = fetchAllMessages() | ||
| res.json(messages) | ||
| }) | ||
|
|
||
| const clients = []; | ||
|
|
||
| app.post("/message", (req, res) => { | ||
| const { message, name } = req.body | ||
|
|
||
| if (!message.trim() || !name.trim()) { | ||
| return res.status(400).json({ | ||
|
cjyuan marked this conversation as resolved.
Outdated
|
||
| error: "Name and message are required" | ||
| }) | ||
| } | ||
| const timestamp = new Date(); | ||
| console.log(`Received message: ${name} ${message}`); | ||
| allMessages.push({ | ||
| name, message, timestamp | ||
| }) | ||
| console.log(allMessages) | ||
|
cjyuan marked this conversation as resolved.
Outdated
|
||
|
|
||
| clients.forEach(client => { | ||
| client.send(JSON.stringify({ | ||
| name, | ||
| message, | ||
| timestamp | ||
| })) | ||
| }) | ||
|
|
||
| res.send({ | ||
| status: "success", | ||
| message: message, | ||
| name: name, | ||
| timestamp: timestamp | ||
| }) | ||
|
|
||
| }) | ||
|
|
||
| //webSocket | ||
|
|
||
| const http = require("http"); | ||
| const server = http.createServer(app); | ||
|
|
||
| const PORT = process.env.PORT || 3000; | ||
|
|
||
| server.listen(PORT, () => { | ||
| console.log("server is running"); | ||
| }); | ||
|
|
||
|
|
||
| const wss = new WebSocket.Server({ server }); | ||
|
|
||
| wss.on("connection", (ws) => { | ||
| console.log("New client connected"); | ||
| clients.push(ws); | ||
|
|
||
| ws.on("close", () => { | ||
| console.log("Client disconnected"); | ||
| const index = clients.indexOf(ws); | ||
| clients.splice(index, 1); | ||
| }); | ||
|
|
||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could consider moving these declaration before line 5 to keep all declarations before executable statements.