-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
78 lines (70 loc) · 2.3 KB
/
server.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import express from "express";
import compression from "compression";
import path from "path";
import cors from "cors";
const app = express();
app.use(express.json());
app.use(compression());
// app.use((req, res, next) => {
// res.setHeader("Access-Control-Allow-Origin", "*");
// next();
// });
app.use(cors());
// Serve any static files
app.use(express.static(path.join(process.cwd(), "client/build")));
let connectionId = 0;
let connections = 0;
const messages = [
{ text: "Message one", timestamp: Date.now() - 5000, username: "bot" },
{ text: "Message nubber two", timestamp: Date.now(), username: "bot2" },
];
app.get("/stream", (req, res, next) => {
// connections.push(res);
if (connections === 10) {
console.log(
"Max 10 connections, (Use emitter.setMaxListeners() to increase limit)"
);
return res.status(500).send("Max 10 clients on the same time :-(");
}
connectionId++;
connections++;
res.set({
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers":
"Origin, X-Requested-With, Content-Type, Accept",
});
console.log(`client ${connectionId} connected to SSE`);
// console.log("connections :>> ", connections);
res.write(`event: connected\n`);
console.log(`Currently ${connections} connections`);
res.status(200).write(`event: connected\n`);
res.status(200).write(`data: ${JSON.stringify(messages)}\n\n`);
res.flush();
app.on("new_message_arrived", () => {
// console.log("data :>> ", data);
res.write(`event: new_message\n`);
res.write(`data: ${JSON.stringify(messages)}\n\n`);
res.flush();
});
req.on("close", () => {
console.log("client closed,");
connections--;
console.log(`Currently ${connections} connections`);
});
});
app.post("/new_message", (req, res) => {
console.log("req.body :>> ", req.body);
const { text, username } = req.body;
messages.push({ text, username, timestamp: Date.now() });
app.emit("new_message_arrived");
res.status(200).json({ message: "Success" }).end();
});
app.get("/", (req, res) => {
res.sendFile(path.join(process.cwd(), "client/build/index.html"));
});
app.listen(process.env.PORT || 4000, function () {
console.log("Example app listening on port 4000!");
});