nats-io / stan.js

Node.js client for NATS Streaming
Apache License 2.0
292 stars 49 forks source link

Subscription protocol response message can be received after a message #31

Closed StarpTech closed 7 years ago

StarpTech commented 7 years ago

When I remove the first line it works.

const nats = require('nats').connect()
const natsStreaming = require('node-nats-streaming').connect('test-cluster', 'test')

natsStreaming.on('connect', function () {

  natsStreaming.publish('foo', 'Hello node-nats-streaming!', function (err, aGuid) {
    console.log('Error publishing: ' + aGuid + ' - ' + err)
  })

  // Subscriber can specify how many existing messages to get.
  var opts = natsStreaming.subscriptionOptions().setStartWithLastReceived();
  var subscription = natsStreaming.subscribe('foo', opts);
  subscription.on('message', function (msg) {
    console.log('Received a message [' + msg.getSequence() + '] ' + msg.getData());
  })

})

Error:

E:\Repositorys\hemera>node examples\full-example.js
Received a message [9] Hello node-nats-streaming!

events.js:160
      throw er; // Unhandled 'error' event
      ^
Error
    at Client.publish (E:\Repositorys\hemera\node_modules\nats\lib\nats.js:1019:13)
    at Message.ack (E:\Repositorys\hemera\node_modules\node-nats-streaming\lib\stan.js:748:24)
    at Message.maybeAutoAck (E:\Repositorys\hemera\node_modules\node-nats-streaming\lib\stan.js:735:10)
    at Object.callback (E:\Repositorys\hemera\node_modules\node-nats-streaming\lib\stan.js:634:11)
    at Client.processMsg (E:\Repositorys\hemera\node_modules\nats\lib\nats.js:954:11)
    at Client.processInbound (E:\Repositorys\hemera\node_modules\nats\lib\nats.js:882:14)
    at Socket.<anonymous> (E:\Repositorys\hemera\node_modules\nats\lib\nats.js:465:12)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
aricart commented 7 years ago

The issue is not related to having the nats library (with the exception that any code you add may contribute to exposing it).

What we know: the on message is getting processed before the subscription is ready. There's a 'ready' notification for the subscription, which you can use to trigger the subscription registration.

If you process.nextTick() the handling of the connect and ready, the issue will be hard to reproduce, but it will still happen.

We need to figure out why this is happening. As the response to the subscription should be getting back before a message is sent.

StarpTech commented 7 years ago

Hi @aricart so there exist no workaround right? Will you investigate in the near future? Thanks.

aricart commented 7 years ago

Yes there's a small workaround - let me re-write your example real quick so that it is clear to you.

aricart commented 7 years ago

@StarpTech you can try https://github.com/nats-io/node-nats-streaming/tree/defer-subs-until-protocol. This solves the issue as far as I can tell. I will pow-wow with Ivan to further dig into why I get a message before the protocol subscription response is sent. You can use the branch until we push it to unblock you.

StarpTech commented 7 years ago

@aricart thanks!

aricart commented 7 years ago

The PR will get you going until we determine how/if the server needs to be fixed.

aricart commented 7 years ago

@StarpTech - the issue with the server was fixed (you need to get the build referencing the fix from @kozlovic). So the existing library you got from npm should work. Let me know if you spot anything else!

StarpTech commented 7 years ago

I will try it asap thanks!