Skip to content

Commit

Permalink
Add console logging of SSE connection
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathansick committed Apr 3, 2024
1 parent 089a274 commit ed7e4f7
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@ export default function TimesSquareHtmlEventsProvider({ children }) {
const { htmlEventsUrl } = useTimesSquarePage();
const fullHtmlEventsUrl = `${htmlEventsUrl}?${urlQueryString}`;

console.log(`In TimesSquareHtmlEventsProvider for ${fullHtmlEventsUrl}`);

React.useEffect(async () => {
async function runEffect() {
await fetchEventSource(fullHtmlEventsUrl, {
method: 'GET',
signal: abortController.signal,
onopen(res) {
if (res.ok && res.status === 200) {
console.log('Connection made ', res);
console.log(`Connection made to ${fullHtmlEventsUrl}`, res);
} else if (
res.status >= 400 &&
res.status < 500 &&
res.status !== 429
) {
console.log('Client side error ', res);
console.log(`Client side error ${fullHtmlEventsUrl}`, res);
}
},
onmessage(event) {
Expand All @@ -42,19 +44,26 @@ export default function TimesSquareHtmlEventsProvider({ children }) {
setHtmlEvent(parsedData);
},
onclose() {
console.log('Connection closed by the server');
console.log(
`Connection closed by the server to ${fullHtmlEventsUrl}`
);
},
onerror(err) {
console.log('There was an error from server', err);
console.log(
`There was an error from server for ${fullHtmlEventsUrl}`,
err
);
},
});
}
console.log(`Opening connection to ${fullHtmlEventsUrl}`);
runEffect();

return () => {
// Clean up: close the event source connection
abortController.abort();
setAbortController(new AbortController());
console.log(`Aborted connection to ${fullHtmlEventsUrl}`);
};
}, [fullHtmlEventsUrl]);

Expand Down

0 comments on commit ed7e4f7

Please sign in to comment.