mpetrunic / fastify-sse-v2

Provide Server-Sent Events to Fastify

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Event emitter example does not work

thomasdingemanse opened this issue · comments

I tried to run the code from the last example in the README:

import {FastifySSEPlugin} from "fastify-sse-v2";
import EventIterator from "event-iterator";

const server = fastify();
server.register(FastifySSEPlugin);

server.get("/", function (req, res) {
    const eventEmitter = new EventEmitter();
    res.sse(new EventIterator(
                (push) => {
                  eventEmitter.on("some_event", push)
                  return () => eventEmitter.removeEventListener("some_event", push)
                }
        )
    );
});

I changed "some event" to the events emitted by my event emitter ("scan"). But I still get the following error:

TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received an instance of Object
        at checkListener (events.js:111:11)
        at _addListener (events.js:348:3)
        at EventEmitter.addListener (events.js:406:10)
        at D:\Development\new-project\server\routes\index.js:25:21

Does this only work with certain versions of event-iterator? Or is there another issue I overlooked?

Try this instead:
const {EventIterator} = require('event-iterator')
If that works for you, then the example needs to be corrected.

Oh, I forgot to mention I've already tried that, but it doesn't work :(

Try to wrap push like ({push}). EventIterator changed api afaik.

Thanks @mpetrunic, that works!

unfortunately the example doesn't work for me.

// working example
res.sse((async function * source () {
          for (let i = 0; i < 10; i++) {
            sleep(2000);
            yield {id: String(i), data: "Some message"};
          }
    })());
// doesn't work
res.sse(new EventIterator.EventIterator(
      ({ push, stop, fail }) => {
        eventEmitter.addListener("data", push)

        return () => {
          this.removeListener("data", push)
        }
      })
    );

Do you use fastify-compress or something similar?
What error do you get?
What version of fastify? Can you share link to example?

my plugins are

"fastify": "^2.0.0",
    "fastify-autoload": "^1.0.0",
    "fastify-cli": "^1.1.0",
    "fastify-cors": "^2.1.3",
    "fastify-env": "^1.0.1",
    "fastify-file-upload": "^2.0.1",
    "fastify-jwt": "^1.0.0",
    "fastify-plugin": "^1.5.0",
    "fastify-sse-v2": "^1.0.7",
    "fastify-static": "^2.5.0",

no error, just can't receive messages. however I'm able to receive "open" and "close" events in browser

Can you share link to example?

probably on this week.. I don't have time enough to create an example from my sources

Btw, first example is working for you but not second? What version of EventIterator are you using?

"event-iterator": "^2.0.0"

It doesn't work because event doesn't contains data key.

The working examples are:

// pure nodejs without EventIterator library
   eventEmitter.emit('data', {data: 'data'}); // somewhere
    reply.sse((async function * source () {
      for await (let event of on(eventEmitter, 'data')) {
        console.log('event', event);
        yield {data: JSON.stringify(event[0])}
      }
   })());
// using EventIterator library
    reply.sse(new EventIterator.EventIterator(
      ({push}) => {
        const cb = (data) => {
          console.log('push', {data});
          push({data});
        }
        eventEmitter.on("request:created", cb);
        req.req.on("close", () => {
          eventEmitter.removeListener("request:created", cb)
          console.log('closed');
        });
        return () => eventEmitter.removeListener("request:created", cb)
      }
      )
    )