sergiodxa / remix-utils

A set of utility functions and types to use with Remix.run
https://sergiodxa.github.io/remix-utils/
MIT License
2.07k stars 116 forks source link

Deduplicate connections to SSE with useEventSource regardless of the event #281

Closed na2hiro closed 9 months ago

na2hiro commented 9 months ago

use-event-source has a feature to deduplicate SSE connections in order to lower the number of EventStreams a browser needs to hold. However, when event name is the only differences, it creates multiple identical EventStreams and then on client side it listens to different events.

Instead, we can create a single EventStream for the same URL and withCredentials flag, no matter what event is.

Example

Given code like these

  const pinged = !!useEventSource(`/events`, { event: "ping" });
  const users = useEventSource(`/events`, { event: "users" });

It creates identical event streams

duplicate-sse-connections

after

Screenshot 2023-11-25 at 9 44 04

sergiodxa commented 9 months ago

Released on v7.3.0