You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pulsar.apache.org by GitBox <gi...@apache.org> on 2020/03/18 10:54:49 UTC

[GitHub] [pulsar-client-node] yosiat edited a comment on issue #62: Listening for messages

yosiat edited a comment 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
   
   If my changes make sense to you, I am happy to open a PR and move the discussion over there.
   

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services