Skip to content

Timeout for Server-Sent Events #330

Answered by Marcdj-02
Marcdj-02 asked this question in Q&A
Discussion options

You must be logged in to vote

@kirankumal I've resolved it with adding a init and ping like this:

 return eventStream(request.signal, (send) => {
    function handle(data: string) {
      send({ event: TARGET_EVENT, data });
    }

    send({ event: 'init', data: '' });

    const pingInterval = setInterval(() => {
      send({ event: 'ping', data: '' });
    }, PING_INTERVAL);

    ServerSentEvent.on(TARGET_EVENT, handle);

    return function clear() {
      ServerSentEvent.off(TARGET_EVENT, handle);

      clearInterval(pingInterval);
    };
  });

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@sergiodxa
Comment options

@Marcdj-02
Comment options

@intuji-kiran
Comment options

@Marcdj-02
Comment options

Answer selected by Marcdj-02
@intuji-kiran
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants