├── .gitignore ├── utils.js ├── package.json ├── README.md ├── index.js └── database.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | /build -------------------------------------------------------------------------------- /utils.js: -------------------------------------------------------------------------------- 1 | const { sentences } = require("./database"); 2 | 3 | const getRandomSentence = () => { 4 | const randomIndex = Math.floor(Math.random() * sentences.length); 5 | const sentence = sentences[randomIndex]; 6 | return sentence; 7 | }; 8 | 9 | function getResponseInterval(min, max) { 10 | return Math.random() * (max - min) + min; 11 | } 12 | 13 | module.exports = { getRandomSentence, getResponseInterval }; 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "whatsapp-web-clone-backend", 3 | "version": "1.0.0", 4 | "description": "Backend server for whatsapp web clone", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "devStart": "nodemon index", 9 | "start": "node index" 10 | }, 11 | "author": "Karen Okonkwo", 12 | "license": "ISC", 13 | "dependencies": { 14 | "express": "^4.17.1", 15 | "nodemon": "^2.0.12", 16 | "socket.io": "^4.2.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Whatsapp Web Clone Backend 2 | 3 | This is a backend service that implements WebSocket protocol to communicate with the [Whatsapp Web Clone UI](https://github.com/KarenOk/whatsapp-web-clone) I previously built. 4 | 5 | Built using Express and Socket.IO 6 | 7 | ## Steps to run 8 | 9 | - Install dependencies with the command `npm install` 10 | - Start the server with the command `npm run start` 11 | - This application should start running on `http://localhost:5000/`. Run the [Whatsapp Web Clone UI](https://github.com/KarenOk/whatsapp-web-clone#readme) together with this service to see it in action. 12 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const { getRandomSentence, getResponseInterval } = require("./utils"); 3 | 4 | const PORT = process.env.PORT || 5000; 5 | const app = express(); 6 | const server = app.listen(PORT, () => console.log("Server running...")); 7 | 8 | const io = require("socket.io")(server, { cors: { origin: "*" } }); 9 | 10 | app.get("/", (req, res) => { 11 | // Health Check 12 | res.send("This service is up and running..."); 13 | }); 14 | 15 | io.on("connection", (socket) => { 16 | socket.on("fetch_response", (data) => { 17 | const { userId } = data; 18 | const responseInterval = getResponseInterval(1000, 4000); 19 | 20 | setTimeout(() => { 21 | socket.emit("start_typing", { userId }); 22 | 23 | setTimeout(() => { 24 | socket.emit("stop_typing", { userId }); 25 | socket.emit("fetch_response", { 26 | response: getRandomSentence(), 27 | userId, 28 | }); 29 | }, responseInterval); 30 | }, 1500); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /database.js: -------------------------------------------------------------------------------- 1 | const sentences = [ 2 | "Ooooh. That seems interesting. Tell me more!", 3 | "Joyce enjoyed eating pancakes with ketchup.", 4 | "It's not possible to convince a monkey to give you a banana by promising it infinite bananas when they die. It's not possible to convince a monkey to give you a banana by promising it infinite bananas when they die.", 5 | "At that moment he wasn't listening to music, he was living an experience.", 6 | "The clock within this blog and the clock on my laptop are 1 hour different from each other.", 7 | "They ran around the corner to find that they had traveled back in time.", 8 | "Please put on these earmuffs because I can't you hear.", 9 | "All she wanted was the answer, but she had no idea how much she would hate it. It's not possible to convince a monkey to give you a banana by promising it infinite bananas when they die.", 10 | "He enjoys practicing his ballet in the bathroom.", 11 | "Can we go to the park .", 12 | "Where is the orange cat? Said the big black dog.", 13 | "We can make the bird fly away if we jump on something.", 14 | "We can go down to the store with the dog. It is not too far away.", 15 | "My big yellow cat ate the little black bird.", 16 | "I like to read my book at school.", 17 | "We are going to swim at the park. It's not possible to convince a monkey to give you a banana by promising it infinite bananas when they die. It's not possible to convince a monkey to give you a banana by promising it infinite bananas when they die.", 18 | "They improved dramatically once the lead singer left. ", 19 | "I hear that Nancy is very pretty. ", 20 | "Sometimes you have to just give up and win by cheating.", 21 | "Blue sounded too cold at the time and yet it seemed to work for gin.", 22 | "The green tea and avocado smoothie turned out exactly as would be expected.", 23 | "In that instant, everything changed. ", 24 | "I currently have 4 windows open up… and I don’t know why.", 25 | "Gary didn't understand why Doug went upstairs to get one dollar bills when he invited him to go cow tipping.", 26 | "The shark-infested South Pine channel was the only way in or out.", 27 | "When he asked her favorite number, she answered without hesitation that it was diamonds.", 28 | "She insisted that cleaning out your closet was the key to good driving.", 29 | "He invested some skill points in Charisma and Strength. It's not possible to convince a monkey to give you a banana by promising it infinite bananas when they die. It's not possible to convince a monkey to give you a banana by promising it infinite bananas when they die.", 30 | "Mary realized if her calculator had a history, it would be more embarrassing than her computer browser history.", 31 | "Cats are good pets, for they are clean and are not noisy.", 32 | "It's much more difficult to play tennis with a bowling ball than it is to bowl with a tennis ball.", 33 | "There are over 500 starfish in the bathroom drawer.", 34 | "The murder hornet was disappointed by the preconceived ideas people had of him.", 35 | "It's not often you find a soggy banana on the street.", 36 | "The Japanese yen for commerce is still well-known.", 37 | "Shakespeare was a famous 17th-century diesel mechanic. It's not possible to convince a monkey to give you a banana by promising it infinite bananas when they die.", 38 | "He decided to live his life by the big beats manifesto.", 39 | "The bees decided to have a mutiny against their queen.", 40 | "His confidence would have bee admirable if it wasn't for his stupidity. It's not possible to convince a monkey to give you a banana by promising it infinite bananas when they die.", 41 | "He strives to keep the best lawn in the neighborhood.", 42 | "Carol drank the blood as if she were a vampire.", 43 | "Dan ate the clouds like cotton candy.", 44 | "He went on a whiskey diet and immediately lost three days.", 45 | "That is an appealing treasure map that I can't read. It's not possible to convince a monkey to give you a banana by promising it infinite bananas when they die.", 46 | "Henry couldn't decide if he was an auto mechanic or a priest.", 47 | "The small white buoys marked the location of hundreds of crab pots.", 48 | "Don't step on the broken glass.", 49 | "Her scream silenced the rowdy teenagers.", 50 | ]; 51 | 52 | module.exports = { sentences }; 53 | --------------------------------------------------------------------------------