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

statele-sse

v0.1.2

Published

Zero-dependency framework-agnostic EventSource client for StateleSSE

Readme

statele-sse

Zero-dependency EventSource client for StateleSSE.AspNetCore. Framework-agnostic — works with React, Vue, Svelte, Angular, or vanilla JS.

Install

npm i statele-sse

Quick start

import { StateleSSEClient } from 'statele-sse'

const sse = new StateleSSEClient('http://localhost:5000/sse')

const unsub = sse.listen(
    (id) => fetch(`/GetMessages?connectionId=${id}&roomId=abc`).then(r => r.json()),
    (messages) => console.log(messages)
)

// later:
unsub()

The constructor opens an EventSource and stores the connectionId from the server. listen calls your API with the connectionId, delivers initial data, then listens for SSE updates on the returned group. Returns a cleanup function.

API

new StateleSSEClient(url, connectEvent?)

| Param | Default | Description | |---|---|---| | url | — | Your SSE endpoint (e.g. http://localhost:5000/sse) | | connectEvent | 'connected' | SSE event name that delivers the connectionId |

Auto-connects immediately. Calls made to listen before the connection is ready are queued.

sse.listen<T>(register, onData, onError?)

const unsub = sse.listen<Room[]>(
    (connectionId) => api.getRooms(connectionId),
    (rooms) => renderRooms(rooms),
    (error) => console.error(error)
)

register must return Promise<{ group: string; data?: T }>. This matches the RealtimeListenResponse<T> from StateleSSE.AspNetCore.

Returns a cleanup function that removes the listener.

sse.connectionId

The connection ID assigned by the server, or null if not yet connected.

sse.status

'connecting' | 'connected' | 'disconnected'

sse.onStatusChange

sse.onStatusChange = (status) => console.log(status)

sse.disconnect()

Closes the EventSource and clears all listeners.

Reconnection

EventSource auto-reconnects natively. When the server assigns a new connectionId, all active listen subscriptions re-register automatically — server-side group membership and queries are restored without any consumer code.

Framework examples

React

const SseContext = createContext<StateleSSEClient>(null!)

function SseProvider({ url, children }: { url: string; children: ReactNode }) {
    const [sse] = useState(() => new StateleSSEClient(url))
    useEffect(() => () => sse.disconnect(), [sse])
    return <SseContext.Provider value={sse}>{children}</SseContext.Provider>
}

function useRealtimeListen<T>(
    register: (id: string) => Promise<{ group: string; data?: T }>,
    onData: (data: T) => void,
    deps: unknown[] = []
) {
    const sse = useContext(SseContext)
    useEffect(() => sse.listen(register, onData), [sse.connectionId, ...deps])
}

Usage:

// in main.tsx
<SseProvider url="http://localhost:5000/sse">
    <App />
</SseProvider>

// in a component
const [rooms, setRooms] = useState<Room[]>([])
useRealtimeListen(
    (id) => api.getRooms(id),
    (data) => setRooms(data)
)

Vue

const sse = new StateleSSEClient('http://localhost:5000/sse')

export function useRealtimeListen<T>(
    register: (id: string) => Promise<{ group: string; data?: T }>,
    onData: (data: T) => void
) {
    onMounted(() => { unsub = sse.listen(register, onData) })
    onUnmounted(() => unsub?.())
}

Vanilla JS

const sse = new StateleSSEClient('http://localhost:5000/sse')

const unsub = sse.listen(
    (id) => fetch(`/GetRooms?connectionId=${id}`).then(r => r.json()),
    (rooms) => document.getElementById('rooms').textContent = JSON.stringify(rooms)
)

Server setup

I recommend seeing the full server docs on http://github.com/uldahlalex/statelesse but here's a brief walkthrough:

Install the NuGet package:

dotnet add package StateleSSE.AspNetCore

Minimal server (in-memory, single instance):

// Program.cs
builder.Services.AddInMemorySseBackplane();
builder.Services.AddControllers();

With EF Core realtime queries:

// Program.cs
builder.Services.AddInMemorySseBackplane();  // or AddRedisSseBackplane() for scaling
builder.Services.AddEfRealtime();
builder.Services.AddDbContext<MyDbContext>((sp, options) => {
    options.UseNpgsql(connectionString);
    options.AddEfRealtimeInterceptor(sp);
});

Controller:

public class ChatController(ISseBackplane backplane, IRealtimeManager realtimeManager, MyDbContext ctx)
    : RealtimeControllerBase(backplane)
{
    [HttpGet(nameof(GetRooms))]
    public async Task<RealtimeListenResponse<List<Room>>> GetRooms(string connectionId)
    {
        var group = "rooms";
        await backplane.Groups.AddToGroupAsync(connectionId, group);

        realtimeManager.Subscribe<MyDbContext>(connectionId, group,
            criteria: changes => changes.OfType<Room>().Any(),
            query: async c => await c.Rooms.ToListAsync());

        return new RealtimeListenResponse<List<Room>>(group, ctx.Rooms.ToList());
    }
}

Any SaveChanges that touches a Room entity will re-execute the query and broadcast the result to all clients listening on the "rooms" group.

License

MIT