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

ts-express-sse

v1.0.3

Published

A Strongly typed server sent events express middleware for quick & easy node express sse implementation

Downloads

18

Readme

ts-express-sse

npm version Build Status Code Climate codecov

NPM

A Strongly typed server sent events express middleware for quick & easy node express sse implementation.

This module is strongly typed you'll get useful helpers while coding! You can use both in javascript and typescript projects.

About

ts-express-sse is designed for simplicity. Need to send server-sent events without unnecessary complexity or fallback mechanisms? This library makes it easy.

Installation:

npm install --save ts-express-sse

or

yarn add ts-express-sse

Usage example:

Options:

You can pass an optional options object to the constructor. Currently it only supports changing the way initial data is treated. If you set isSerialized to false, the initial data is sent as a single event. The default value is true.

const sse = new SSE(["Hello world", "Node express sse", "ts-express-sse"], { isSerialized: false, initialEvent: 'optional initial event name' });

Example 1 - Server:

const SSE = require('ts-express-sse');
const express = require('express');
const http  = require ("http") 

// express app
const app = express();
// create sse instance
const sse = new SSE(["Hello world", "Node express sse", "ts-express-sse"]);

app.get('/stream', sse.init);

const content = "sse node js test";

const eventName = "my event";

const customID = Math.random()

// send content 
sse.send(content);
// send content with custom event name
sse.send(content, eventName);
// send content with custom event & custom ID
sse.send(content, eventName, customID);
// Update the data initially served by the SSE stream
sse.updateInit(["array", "containing", "new", "content"]);
// Data to be serialized as a series of events
sse.serialize(["data", "to", "be", "sent", "as", "serialized", "events"]);

// get port
const PORT = process.env.PORT || 8081

// create server
const server =  http.createServer(app);
// Start Server on port 8081
server.listen(PORT, function () {
  console.log(`App started: http://localhost:${PORT}`, );
});

Example 2 - Server ES support:

import SSE from 'ts-express-sse';
import express from 'express'
import http from "http"

// express app
const app = express();
// create an sse instance
const sse = new SSE(["Hello world", "Node express sse", "ts-express-sse"]);

app.get('/stream', sse.init);

const content = "sse node js test";

// send content 
sse.send(content);

// get port
const PORT = process.env.PORT || 8081

// create server
const server =  http.createServer(app);
// Start Server on port 8081
server.listen(PORT, function () {
  console.log(`App started: http://localhost:${PORT}`, );
});

Example 3 - Server - Ping Clients to keep sse active:

import SSE from 'ts-express-sse';
import express from 'express';

// express app
const app = express();

const sse = new SSE(["Node express sse", "ts-express-sse"]);

app.get('/stream', sse.init);

// constantly send sse stream to keep connected clients active to avoid disconnection due to inactivity
setInterval(() => {
  sse.send("Hello, server is active", 'ping');
}, 25000);

// get port
const PORT = process.env.PORT || 8081

// create server
const server =  http.createServer(app);
// Start Server on port 8081
server.listen(PORT, function () {
  console.log(`App started: http://localhost:${PORT}`, );
});

Example 4 - Server - use with compression:

Incase ts-express-sse doesn't work as expected when using it with compression most especially with the latest express version, please use the workaround below;

import SSE from 'ts-express-sse';
import express from 'express'
import http from "http"
import compression from
// express app
const app = express();

// add this workaround
app.use(compression({
  filter: (req, res) =>{
    const outgoingHeaders = res?.getHeaders()
    const contentType = outgoingHeaders['content-type']
    return !contentType?.toString().includes("text/event-stream")
  }
}));
// create an sse instance
const sse = new SSE(["Hello world", "Node express sse", "ts-express-sse"]);

app.get('/stream', sse.init);

const content = "sse node js test";

// send content 
sse.send(content);

// get port
const PORT = process.env.PORT || 8081

// create server
const server =  http.createServer(app);
// Start Server on port 8081
server.listen(PORT, function () {
  console.log(`App started: http://localhost:${PORT}`, );
});

Client - HTML project or React or Nextjs:

const es = new EventSource('http://localhost:8081/stream', );

// // or
// const es = new EventSource('http://localhost:8081/stream', {withCredentials: true} );

es.onmessage = function (event) {
  console.log(event.data)
};

es.addEventListener(eventName, function (event) {
  console.log(event.data)
});

Support us

You enjoyed using this package?

Happy coding!