x-cubed / event-store-client

JS client library for connecting to Event Store over TCP/IP
The Unlicense
71 stars 24 forks source link

handling of "socket has been ended by the other party" #13

Closed deyhle closed 8 years ago

deyhle commented 8 years ago

After a few hours I always get the error

Error: This socket has been ended by the other party
    at Socket.writeAfterFIN [as write] (net.js:278:12)
    at Connection.sendMessage (/.../node_modules/event-store-client/lib/connection.js:504:21)
    at Connection.writeEvents (/.../node_modules/event-store-client/lib/connection.js:423:10)
    at /.../eventstore.js:43:18

My code (the line 43 from the stacktrace is the one with eventStore.writeEvents):

let connected = false;
const eventStore = new EventStore.Connection({
  host: config.eventStore.host,
  port: config.eventStore.port,
  debug: config.eventStore.debug,
  onError: (error) => {
    connected = false;
    logger.error(error);
  },
  onConnect: () => {
    logger.info('connected to eventstore');
    connected = true;
  },
});

function emitToEventStore(message) {
  if (!connected) {
    return Promise.reject(new Error('EventStore not connected'));
  }
  return Promise.resolve()
  .then(() => {
    const event = {
      ...
    };
    return new Promise((resolve, reject) => {
      eventStore.writeEvents('streamname', EventStore.ExpectedVersion.Any, true, [event], null, (result) => {
        if (result.message) {
          return reject(new Error(result.message, result));
        }
        return resolve();
      });
    });
  })
  .then(() => logger.info(`posted message # ${message.properties.messageId} to eventstore`))
  .catch((error) => {
    logger.error(`could not post message # ${message.properties.messageId}`, error);
  });
}

Is there any way to react to a disconnect?

x-cubed commented 8 years ago

Hi Ruben,

It's possible that the reason you're seeing the remote end drop the connection is because EventStore thinks that the connection is idle and not being used. I'd recommend using the Connection.sendPing function on a regular basis to keep the connection alive.

There's currently no easy way with this library to handle socket-level errors such as remote disconnection, but you're welcome to submit a Pull Request if you want to go ahead and implement support for handling them.

x-cubed commented 8 years ago

This case duplicates #8

deyhle commented 8 years ago

Apparently it's another issue. I added sendPing requests now that fire every 5 minutes, but I see the following in the log:

2016-07-11T07:26:42.289Z - info: connected to eventstore
2016-07-11T07:31:42.278Z - debug: received pong response from eventstore
2016-07-11T07:36:42.280Z - debug: received pong response from eventstore
-- many more in 5 minute intervals --
2016-07-11T11:06:42.304Z - debug: received pong response from eventstore
2016-07-11T11:11:41.652Z - error:  Error: This socket has been ended by the other party
    at Socket.writeAfterFIN [as write] (net.js:278:12)
    at ...

I'll probably make new connections now to solve the problem.