Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
express-eventstream
Advanced tools
A library to easily create web endpoints that stream events to clients
A library to easily create web endpoints that stream events to clients.
These endpoints speak server-sent events (SSE), so can be easily consumed in web browsers using EventSource.
The library is GRIP-aware, which means it can be used with Pushpin or Fanout Cloud for easy scaling.
aco
parameter to your routes config fileCreate an events
object, with optional GRIP configuration
// server.js
const expressEventStream = require('express-eventstream')
const grip = process.env.GRIP_URL // e.g. 'http://localhost:5561?key=changeme'
const events = expressEventStream.events({ grip })
Create and mount the express handler wherever you want
const yourApp = require('express')()
yourApp.get('/events/', expressEventStream.express({ events, grip })
Publish events throughout your app
yourApp.post('/messages/', (req, res, next) => {
req.pipe(require('concat-stream')((reqBody) => {
events.channel('messages').write({
event: 'message',
data: {text: reqBody.toString()}
})
res.status(201).end()
}))
})
Note: If you're not using GRIP, and your application has several processes running, published events will only go to HTTP Connections on the process that publishes the message. To scale to more than one web server process, you'll need to use a GRIP-compatible proxy such as Pushpin or Fanout Cloud, and make sure you publish each event from one place.
Stream events to your web client using EventSource
// client.js
var eventSrc = new EventSource('/events/?channel=messages')
.addEventListener('message', function (event) {
console.log('message is', event.data)
})
.addEventListener('stream-error', function (event) {
console.error('stream-error', event)
this.close()
})
The express handler created via .express()
behaves as follows.
Make an HTTP GET request to your endpoint. You decide the path to mount it on. The endpoint will interpret the following querystring parameters:
.events({ prefix })
.express-eventstream endpoints respond with text/event-stream responses. The Events in this Stream are one of a few predefined types, plus any others that you instruct it to publish. All of these events are prefixed with 'stream-', so consider using another prefix for your application's events.
If a 'data' field is present on these events, it will be a JSON-encoded string.
FAQs
A library to easily create web endpoints that stream events to clients
The npm package express-eventstream receives a total of 12 weekly downloads. As such, express-eventstream popularity was classified as not popular.
We found that express-eventstream demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 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
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.