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

onewaydata

v6.3.2

Published

[Server sent events](https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events) for NodeJs, works with raw http, express and polka.

Downloads

28

Readme

onewaydata

Server sent events for NodeJs, works with raw http, express and polka.

to be used with EventSource API client side.

install

npm i onewaydata

usage

import { 
    createEventStream,
    sendOne,
    RECONNECT,
    CONNECT,
    DISCONNECT,
} from "onewaydata";
import { useDefaultLogging } from "onewaydata/source/defaultLogging.js";


const server = ...
const condition = (request, response) => {
    return request.url === `/sse`;
};
const eventStream = createEventStream({server, condition, reconnectionTime: 5000 });
useDefaultLogging({ eventStream });


eventStream.send({ data: `data only`)});
eventStream.send({ data: `something`,  event: `eventName`, id: String(Date.now())});
eventStream.on(RECONNECT, ({lastId, response}) => {
    if (lastId) {
        // opportunity to resume with sendOne
        sendOne(response, `here is what you missed since last disconnection`);
    }
});

// eventStream.off(RECONNECT); // unsubscribe
// eventStream.close(); // closes all eventStream 
// eventStream.setAndSendId(); // shorthand to send id only and saving lastEventId
// same as send but takes a filter function 
eventStream.sendWithCondition({
    data: `latitude=434521, longitude=23213`,
    event: "fire started",
    function (request, response) {
        // request and response are from http because SSE is built on top of HTTP protocol
        // assumes some request have this property set before hand
        return request.isFirefighter; // only send to isFirefighter === true
    },
);

with express/polka

The main difference is that the path is handled at the web framework level.

import { createEventStream } from "onewaydata";
import polka from "polka";


const polkaServer = polka();
const eventStream = createEventStream({ asMiddleWare: true });
polkaServer.use(`/sse`, eventStream.middleWare);
polkaServer.listen(PORT);

eventStream.send({ data: `something`,  event: `eventName`});

See examples

Extras

More information

https://hpbn.co/server-sent-events-sse/

Changelog

changelog.md

License

CC0