Onewaydata

Posted by Cyril Walle

Last edit

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 } from "onewaydata";
import { useDefaultLogging } from "onewaydata/source/defaultLogging.js";


const server = ...

const eventStream = createEventStream({server, path: `/sse`});
useDefaultLogging({ eventStream });


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

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/

License

CC0