yosiat commented on issue #62: Listening for messages
URL: 
https://github.com/apache/pulsar-client-node/issues/62#issuecomment-600553167
 
 
   Hi!
   
   I tried the listener implementation with the following code, that listens 
forever to messages and once any key received we close the consumer and client 
(in real-life: instead of listening to any key, we will listen to shutdown 
signals) - 
   
   ```javascript
   const Pulsar = require('pulsar-client');
   
   const readline = require('readline');
   
   const rl = new readline.createInterface({
     input: process.stdin,
     output: process.stdout
   })
   
   function prompt() {
     return new Promise((resolve) => {
       rl.question(`press any key to stop consuming`, () => resolve());
     })
   }
   
   (async () => {
     // Create a client
     const client = new Pulsar.Client({
       serviceUrl: 'pulsar://localhost:6650'
       //operationTimeoutSeconds: 30,
     });
   
     // Create a consumer
     const consumer = await client.subscribe({
       topic: 'persistent://public/default/my-topic',
       subscription: 'sub1',
       subscriptionType: 'Shared',
       ackTimeoutMs: 10000,
       listener: function(msg, consumer) {
         console.log(msg.getData().toString());
         consumer.acknowledge(msg);
       }
     });
   
     await prompt();
     await consumer.close();
     await client.close();
   
   })();
   ```
   
   I have producer which emits at regular intervals (after some time, fails on 
this - https://github.com/apache/pulsar-client-node/issues/78 and I restart it).
   
   The consumer after some random time, just closes the connection. I added 
`std::cout` to the code and I see `Consumer` deconstructor is called for every 
message (which makes sense, since we create for every message a consumer) and 
the `Client` deconstructor is called after a while.
   
   I did some changes in order to solve this and it works now, except the 
`listener` don't get the `consumer` instance.
   
   Changes are here - 
https://github.com/apache/pulsar-client-node/compare/master...yosiat:consumer-listener-bug
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to