mirror of
https://git.sr.ht/~cadence/bibliogram
synced 2025-12-14 10:35:07 +00:00
Add experimental update stream
This commit is contained in:
30
src/site/api/update_stream.js
Normal file
30
src/site/api/update_stream.js
Normal file
@@ -0,0 +1,30 @@
|
||||
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)
|
||||
}
|
||||
})).constructor, 50000)
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
route: "/api/update_stream", methods: ["GET"], code: async () => {
|
||||
const stream = new Readable({
|
||||
read: function() {},
|
||||
destroy: function() {
|
||||
streams.delete(stream)
|
||||
}
|
||||
})
|
||||
streams.add(stream)
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
contentType: "text/event-stream",
|
||||
stream
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user