New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example of using Hapi with SSEs (Server Sent Events) #1008
Comments
Is there a SSE module you are familiar with? |
I'm not too familiar with any and the only examples I've seen using SSEs
|
Point me to the express example and we'll port it. |
Here's a method that just adds the headers: module.exports.writeSSEData = function (req, res, event, data, cb) {
var id = (new Date()).toLocaleTimeString();
res.write("id: " + id + '\n');
res.write("event: " + event + "\n");
res.write("data: " + JSON.stringify(data) + "\n\n");
if (cb) {
return cb(req, res);
}
}; Taken from this page: http://www.activestate.com/blog/2013/07/server-sent-events-aura-and-nodejs |
a bit more/another example taken from here: http://tomkersten.com/articles/server-sent-events-with-node/ app.get('/update-stream', function(req, res) {
// let request last as long as possible
req.socket.setTimeout(Infinity);
var messageCount = 0;
var subscriber = redis.createClient();
subscriber.subscribe("updates");
// In case we encounter an error...print it out to the console
subscriber.on("error", function(err) {
console.log("Redis Error: " + err);
});
// When we receive a message from the redis connection
subscriber.on("message", function(channel, message) {
messageCount++; // Increment our message count
res.write('id: ' + messageCount + '\n');
res.write("data: " + message + '\n\n'); // Note the extra newline
});
//send headers for event-stream connection
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
res.write('\n');
// The 'close' event is fired when a user closes their browser window.
// In that situation we want to make sure our redis channel subscription
// is properly shut down to prevent memory leaks...and incorrect subscriber
// counts to the channel.
req.on("close", function() {
subscriber.unsubscribe();
subscriber.quit();
});
}); |
Hi. I want to run hapi.js on Heroku and without WebSocket support i wanted to switch to SSE. https://github.com/mainiak/hapi-sse
Thank you for response in advance. Best regards, Jakub V. |
Has anyone made progress on this? Checking before I invest time in it. |
I am suffering with node.js streams and application design, but I still want to finish it. See https://github.com/mainiak/hapi-sse/blob/work/lib/index.js I am wondering what's best solution. I want to have one object instance on server for pushing messages to different clients. And I need handle non-closing connections with every client for at least 5 minutes (Heroku timeout limit). Streams helped me to avoid closing of connections but even with one PassThrough is not good approach for several clients. |
Ok, I think I have most-simple-implementation done. Please, checkout my master and try to play with it. This was working for me - mainiak/hapi-sse@68709f2 This isn't working right now, but it should :-/ |
An example of how to use SSEs with Hapi would be fantastic. Not sure where to start with using them in the framwork.
The text was updated successfully, but these errors were encountered: