const token = "your_jwt_token";
const eventSource = new EventSource(
`http://localhost:8000/api/v1/realtime/subscribe?token=${token}&collection=posts`
);
eventSource.addEventListener("message", (event) => {
const message = JSON.parse(event.data);
if (message.event === "heartbeat") {
console.log("Heartbeat received");
return;
}
console.log("Event received:", message);
// Update your UI
});
eventSource.onerror = (error) => {
console.error("SSE error:", error);
};
// Close the connection when done
// eventSource.close();