'use strict' /** * Module dependencies */ const express = require('express') const fs = require('fs') const path = require('path') const cors = require('cors') /** * To test, publish something on 'test-express' channel * * $ redis-cli publish test-express testmessage */ // stream const subscribe = require('redis-subscribe-sse') // express app let app = express() app.use(cors()) let sse = subscribe({ channels: 'qrcode', retry: 5000, host: '127.0.0.1', port: 6379 }) let latestMessage = null sse.addListener("data", (data) => { // console.log(data.toString().substring(6)); latestMessage = JSON.parse(data.toString().substring(6)) }) app.get('/', (req, res) => { let index = path.join(__dirname, './index.html') fs.createReadStream(index).pipe(res) }) app.get("/qrcode", (req, res) => { res.json(latestMessage) // res.send("hello") }) app.get('/qrcode-sse', (req, res) => { req.socket.setTimeout(Number.MAX_VALUE) res.set({ 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive' }) sse.pipe(res) }) app.listen(3000, () => { console.log('Express listening on port 3000') })