toverux / expresse Goto Github PK
View Code? Open in Web Editor NEWExpreSSE: A better module for working with Server-Sent Events in Express
License: MIT License
ExpreSSE: A better module for working with Server-Sent Events in Express
License: MIT License
Let me start by pointing out a bug I found when applying your proposed to solution using this module with expressjs/compression.
You suggest that res.flush()
be called after each res.sse.*
call, but keep-alive messages also call the problematic res.write
function, since this keep-alive logic happens outside of module user's reach we can't call flush after this happens.
This results in the client never receiving heartbeats until connection is closed or at user-level res.sse.* ; res.flush();
is called.
Secondly, what exactly are keep-alive messages for in this module?
Initially I thought they were a mechanism to be aware of a lost connection to a user, but there is no logic in place to notify module users of this.
So really what are they for?
I'm not able to receive events in the browser.
SERVER
res.sse.event("firstPaint", "10"); //
CLIENT
`var source = new EventSource('/api/v1/get_SEE/test');
source.addEventListener('message', function(e) {
console.log("message"); // never fired
}, false);
source.addEventListener('open', function(e) {
// this is fired correctly when the page is opened
console.log("connection opened");
}, false);
source.addEventListener('error', function(e) {
if (e.readyState == EventSource.CLOSED) {
// this is fired correctly in the case
console.log("connection closed");
}
}, false);`
I am using vite-express and having this issue when making a GET request from my frontend to the route:
res.sse.data('ok', someEvent)
^
TypeError: Cannot read properties of undefined (reading 'data')
I have used Typescript import:
import { ISseResponse, sse } from '@toverux/expresse'
let someEvent = 'foo'
router.get('/check', sse({ flushAfterWrite: true }) ,(res: ISseResponse) => {
setTimeout(() => {
res.sse.data('ok', someEvent)
}, 1000)
})
I've been trying to make example code work for the past couple of hours .but couldn't make it work .I dont have much experience with node js .If possible can you provide some more like noob friendly example code .
I'm stuck at
someModule.on('someEvent', (event) => {
Does this someModule needs to be an event emitter ?
hope you reply to this
I'm using the simple form
this.router.get('/:id/events', sse(), (req,res:ISseResponse) => {
someModule.on('someEvent', (event) =>
And what happens is that when I reload my client page a couple of times, the number of event listeners keep adding up (from the someModule.on
).
I can't depend on clients properly calling some "unregister" url, so I'm wondering if there's any way to detect clients which are no longer listening for SSE?
Hi,
I want to use sseHub()
with a different hub instance depending on the data in the request. Basically there are distinct sessions based on a token provided by the client, and each session should have its own hub. Since sseHub
accepts ISseHubMiddlewareOptions
that means I have to hard-code the hub, right? Is there any way to make it dynamic?
Basically, for each call of the API with the sseHub
middleware, I need to first examine the request body to determine which hub to use for that client going forward.
I feel like I am going to end up subclassing Hub
but I'm not sure of the direction, yet.
As stated in the documentation SSE encounters problem when undergoing compression. The proposed flushAfterWrite: true
is not effective for webpack-dev-server compression (which is used in create-react-app). To resolve this, one can set the 'Cache-Control' response header to 'no-cache, no-transform' (at least while not in production). I wasn't sure how to alter response headers using expresse, so I uninstalled.
On the browser:
We can see that it connected successfully (the first event), but I can't get it to send events afterwards?
I'm setting up the hub and middleware in an exclusive file. Which it works because it prints to the console only once.
import { Hub, sseHub } from '@toverux/expresse';
export const hub = (() => {
const hubInstance = new Hub();
console.log('INFO - SSE Hub created');
return hubInstance;
})();
export const hubMiddleware = sseHub({ hub, serializer: String });
Here's the router definition, which it works because we can access the endpoint as the screenshot above.
import hub from './sseHub';
server.get('/v1/companies/events', sseHub({ hub }), (req, res) => {
res.sse.event('subscribed:company', Date.now());
});
Here's another file where I subscribe to a Redis channel and want to transmit the data through the hub to others peers connected.
The console.log
after the hub.event
prints into the console right, so I'm not sure where I'm getting things wrong.
import { hub } from './sseHub';
export default function () {
const { port, host, db } = configRedis;
const client = redis.createClient({ host, port, db });
client.on('pmessage', (pattern, channel, message) => {
if (channel.includes('company:new:')) {
hub.event('eventname', { data: message }); // tried hub.event('eventname', 'event') too
console.log(`INFO - Nova Empresa (${channel.slice(-14)}) | TxID: ${message}`);
}
});
client.psubscribe('company:new:*');
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.