
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
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
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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.