toverux/expresse

sse-keep-alive instead of message - Silent Error on Hub.emit() ?

tloriato opened this issue · 1 comments

On the browser:
Screen Shot 2019-07-25 at 5 10 11 PM
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:*');
}

I managed to get it working.

Here's what had to be changed.

from the first snippet
export const hubMiddleware = _hub => sseHub({ hub: _hub, serializer: String});

from the second snippet

import { hub, hubMiddleware } from './sseHub';

server.get('/v1/companies/events', hubMiddleware(hub), (req, res) => {
    res.sse.event('subscribed:company', Date.now());
  });