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 🙏

© 2024 – Pkg Stats / Ryan Hefner

remix-sse

v5.0.0

Published

Server Side Events (SSE) for Remix, made easy.

Downloads

405

Readme

remix-sse

Server Side Events (SSE) for Remix, made easy.

Features

  • ✅ Zero-dependencies
  • ✅ Small bundle
  • ✅ Context for re-using event source connections across remix application
  • ✅ supports multiple event types from a single emitter
  • ✅ 100% typescript
  • ✅ (experimental) Typed deserialization

Planned

  • 👷 Topic support - pass a topic string to useSubscribe and only listen to events that match the topic
  • 👷 Emitter type support - ensures the messages you are sending are in a format your hooks are expecting

Installation

npm i remix-sse

Documentation

See examples directory.

Quick Start

See basic example for more detail.

  1. Setup your event source route, here it is called /routes/emitter.tsx for simplicity

Note: This MUST be a resource route, you cannot return a component from this route.

import { EventStream } from 'remix-sse'
export const loader: LoaderFunction = ({ request }) => {

  // Return the EventStream from your route loader
  return new EventStream(request, (send) => {
    // In the init function, setup your SSE Event source
    // This can be any asynchronous data source, that will send
    // events to the client periodically

    // Here we will just use a `setInterval`

    const interval = setInterval(() => {
      // You can send events to the client via the `send` function
      send('greeting', JSON.stringify({ hello: 'world'}))
    }, 1000)


    return () => {
      // Return a cleanup function
      clearInterval(interval)
    };
  });
};

Note: the first argument passed to the send function is the EventKey, this can be anything you want - but you will need to reference it again via useSubscribe.

  1. Wrap your root.tsx with RemixSseProvider.

import { RemixSseProvider} from 'remix-sse/dist/client'


<RemixSseProvider>
  <Outlet />
</RemixSseProvider>

Note: v4 has temporarily broken the flat file structure we used to have ie. remix-sse/dist/client instead of remix-sse/client

  1. Call the useEventSource to setup an EventSource in your browser
import { useEventSource } from 'remix-sse/dist/client'
useEventSource('/emitter');
  1. Call useSubscribe from anywhere in your tree to begin listening to events emitted from the event source
// This value is a react state object, and will change everytime
// an event is emitted

// By default this is a string[]
const greeting = useSubscribe('/emitter', 'greeting')

// But you can return only the latest event as follows
const latestGreeting = useSubscribe('/emitter', 'greeting', {
  returnLatestOnly: true
})

// Or you can type the return by deserializing the event data
const typedGreeting = useSubscribe('/emitter', 'greeting', {
  returnLatestOnly: true,
  deserialize: (raw) => JSON.parse(raw) as Greeting
})

Deserialize

By default the data returned from useSubscribe is a string[]

You can pass a deserialize function to de-deserialize each event as it comes in.

Note: this feature is experimental and is subject to change.

See deserialize for more details.

useSubscribe options

| Option | Description | Default | | ------------------- | ----------------------------------------------------------------------------------------- | ------- | | maxEventRetention | The maximum number of events that will be kept. | 50 | | returnLatestOnly | Returns only the most recently emitted event - ie. returns TEvent instead of TEvent[] | false |

Experimental options

These are currently being tested, and are subject to change at any point.

| Option | Description | Default | | ------------- | ---------------------------------------------------------------------------------------------------------------------------------- | --------- | | deserialize | A function that will receive the raw event data and returns a deserialized value. See deserialize example | undefined |

.