Server Sent Event with React-native

48 views Asked by At

i am trying to integrate server sent event in React-Native , so i am using this package https://github.com/binaryminds/react-native-sse#readme

and this is the code , i pust it in the main stack of the app and

 useEffect(() => {
    const url = new URL("https://your-sse-server.com/.well-known/mercure");

    const es = new EventSource(url, {
      headers: {
        UserId: "123",
        Authorization: "123",
        "X-Api-Key": "0242ac120002",
      },
      method: "PATCH",
    });

    const listener: EventSourceListener = event => {
      if (event.type === "open") {
        console.log("Open SSE connection.");
      } else if (event.type === "waiting") {
        console.log(`Received waiting`);
      } else if (event.type === "success") {
        console.log(`Received success`);
      } else if (event.type === "error") {
        console.error("Connection error:", event.message);
      } else if (event.type === "exception") {
        console.error("Error:", event.message, event.error);
      }
    };

    es.addEventListener("open", listener);
    es.addEventListener("waiting", listener);
    es.addEventListener("success", listener);

    return () => {
      es.removeAllEventListeners();
      es.close();
    };
  }, []);

the issue i have when i start the app this line printed

 LOG  Open SSE connection.
 LOG  Received waiting
 LOG  Received waiting
 LOG  Received waiting
 LOG  Received waiting
 LOG  Received waiting
 LOG  Open SSE connection.
 LOG  Received waiting
 LOG  Received waiting
 LOG  Received waiting
 LOG  Received waiting
 LOG  Received waiting
 LOG  Received waiting
 LOG  Open SSE connection.
 LOG  Received waiting
 LOG  Received waiting
 LOG  Received waiting
 LOG  Received waiting
 LOG  Received waiting
 LOG  Received waiting
 LOG  Received waiting
 LOG  Open SSE connection.

the issue that this lines LOG Received waiting always printed one time without delay , and request to the https://your-sse-server.com/.well-known/mercure will be fired again this cyle will contine , api work on postman well , it return waiting every 2 second

0

There are 0 answers