Hi I want my server to send data to the client. For now I want to count down from 10 till 0 and then disconnect. I want to use SSE because I only need it to be unidirectional.
I'm using nodejs with express.
I found this thread on stackoverflow that got me in the right direction but im stuck so im here for a helping hand. This is the thread: How to use server-sent-events in express.js
This Is what i got:
Routes:
var express = require('express');
var router = express.Router();
const fs = require('fs');
/* GET home page. */
const myHtml = fs.readFileSync('./views/price/changes/24hours.html', 'utf-8');
const price_24h_apidata = fs.readFileSync("./apidata/price/apidata_price_24h_changes.json", 'utf-8');
function countdown(res, count) {
res.write("data: " + count + "\n\n")
if (count)
//Placeholder: later update, jsonapidata
setTimeout(() => countdown(res, count-1), 1000)
else
res.end()
}
router.get('/', function(req, res, next) {
res.send(myHtml.replace(/jsonapidata/g, JSON.stringify(({price_24h_apidata}), null, 0)));
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
})
countdown(res, 10)
});
module.exports = router;
code included in the HTML Page:
<script>
if (!!window.EventSource) {
var source = new EventSource('/price/changes/24hour');
source.addEventListener('message', function(e) {
document.getElementById('data').innerHTML = e.data
}, false)
source.addEventListener('open', function(e) {
document.getElementById('state').innerHTML = "Connected"
}, false)
source.addEventListener('error', function(e) {
const id_state = document.getElementById('state')
if (e.eventPhase == EventSource.CLOSED)
source.close()
if (e.target.readyState == EventSource.CLOSED) {
id_state.innerHTML = "Disconnected"
}
else if (e.target.readyState == EventSource.CONNECTING) {
id_state.innerHTML = "Connecting..."
}
}, false)
} else {
console.log("Your browser doesn't support SSE")
}
</script>
</head>
<body>
<script>
var unparseddata = (jsonapidata);
var apidata = JSON.parse(unparseddata.price_24h_apidata);
</script>
<div class="test1">
<h1>SSE: <span id="state"></span></h1>
<h3>Data: <span id="data"></span></h3>
</div>
When running the server there is no error. When visiting the page I'm receiving this error:
GET /price/changes/24hour 200 24.501 ms - -
Error: Cannot find module 'html'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at new View (C:\Users\Feds\Documents\Coinparticle\coinparticle\node_modules\express\lib\view.js:81:14)
at Function.render (C:\Users\Feds\Documents\Coinparticle\coinparticle\node_modules\express\lib\application.js:570:12)
at ServerResponse.render (C:\Users\Feds\Documents\Coinparticle\coinparticle\node_modules\express\lib\response.js:1008:7)
at C:\Users\Feds\Documents\Coinparticle\coinparticle\app.js:70:7
at Layer.handle_error (C:\Users\Feds\Documents\Coinparticle\coinparticle\node_modules\express\lib\router\layer.js:71:5)
at trim_prefix