Skip to content
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

Closed
scottcorgan opened this issue Aug 8, 2013 · 10 comments
Closed

Example of using Hapi with SSEs (Server Sent Events) #1008

scottcorgan opened this issue Aug 8, 2013 · 10 comments
Labels
documentation Non-code related changes

Comments

@scottcorgan
Copy link

An example of how to use SSEs with Hapi would be fantastic. Not sure where to start with using them in the framwork.

@hueniverse
Copy link
Contributor

Is there a SSE module you are familiar with?

@scottcorgan
Copy link
Author

I'm not too familiar with any and the only examples I've seen using SSEs
with Node is for express.
On Aug 8, 2013 1:47 PM, "Eran Hammer" notifications@github.com wrote:

Is there a SSE module you are familiar with?


Reply to this email directly or view it on GitHubhttps://github.com//issues/1008#issuecomment-22354307
.

@hueniverse
Copy link
Contributor

Point me to the express example and we'll port it.

@scottcorgan
Copy link
Author

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

@dacbd
Copy link
Contributor

dacbd commented Aug 13, 2013

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();
  });
});

@mainiak
Copy link

mainiak commented Sep 14, 2013

Hi.

I want to run hapi.js on Heroku and without WebSocket support i wanted to switch to SSE.
But I am lacking deep knowledge about Hapi internals.
Could you, please, guide me to complete my plugin?

https://github.com/mainiak/hapi-sse

  1. How should I keep connection open without violating plugin chain?
  2. How should I avoid caching for one url/path without conflicting caching plugins or anything else?
  3. What is the best way to export EventEmitter inside Hapi for raising messages towards client?

Thank you for response in advance.

Best regards, Jakub V.

@hueniverse
Copy link
Contributor

Has anyone made progress on this? Checking before I invest time in it.

@mainiak
Copy link

mainiak commented Oct 3, 2013

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.

@mainiak
Copy link

mainiak commented Oct 3, 2013

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 :-/
mainiak/hapi-sse@6bc62f6

@binarymist
Copy link

@Marsup Marsup added documentation Non-code related changes and removed example labels Sep 20, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Mar 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Non-code related changes
Projects
None yet
Development

No branches or pull requests

6 participants