RemoteMetering / geteventstore-promise

An EventStore http+tcp wrapper package
MIT License
31 stars 17 forks source link

Could not catch events by subscribeToStream #80

Closed kss943 closed 4 years ago

kss943 commented 4 years ago

I have two files app.js:

async function boot(){
  const EventStore = require('geteventstore-promise');
  const client = new EventStore.TCPClient({
    hostname: 'localhost',
    port: 1113,
    credentials: {
        username: 'admin',
        password: 'changeit'
    }
  });
  function onEventAppeared(ev) {
     console.log(ev)
    return;
  };
  function onDropped(subscription, reason, error) {
     console.log(error)
  };
  await client.subscribeToStream('TestStream', onEventAppeared, onDropped, false)
  console.log('Event stream beginning... ')
}
boot()

event.js:

async function boot() {
  const testStream = 'TestStream'
  const EventStore = require('geteventstore-promise');
  const uuid = require('uuid');
  try {
    const client = new EventStore.HTTPClient({
        hostname: 'localhost',
        port: 2113,
        credentials: {
            username: 'admin',
            password: 'changeit'
        }
    });
    const events = [new EventStore.EventFactory().newEvent('TestEventType', { something: '456'})];
    await client.writeEvents('TestStream-' + uuid.v4(), events);
  } catch (e) {
    console.trace(e)
  }
}
boot()

When i run node app.js and after that try to write stream node event.js function onEventAppeared don't react . What can i implement this ?

mmuller99 commented 4 years ago

Hi @kss943,

It seems that you are appending an ID to the stream name when you are writing the event, but only subscribing to TestStream without the appended ID. Perhaps you want to subscribe to the category projection, $ce-TestStream to receive all TestStream events

kss943 commented 4 years ago

@mmuller99 Did you mean to add $ce in line of code below? await client.subscribeToStream('$ce-TestStream', onEventAppeared, onDropped, false) It doesn't work too. Stream appeared at EventStore 'Stream Browser' but onEventAppeared function doesn't work.

mmuller99 commented 4 years ago

Did the event appear on the $ce-TestStream as well?

kss943 commented 4 years ago

@mmuller99 I got this in Stream Browser but onEventAppeared function did not log it:

Screenshot 2019-12-19 at 15 58 00
mmuller99 commented 4 years ago

So neither onEventAppeared and onDropped got called at all? I do see that our Readme still states the handler as onEventAppeared(ev) instead of what it is now onEventAppeared(subscription, ev).

kss943 commented 4 years ago

@mmuller99 onDropped() work, for example when i close EventStore container.

As i see this function is called after event creating (node event.js) in subscribeToStream.js file of library:

onEvent = function onEvent(sub, ev) {
                 var mappedEvent = (0, _mapEvents.default)([ev])[0];
                  if (mappedEvent) onEventAppeared(mappedEvent);
                };

When the line var mappedEvent = (0, _mapEvents.default)([ev])[0]; starts executing it call this._log.error(err, "Exception during executing user callback: %s.", err.message); in SubscriptionOperation.prototype._executeActions which belongs to node-eventstore-client library. err.message = "Unexpected token @ in JSON at position 1"

While using node-eventstore-client with its function subscribeToAll(), onEventAppeared is called

mmuller99 commented 4 years ago

Thanks, I see you've opened up a PR for the readme fix. Would it be possible for you to please replicate your scenario with a failing test within the library?

kss943 commented 4 years ago

I just started $by_category projection via EventStore web interface and it works now. Also added second parameter to onEventAppeared and now this function work. So, is it important always start $by_category projection? Maybe that must be added to readme?