react-eventsource-hook
Version:
React hook for the EventSource interface (Server-Sent Events) with retry backoff, pause on hidden, and reconnect/close controls.
27 lines (24 loc) • 824 B
TypeScript
import { EventSourceMessage } from '@microsoft/fetch-event-source';
interface UseEventSourceOptions {
url: string;
withCredentials?: boolean;
init?: RequestInit;
fetch?: typeof window.fetch;
retryIntervalMs?: number;
maxRetryIntervalMs?: number;
pauseOnHidden?: boolean;
onopen?: (response: Response) => void;
onmessage?: (message: EventSourceMessage) => void;
onerror?: (error: unknown) => void;
onclose?: () => void;
}
interface UseEventSourceResult {
readyState: 0 | 1 | 2;
lastEventId: string;
error?: any;
events: EventSourceMessage[];
close: () => void;
reconnect: () => void;
}
declare function useEventSource(options: UseEventSourceOptions): UseEventSourceResult;
export { type UseEventSourceOptions, type UseEventSourceResult, useEventSource };