[Improve] Add error handling for the message listener (#319)

## Motivation
Currently, there is no error handling for the message listener. If there are any errors thrown from the user's listener, the program will crash.

## Modification
* Add error handling for the message listener. The client won't crash the program if there are any errors in the user function. Instead, it will log as the error.
* Add LogUtils to the internal native code.
* Add `GetTopic` and `GetSubscriptionName` for the internal native consumer.
5 files changed
tree: b81097f433ee8fef4d6fca8abb8343b28289036d
  1. .github/
  2. build-support/
  3. docs/
  4. examples/
  5. perf/
  6. pkg/
  7. src/
  8. tests/
  9. .asf.yaml
  10. .clang-format
  11. .eslintignore
  12. .eslintrc.json
  13. .gitignore
  14. binding.gyp
  15. dependencies.yaml
  16. GenCertFile.js
  17. Gruntfile.js
  18. index.d.ts
  19. index.js
  20. LICENSE
  21. license-header.txt
  22. NOTICE
  23. package-lock.json
  24. package.json
  25. pulsar-client-cpp.txt
  26. README.md
  27. tsconfig.json
  28. tslint.json
  29. tstest.ts
  30. typedoc.json
README.md

Pulsar Node.js client library

The Pulsar Node.js client can be used to create Pulsar producers and consumers in Node.js.

This library works only in Node.js 10.x or later because it uses the node-addon-api module to wrap the C++ library.

Getting Started

Note

These instructions are only available for versions after 1.8.0. For versions previous to 1.8.0, you need to install the C++ client first. Please switch to the corresponding version branch of this repo to read the specific instructions.

To run the examples, skip this section.

To use the Pulsar Node.js client in your project, run:

npm install pulsar-client

or

yarn add pulsar-client

Then you can run the following simple end-to-end example:

const Pulsar = require('pulsar-client');

(async () => {
  // Create a client
  const client = new Pulsar.Client({
    serviceUrl: 'pulsar://localhost:6650'
  });

  // Create a producer
  const producer = await client.createProducer({
    topic: 'persistent://public/default/my-topic',
  });

  // Create a consumer
  const consumer = await client.subscribe({
    topic: 'persistent://public/default/my-topic',
    subscription: 'sub1'
  });

  // Send a message
  producer.send({
    data: Buffer.from("hello")
  });

  // Receive the message
  const msg = await consumer.receive();
  console.log(msg.getData().toString());
  consumer.acknowledge(msg);

  await producer.close();
  await consumer.close();
  await client.close();
})();

You should find the output as:

hello

You can see more examples in the examples directory. However, since these examples might use an API that was not released yet, you need to build this module. See the next section.

How to build

Note

Build from source code requires the Node.js version greater than 16.18

First, clone the repository.

git clone https://github.com/apache/pulsar-client-node.git
cd pulsar-client-node

Since this client is a C++ addon that depends on the Pulsar C++ client, you need to install the C++ client first. You need to ensure there is a C++ compiler that supports C++11 installed in your system.

  • Install C++ client on Linux:
pkg/linux/download-cpp-client.sh
  • Install C++ client on Windows:
pkg\windows\download-cpp-client.bat
  • Install C++ client on macOS:
pkg/mac/build-cpp-deps-lib.sh
pkg/mac/build-cpp-lib.sh

After the C++ client is installed, run the following command to build this C++ addon.

npm install

To verify it has been installed successfully, you can run an example like:

Note

A running Pulsar server is required. The example uses pulsar://localhost:6650 to connect to the server.

node examples/producer

You should find the output as:

Sent message: my-message-0
Sent message: my-message-1
Sent message: my-message-2
Sent message: my-message-3
Sent message: my-message-4
Sent message: my-message-5
Sent message: my-message-6
Sent message: my-message-7
Sent message: my-message-8
Sent message: my-message-9

Documentation

You can generate the API docs by:

npm install
npx typedoc
# Documentation generated at ./apidocs