22 lines
850 B
TypeScript
22 lines
850 B
TypeScript
![]() |
import ReconnectingEventSource from "reconnecting-eventsource";
|
||
|
|
||
|
|
||
|
export function subscribeToAllTickets({ onMessage, onError, accessToken }: {
|
||
|
accessToken: string;
|
||
|
onMessage: (e: MessageEvent) => void;
|
||
|
onError: (e: Event) => void;
|
||
|
}) {
|
||
|
if (!accessToken) throw new Error("Trying to subscribe to SSE without access token");
|
||
|
|
||
|
const url = `https://admin.pena.digital/heruvym/subscribe?Authorization=${accessToken}`;
|
||
|
const eventSource = new ReconnectingEventSource(url);
|
||
|
|
||
|
eventSource.addEventListener("open", () => console.log(`EventSource connected with ${url}`));
|
||
|
eventSource.addEventListener("close", () => console.log(`EventSource closed with ${url}`));
|
||
|
eventSource.addEventListener("message", onMessage);
|
||
|
eventSource.addEventListener("error", onError);
|
||
|
|
||
|
return () => {
|
||
|
eventSource.close();
|
||
|
};
|
||
|
}
|