I have a very simple set up.
NodeJs
export default class StreamController {
public static newMessage = async (req: Request, res: Response) => {
const { userid } = req.params;
res.writeHead(200, {
"Connection": "keep-alive",
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
});
setInterval(async () => {
console.log(await MessagesController.hasNewMessage(userid));
res.write(`${JSON.stringify({ hasUnread: await MessagesController.hasNewMessage(userid) })}`);
res.write("\n\n");
}, 5000);
}
}
React
constructor() {
super();
const uid = getUserId();
const eventSource = new EventSource(`http://localhost:8080/stream/messages/${uid}`)
eventSource.onmessage = (e) => {
console.log(e)
}
}
I can see that stream was opened but none event was passed from the server, while data was issued on the server side. What I'm doing wrong?
from Server Sent Events not receiving messages from stream with React and NodeJs
No comments:
Post a Comment