Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
@evanshortiss/fastify-sse
Advanced tools
Easily send Server-Send-Events with Fastify.
This is based on github.com/mtharrison/susie
npm install --save fastify-sse
Add it to you project with register
and you are done!
You can now configure a new route, and call the new reply.sse()
to send Events to browser.
When you have finished sending event, you could send an empty message, or if using stream and end of stream, an
end
event will be fired just before closing the connection. You could work with it browser side to prevent
automatic reconnection.
// Register the plugin
fastify.register(require("fastify-sse"), (err) => {
if (err) {
throw err;
}
});
// Define a new route in hapijs notation
fastify.route({
method: "GET",
url: "/sse-hapi",
handler: (request, reply) => {
let index = 0;
const options = {};
const cleanUp = () => {
fastify.log.info('cleaning up interval after disconnect')
clearInterval(interval)
}
// Send the first data
reply.sse("sample data", options);
// Handle client disconnect event
request.socket.on('close', cleanUp)
// Send a new data every seconds for 10 seconds then close
const interval = setInterval(() => {
index += 1
reply.sse({event: "test", data: index});
if (!(index % 10)) {
// Close the socket
reply.sse();
}
}, 1000);
}
});
// Define a new route in express notation
fastify.get("/sse-express",(request, reply) => {
let index;
const options = {};
// Send the first data
reply.sse("sample data", options);
// Send a new data every seconds for 10 seconds then close
const interval = setInterval(() => {
reply.sse({event: "test", data: index});
if (!(index % 10)) {
reply.sse();
clearInterval(interval);
}
}, 1000);
});
The options
are used only for the first call, subsequent ignore it.
You could specify:
strings
that well be sent directlybuffers
that will be converted beck to strings, utf8 encodedobjects
that will be stringified with the use of "fast-safe-stringify"
streams
that are readables, and could deals with objectMode or notidGenerator
: generate the event id, defaulting to a number incrementing, from 1event
: can be a string for the event name, or a function to compute the event nameIt must be a function that will be called with the event in parameter, and must return a string that will be the
id
of the SSE, or it could be null
if no id is needed.
Using a function:
reply.sse("message", {
idGenerator: (event) => {
// Retrieve the event name using the key myIdentifiant or use the timestamp if not exists …
return event.myIdentifiant || (new Date()).getTime();
}
});
It will transmit:
id: 1504624133267
data: message
Do not display id
, so pass null:
reply.sse("message", {idGenerator: null});
It will transmit:
data: message
It could be:
.on("eventName", […])
..on("message", […])
.reply.sse({myEventName: "myEvent", hello: "world"}, {
event: (event) => {
// Retrieve the event name using the key myEventName …
const name = event.myEventName;
// … delete it from the object …
delete event.myEventName;
// then return the name
return name;
}
});
It will transmit:
id: 1
event: MyEvent
data: {"hello":"world"}
FAQs
Provide Server-Sent Events to Fastify
We found that @evanshortiss/fastify-sse demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.