bibliogram/src/site/api/update_stream.js

35 lines
666 B
JavaScript
Raw Normal View History

2020-05-21 15:44:52 +00:00
const {Readable} = require("stream")
const streams = new Set()
setInterval((new (function() {
const payload = `:keepalive ${Date.now()}\n\n`
for (const stream of streams.values()) {
stream.push(payload)
}
2020-06-20 16:09:36 +00:00
})).constructor, 50000).unref()
2020-05-21 15:44:52 +00:00
module.exports = [
{
route: "/api/update_stream", methods: ["GET"], code: async () => {
const stream = new Readable({
read: function() {},
destroy: function() {
streams.delete(stream)
}
})
streams.add(stream)
2020-05-21 16:24:21 +00:00
stream.push(":connected\n\n")
2020-05-21 15:44:52 +00:00
return {
statusCode: 200,
contentType: "text/event-stream",
2020-05-21 16:24:21 +00:00
headers: {
"X-Accel-Buffering": "no"
},
2020-05-21 15:44:52 +00:00
stream
}
}
}
]