npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@silyze/next-sse

v1.0.0

Published

Server Sent Events implementation for @mojsoski/next-endpoint

Readme

Next SSE

Server-Sent Events implementation for @mojsoski/next-endpoint, providing a simple API to stream SSE responses in Next.js endpoints.

Installation

npm install @silyze/next-sse

Usage

Import and use in your Next.js API route to create an SSE stream:

import createServerSentEvents from "@silyze/next-sse";
import { createApiEndpoint } from "@mojsoski/next-endpoint";
import { NextResponse } from "next/server";

export const GET = createApiEndpoint(({ request }) => {
  return createServerSentEvents(
    (write) => {
      // Send a simple data message
      void write({ data: "Hello, World!" });

      // Optionally return a cleanup function
      return () => {
        // Clean up timers or subscriptions here
      };
    },
    {
      request,
      Response: NextResponse,
      keepAliveInterval: 1000, // milliseconds between comment pings
    }
  );
}, [] as const);

API Reference

createServerSentEvents(handler, options)

Creates a streaming SSE response.

  • handler: ServerSentEventsHandler Function invoked with a write callback to send SSE messages. May return a cleanup function.

  • options: ServerSentEventsOptions<TRequest, TResponse> Either the incoming Request object or an object with:

    • request: the incoming request
    • Response: a response constructor (e.g. NextResponse)
    • keepAliveInterval?: interval in milliseconds to send SSE comments (":" ping) to keep the connection alive (default: 1000)

Returns an instance of Response (or the provided Response constructor) with headers:

  • Content-Type: text/event-stream
  • Cache-Control: no-cache
  • Connection: keep-alive

Types

ServerSentData

export type ServerSentData = {
  data: string;
  id?: string;
  event?: string;
  type?: "data" | undefined;
};
  • data: The message payload (string).
  • id: Optional message identifier (id: field).
  • event: Optional custom event type (event: field).
  • type: Message type; normally omitted or set to "data".

ServerSentComment

export type ServerSentComment = {
  type: "comment";
  text: string;
};
  • type: "comment" to send an SSE comment.
  • text: Comment text; each line will be prefixed with ": ".

ServerSentRetry

export type ServerSentRetry = {
  type: "retry";
  time: number;
};
  • type: "retry" to adjust reconnection time.
  • time: Milliseconds the client should wait before retrying.

ServerSentMessage

export type ServerSentMessage =
  | ServerSentData
  | ServerSentRetry
  | ServerSentComment;

Union of all supported message types.

ServerSentEventsWrite

export type ServerSentEventsWrite = (
  message: ServerSentMessage
) => Promise<void>;

Function to send a single SSE message.

ServerSentEventsHandler

export type ServerSentEventsHandler = (
  write: ServerSentEventsWrite
) => (() => void) | void;

Function that receives the write callback. Can return an optional cleanup function that is called when the connection is closed or aborted.

ServerSentEventsOptions<TRequest, TResponse>

export type ServerSentEventsOptions<
  TRequest extends Request,
  TResponse extends Response
> =
  | TRequest
  | {
      request: TRequest;
      Response: new (body?: BodyInit | null, init?: ResponseInit) => TResponse;
      keepAliveInterval?: number;
    };

Options to configure the SSE stream:

  • Pass the raw Request if using standard web APIs.
  • Or supply an object with request, a custom Response constructor, and optional keepAliveInterval.

Examples

Custom Event and ID

return createServerSentEvents((write) => {
  void write({
    data: JSON.stringify({ count: 1 }),
    id: "1",
    event: "tick",
  });
});

Retry Directive

return createServerSentEvents((write) => {
  // Instruct client to retry after 5 seconds on disconnect
  void write({ type: "retry", time: 5000 });
});

Comments for Keep-Alive

return createServerSentEvents((write) => {
  // Send a comment ping manually
  void write({ type: "comment", text: "heartbeat" });
});