-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
64 lines (47 loc) · 1.5 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
const { join } = require("path")
const { writeFileSync } = require("fs")
const { EventEmitter } = require("node:events")
const cors = require("cors")
const { createClient } = require("redis")
const express = require("express")
void (async() => {
const app = new express()
const { PORT } = process.env
const localEventEmitter = new EventEmitter()
const redisClient = createClient({
url: "redis://redis-sse:6379"
})
await redisClient.connect()
const redisSubscriber = redisClient.duplicate()
await redisSubscriber.connect()
redisSubscriber.subscribe("redis-channel", data => {
localEventEmitter.emit("message", data)
})
app.use(express.static(join(__dirname, "public")))
app.use(cors())
app.get("/ping", async(req, res) => {
const data = `pong ${PORT}`
if (req.query.redis === "true") {
await redisClient.publish("redis-channel", data)
} else {
localEventEmitter.emit("message", data)
}
res.sendStatus(200)
})
app.get("/streaming", (req, res) => {
res.set({
"Cache-Control": "no-cache",
"Content-Type": "text/event-stream",
Connection: "keep-alive"
})
res.flushHeaders()
localEventEmitter.on("message", (data) => {
res.write(`data: ${data}\nretry: 1000\n\n`)
})
})
app.get("/kill", () => {
// write a file to trigger nodemon live reload
writeFileSync(join(__dirname, "ignore", "restart.js"), "")
})
app.listen(PORT, () => console.log(`Server listening on port ${PORT}`))
})().catch(console.error)