forked from KarenOk/whatsapp-web-clone-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (26 loc) · 846 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const express = require("express");
const { getRandomSentence, getResponseInterval } = require("./utils");
const PORT = process.env.PORT || 5000;
const app = express();
const server = app.listen(PORT, () => console.log("Server running..."));
const io = require("socket.io")(server, { cors: { origin: "*" } });
app.get("/", (req, res) => {
// Health Check
res.send("This service is up and running...");
});
io.on("connection", (socket) => {
socket.on("fetch_response", (data) => {
const { userId } = data;
const responseInterval = getResponseInterval(1000, 4000);
setTimeout(() => {
socket.emit("start_typing", { userId });
setTimeout(() => {
socket.emit("stop_typing", { userId });
socket.emit("fetch_response", {
response: getRandomSentence(),
userId,
});
}, responseInterval);
}, 1500);
});
});