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

Upgrade to 1.x of fastify #3

Open
edson-gaspar opened this issue Mar 29, 2018 · 7 comments
Open

Upgrade to 1.x of fastify #3

edson-gaspar opened this issue Mar 29, 2018 · 7 comments

Comments

@edson-gaspar
Copy link

Any plan to upgrade to version 1.x of fastify dependence?

@piranna
Copy link
Contributor

piranna commented Dec 19, 2018

Any update on this? There are three forks of this project and all of them are just to add this fix... Can it be merged?

@piranna
Copy link
Contributor

piranna commented Jan 11, 2019

This can be clossed, just only it would be nice to publish a new version in npm (ideally with my new pull-request to add support for fastify 2.0).

@ancms2600
Copy link

ancms2600 commented Jul 27, 2019

ppl you don't need an npm module for this. its basics.

// RFC: https://www.w3.org/TR/eventsource/

const EventStream = {
  setup(req, reply, closeCb) {
    reply.type('text/event-stream');
    reply.header('content-encoding', 'identity');
    reply.header('cache-control', 'no-cache');
    const stream = new require('stream').PassThrough({ allowHalfOpen: false });
    stream.on('error', e => {
      console.error(`EventStream Error: ${e.stack}`);
    });		
    stream.write('\n');
    req.raw.stream.on('close', async (...args) => {
      stream.ended = true;
      stream.end();
      await closeCb(...args);
    });
    reply.send(stream);
    return stream;
  },

  emit(stream, id, data) {
    if (null == stream || true === stream.ended) return;
    stream.write(`id: ${id}\n`);
    stream.write(`data: ${JSON.stringify(data)}\n\n`);
  },
};

app.get('/event-stream', async (req, reply) => {
  console.debug('user connected to event stream');
  const stream = EventStream.setup(req, reply, () => {
    console.debug('user disconnected from event stream.');
  });

  EventStream.emit(stream, 'userAdd', {
    name: req.user.name,
  });
});

@wmertens
Copy link

@ancmikesmullin that's already quite a lot of code to come up with without mistakes.

Also, shouldn't SSE set the TCP keepalive flag?

@ancms2600
Copy link

I should add I am using HTTP/2 :)

@wmertens
Copy link

@ancmikesmullin That's why I'm looking into SSE, because websockets are so complex and SSE over HTTP/2 is so much simpler :)

I'm trying to find the best way to do GraphQL subscriptions and I think I'll be able to do a POST resulting in an event stream. Browsers don't support that but https://github.com/mpetazzoni/sse.js does.

So what you mean is that with HTTP/2, setting TCP keepalive is not needed/not an option, right?

@ancms2600
Copy link

Yes and I learned about it because I initially tried to set it, but got warnings in Firefox or something. Can't recall specifics but the error message was informative. Try it and see.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants