Emitter Not updating
Unanswered
Maine Coon posted this in #help-forum
Maine CoonOP
Hi im trying to make my UI update in real time and i implemented the emitter events from Node.js
app/api/events/route.js: https://sourceb.in/rscNsXBuKl
lib/eventsEmitter: https://sourceb.in/FEugx1Fhbv
app/api/events/route.js: https://sourceb.in/rscNsXBuKl
lib/eventsEmitter: https://sourceb.in/FEugx1Fhbv
//Update in my main page
useEffect(() => {
async function loadMotds() {
const res = await fetch('/api/motds');
const data = await res.json();
data.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
setMotds(data);
}
loadMotds().catch(() => {});
setDate(new Date(Date.now()));
const es = new EventSource('/api/events');
es.onmessage = (e) => {
try {
const msg = JSON.parse(e.data);
if (msg.resource === 'motds') {
loadMotds().catch(() => {});
}
} catch {}
};
return () => es.close();
}, []);
1 Reply
Maine CoonOP
Something i forgot to say the events are triggered because logging i see them and they really do something but the UI doesn't really update