Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ServerSentEventStream() #3751

Merged
merged 11 commits into from
Nov 8, 2023
Merged

Conversation

iuioiua
Copy link
Collaborator

@iuioiua iuioiua commented Oct 30, 2023

You can test this via:

import {
  type ServerSentEventMessage,
  ServerSentEventStream,
} from "./http/server_sent_event_stream.ts";

Deno.serve({ port: 8000 }, (req) => {
  if (req.url.endsWith("/")) {
    const body = `
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset=&quot;utf-8&quot; />
      </head>
      <body>
        <script>
          const source = new EventSource('/sse');
          source.onmessage = function(e) {
            document.body.innerHTML += e.data + '<br>';
          };
        </script>
      </body>
    </html>
    `;
    return new Response(body, {
      headers: { "content-type": "text/html; charset=utf-8" },
    });
  }

  if (req.url.endsWith("/sse")) {
    const stream = ReadableStream.from<ServerSentEventMessage>([{
      data: "hello there",
      id: "123",
    }]).pipeThrough(new ServerSentEventStream());
    return new Response(stream, {
      headers: {
        "content-type": "text/event-stream",
        "cache-control": "no-cache",
      },
    });
  }

  return new Response("not found", { status: 404 });
});

My main concern is that the name is too wordy. However, the reason I chose it is because it's clearest and doesn't conflict with the current ServerSentEvent() in std/http.

Closes #3693

Copy link
Member

@lucacasonato lucacasonato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay

http/server_sent_event_message_stream.ts Outdated Show resolved Hide resolved
http/server_sent_event_message_stream.ts Outdated Show resolved Hide resolved
@iuioiua iuioiua changed the title feat: ServerSentEventMessageStream() feat: ServerSentEventStream() Oct 30, 2023
@iuioiua iuioiua marked this pull request as ready for review October 30, 2023 22:47
@iuioiua iuioiua requested a review from kt3k as a code owner October 30, 2023 22:47
Copy link
Contributor

@lino-levan lino-levan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, looks great! Nice work Asher. Two nits.

http/server_sent_event_stream.ts Outdated Show resolved Hide resolved
http/server_sent_event_stream_test.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@lino-levan lino-levan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kt3k
Copy link
Member

kt3k commented Nov 8, 2023

How about directly encoding into Uint8Array? Do we ever need string output of SSE?

@iuioiua
Copy link
Collaborator Author

iuioiua commented Nov 8, 2023

Good idea. Done.

Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@iuioiua iuioiua merged commit 08f4dbc into main Nov 8, 2023
12 checks passed
@iuioiua iuioiua deleted the server-sent-event-message-stream branch November 8, 2023 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

proposal: replace ServerSentEvent() with ServerSentEventStream()
4 participants