Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
better-sse
Advanced tools
Dead simple, dependency-less, spec-compliant server-side events implementation for Node, written in TypeScript.
A dead simple, dependency-less, spec-compliant server-side events implementation for Node, written in TypeScript.
This package aims to be the easiest to use, most compliant and most streamlined solution to server-side events with Node that is framework agnostic and feature rich.
Please consider starring the project on GitHub ⭐.
Server-sent events (SSE) is a standardised protocol that allows web-servers to push data to clients without the need for alternative mechanisms such as pinging or long-polling.
Using SSE can allow for significant savings in bandwidth and battery life on portable devices, and will work with your existing infrastructure as it operates directly over the HTTP protocol without the need for the connection upgrade that WebSockets require.
Compared to WebSockets it has comparable performance and bandwidth usage, especially over HTTP/2, and natively includes event ID generation and automatic reconnection when clients are disconnected.
event-source-polyfill
and eventsource-polyfill
.See a comparison with other Node SSE libraries in the documentation.
# npm
npm install better-sse
# Yarn
yarn add better-sse
# pnpm
pnpm add better-sse
Better SSE ships with types built in. No need to install from @types
for TypeScript users!
The following example shows usage with Express, but Better SSE works with any web-server framework (that uses the underlying Node HTTP module).
See the Recipes section of the documentation for use with other frameworks and libraries.
Use sessions to push events to clients:
// Server
import {createSession} from "better-sse";
app.get("/sse", async (req, res) => {
const session = await createSession(req, res);
session.push("Hello world!");
});
// Client
const sse = new EventSource("/sse");
sse.addEventListener("message", ({data}) => {
console.log(data);
});
Use channels to send events to many clients at once:
import {createSession, createChannel} from "better-sse";
const channel = createChannel();
app.get("/sse", async (req, res) => {
const session = await createSession(req, res);
channel.register(session);
channel.broadcast("A user has joined.", "join-notification");
});
Loop over sync and async iterables and send each value as an event:
const session = await createSession(req, res);
const list = [1, 2, 3];
await session.iterate(list);
Pipe readable stream data to the client as a stream of events:
const session = await createSession(req, res);
const stream = Readable.from([1, 2, 3]);
await session.stream(stream);
Check the API documentation and live examples for information on getting more fine-tuned control over your data such as managing event IDs, data serialization, event filtering, dispatch controls and more!
API documentation, getting started guides and usage with other frameworks is available on GitHub.
This library is always open to contributions, whether it be code, bug reports, documentation or anything else.
Please submit suggestions, bugs and issues to the GitHub issues page.
For code or documentation changes, submit a pull request on GitHub.
Install Node:
curl -L https://git.io/n-install | bash
n auto
Install pnpm:
npm i -g pnpm
Install dependencies:
pnpm i
Run tests:
pnpm t
This project is licensed under the MIT license.
0.8.0 - 2022-06-02
Session#flush
method.Pragma
, X-Accel-Buffering
headers and add additional values to the Cache-Control
default header to further disable all forms of caching.Http2ServerRequest
and Http2ServerResponse
objects to the Session
constructor req
and res
parameters, respectively.Session#event
, Session#data
, Session#id
, Session#retry
and Session#comment
methods to write to the internal data buffer instead of sending the field data over the wire immediately.Session#dispatch
method to only write a newline (and to the internal data buffer) and not flush the written data to the client.Channel#broadcast
method to generate its own custom event ID and thus add it as an additional argument to its broadcast
event callback function.Channel#register
and Channel#deregister
to not do anything if the channel is already registered or deregistered, respectively.Session
constructor options header
field to overwrite conflicting default headers instead of being ignored.session-disconnected
being fired after instead of before the session is deregistered.null
to Session#id
. Give no arguments at all instead.FAQs
Dead simple, dependency-less, spec-compliant server-sent events implementation for Node, written in TypeScript.
The npm package better-sse receives a total of 721 weekly downloads. As such, better-sse popularity was classified as not popular.
We found that better-sse demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.