Skip to content

Instantly share code, notes, and snippets.

@sbin0819
Created April 8, 2023 03:39
Show Gist options
  • Save sbin0819/f5fc24542c0caedad06a3229fb8c0e16 to your computer and use it in GitHub Desktop.
Save sbin0819/f5fc24542c0caedad06a3229fb8c0e16 to your computer and use it in GitHub Desktop.
useEffect(() => {
  const sse = new EventSource('[YOUR_SSE_ENDPOINT_URL]',
    { withCredentials: true });
  function getRealtimeData(data) {
    // process the data here,
    // then pass it to state to be rendered
  }
  sse.onmessage = e => getRealtimeData(JSON.parse(e.data));
  sse.onerror = () => {
    // error log here

    sse.close();
  }
  return () => {
    sse.close();
  };
}, [YOUR_DEPENDENCIES_HERE]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment