blob: c5b88aa813c0e83be45fcf04628bd862fb7a86ce [file] [log] [blame]
<!DOCTYPE html><html lang="en"><head><meta charSet="utf-8"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><title>The Pulsar Node.js client · Apache Pulsar</title><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta name="generator" content="Docusaurus"/><meta name="description" content="The Pulsar Node.js client can be used to create Pulsar [producers](#producers), [consumers](#consumers), and [readers](#readers) in Node.js."/><meta name="docsearch:version" content="2.9.2"/><meta name="docsearch:language" content="en"/><meta property="og:title" content="The Pulsar Node.js client · Apache Pulsar"/><meta property="og:type" content="website"/><meta property="og:url" content="https://pulsar.apache.org/"/><meta property="og:description" content="The Pulsar Node.js client can be used to create Pulsar [producers](#producers), [consumers](#consumers), and [readers](#readers) in Node.js."/><meta name="twitter:card" content="summary"/><meta name="twitter:image" content="https://pulsar.apache.org/img/pulsar.svg"/><link rel="shortcut icon" href="/img/pulsar.ico"/><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/atom-one-dark.min.css"/><link rel="alternate" type="application/atom+xml" href="https://pulsar.apache.org/blog/atom.xml" title="Apache Pulsar Blog ATOM Feed"/><link rel="alternate" type="application/rss+xml" href="https://pulsar.apache.org/blog/feed.xml" title="Apache Pulsar Blog RSS Feed"/><link rel="stylesheet" href="/css/code-blocks-buttons.css"/><script type="text/javascript" src="https://buttons.github.io/buttons.js"></script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js"></script><script type="text/javascript" src="/js/custom.js"></script><script src="/js/scrollSpy.js"></script><link rel="stylesheet" href="/css/main.css"/><script src="/js/codetabs.js"></script></head><body class="sideNavVisible separateOnPageNav"><div class="fixedHeaderContainer"><div class="headerWrapper wrapper"><header><a href="/en"><img class="logo" src="/img/pulsar.svg" alt="Apache Pulsar"/></a><a href="/en/versions"><h3>2.9.2</h3></a><div class="navigationWrapper navigationSlider"><nav class="slidingNav"><ul class="nav-site nav-site-internal"><li class="siteNavGroupActive"><a href="/docs/en/2.9.2/getting-started-standalone" target="_self">Docs</a></li><li class=""><a href="/en/download" target="_self">Download</a></li><li class="siteNavGroupActive"><a href="/docs/en/2.9.2/client-libraries" target="_self">Clients</a></li><li class=""><a href="#restapis" target="_self">REST APIs</a></li><li class=""><a href="#cli" target="_self">Cli</a></li><li class=""><a href="/blog/" target="_self">Blog</a></li><li class=""><a href="#community" target="_self">Community</a></li><li class=""><a href="#apache" target="_self">Apache</a></li><li class=""><a href="https://pulsar-next.staged.apache.org/" target="_self">New Website (Beta)</a></li><span><li><a id="languages-menu" href="#"><img class="languages-icon" src="/img/language.svg" alt="Languages icon"/>English</a><div id="languages-dropdown" class="hide"><ul id="languages-dropdown-items"><li><a href="/docs/ja/2.9.2/client-libraries-node">日本語</a></li><li><a href="/docs/fr/2.9.2/client-libraries-node">Français</a></li><li><a href="/docs/ko/2.9.2/client-libraries-node">한국어</a></li><li><a href="/docs/zh-CN/2.9.2/client-libraries-node">中文</a></li><li><a href="/docs/zh-TW/2.9.2/client-libraries-node">繁體中文</a></li><li><a href="https://crowdin.com/project/apache-pulsar" target="_blank" rel="noreferrer noopener">Help Translate</a></li></ul></div></li><script>
const languagesMenuItem = document.getElementById("languages-menu");
const languagesDropDown = document.getElementById("languages-dropdown");
languagesMenuItem.addEventListener("click", function(event) {
event.preventDefault();
if (languagesDropDown.className == "hide") {
languagesDropDown.className = "visible";
} else {
languagesDropDown.className = "hide";
}
});
</script></span></ul></nav></div></header></div></div><div class="navPusher"><div class="docMainWrapper wrapper"><div class="docsNavContainer" id="docsNav"><nav class="toc"><div class="toggleNav"><section class="navWrapper wrapper"><div class="navBreadcrumb wrapper"><div class="navToggle" id="navToggler"><div class="hamburger-menu"><div class="line1"></div><div class="line2"></div><div class="line3"></div></div></div><h2><i></i><span>Client Libraries</span></h2><div class="tocToggler" id="tocToggler"><i class="icon-toc"></i></div></div><div class="navGroups"><div class="navGroup"><h3 class="navGroupCategoryTitle">Get Started</h3><ul class=""><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/getting-started-standalone">Run Pulsar locally</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/getting-started-docker">Run Pulsar in Docker</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/getting-started-helm">Run Pulsar in Kubernetes</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Concepts and Architecture</h3><ul class=""><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/concepts-overview">Overview</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/concepts-messaging">Messaging</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/concepts-architecture-overview">Architecture</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/concepts-clients">Clients</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/concepts-replication">Geo Replication</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/concepts-multi-tenancy">Multi Tenancy</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/concepts-authentication">Authentication and Authorization</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/concepts-topic-compaction">Topic Compaction</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/concepts-proxy-sni-routing">Proxy support with SNI routing</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/concepts-multiple-advertised-listeners">Multiple advertised listeners</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Pulsar Schema</h3><ul class=""><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/schema-get-started">Get started</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/schema-understand">Understand schema</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/schema-evolution-compatibility">Schema evolution and compatibility</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/schema-manage">Manage schema</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Pulsar Functions</h3><ul class=""><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/functions-overview">Overview</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/functions-runtime">Setup: Configure Functions runtime</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/functions-worker">Setup: Pulsar Functions Worker</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/functions-develop">How-to: Develop</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/functions-package">How-to: Package</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/functions-debug">How-to: Debug</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/functions-deploy">How-to: Deploy</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/functions-cli">Reference: CLI</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/window-functions-context">Window Functions: Context</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Pulsar IO</h3><ul class=""><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/io-overview">Overview</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/io-quickstart">Get started</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/io-use">Use</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/io-debug">Debug</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/io-connectors">Built-in connector</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/io-cdc">CDC connector</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/io-develop">Develop</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/io-cli">CLI</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Pulsar SQL</h3><ul class=""><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/sql-overview">Overview</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/sql-getting-started">Query data</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/sql-deployment-configurations">Configuration and deployment</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/sql-rest-api">REST APIs</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Tiered Storage</h3><ul class=""><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/tiered-storage-overview">Overview</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/tiered-storage-aws">AWS S3 offloader</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/tiered-storage-gcs">GCS offloader</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/tiered-storage-filesystem">Filesystem offloader</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/tiered-storage-azure">Azure BlobStore offloader</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/tiered-storage-aliyun">Aliyun OSS offloader</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Transactions</h3><ul class=""><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/txn-why">Why transactions?</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/txn-what">What are transactions?</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/txn-how">How transactions work?</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/txn-use">How to use transactions?</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/txn-monitor">How to monitor transactions?</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Kubernetes (Helm)</h3><ul class=""><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/helm-overview">Overview</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/helm-prepare">Prepare</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/helm-install">Install</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/helm-deploy">Deployment</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/helm-upgrade">Upgrade</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/helm-tools">Required Tools</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Deployment</h3><ul class=""><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/deploy-aws">Amazon Web Services</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/deploy-kubernetes">Kubernetes</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/deploy-bare-metal">Bare metal</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/deploy-bare-metal-multi-cluster">Bare metal multi-cluster</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/deploy-docker">Docker</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/deploy-monitoring">Monitor</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Administration</h3><ul class=""><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/administration-zk-bk">ZooKeeper and BookKeeper</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/administration-geo">Geo-replication</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/administration-pulsar-manager">Pulsar Manager</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/administration-stats">Pulsar statistics</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/administration-load-balance">Load balance</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/administration-proxy">Pulsar proxy</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/administration-upgrade">Upgrade</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/administration-isolation">Pulsar isolation</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Security</h3><ul class=""><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/security-overview">Overview</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/security-tls-transport">Transport Encryption using TLS</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/security-tls-authentication">Authentication using TLS</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/security-tls-keystore">Using TLS with KeyStore configure</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/security-jwt">Authentication using JWT</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/security-athenz">Authentication using Athenz</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/security-kerberos">Authentication using Kerberos</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/security-oauth2">Authentication using OAuth 2.0 access tokens</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/security-authorization">Authorization and ACLs</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/security-encryption">End-to-End Encryption</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/security-extending">Extending</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/security-bouncy-castle">Bouncy Castle Providers</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Performance</h3><ul class=""><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/performance-pulsar-perf">Pulsar Perf</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Client Libraries</h3><ul class=""><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/client-libraries">Overview</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/client-libraries-java">Java</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/client-libraries-go">Go</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/client-libraries-python">Python</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/client-libraries-cpp">C++</a></li><li class="navListItem navListItemActive"><a class="navItem" href="/docs/en/2.9.2/client-libraries-node">Node.js</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/client-libraries-websocket">WebSocket</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/client-libraries-dotnet">C#</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Admin API</h3><ul class=""><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/admin-api-overview">Overview</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/admin-api-clusters">Clusters</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/admin-api-tenants">Tenants</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/admin-api-brokers">Brokers</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/admin-api-namespaces">Namespaces</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/admin-api-permissions">Permissions</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/admin-api-topics">Topics</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/admin-api-functions">Functions</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/admin-api-packages">Packages</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Adaptors</h3><ul class=""><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/adaptors-kafka">Kafka client wrapper</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/adaptors-spark">Apache Spark</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/adaptors-storm">Apache Storm</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Cookbooks</h3><ul class=""><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/cookbooks-compaction">Topic compaction</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/cookbooks-deduplication">Message deduplication</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/cookbooks-non-persistent">Non-persistent messaging</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/cookbooks-retention-expiry">Message retention and expiry</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/cookbooks-encryption">Encryption</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/cookbooks-message-queue">Message queue</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/cookbooks-bookkeepermetadata">BookKeeper Ledger Metadata</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Development</h3><ul class=""><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/develop-tools">Simulation tools</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/developing-binary-protocol">Binary protocol</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/develop-schema">Custom schema storage</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/develop-load-manager">Modular load manager</a></li></ul></div><div class="navGroup"><h3 class="navGroupCategoryTitle">Reference</h3><ul class=""><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/reference-terminology">Terminology</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/reference-cli-tools">Pulsar CLI tools</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/reference-configuration">Pulsar configuration</a></li><li class="navListItem"><a class="navItem" href="/docs/en/2.9.2/reference-metrics">Pulsar Metrics</a></li></ul></div></div></section></div><script>
var coll = document.getElementsByClassName('collapsible');
var checkActiveCategory = true;
for (var i = 0; i < coll.length; i++) {
var links = coll[i].nextElementSibling.getElementsByTagName('*');
if (checkActiveCategory){
for (var j = 0; j < links.length; j++) {
if (links[j].classList.contains('navListItemActive')){
coll[i].nextElementSibling.classList.toggle('hide');
coll[i].childNodes[1].classList.toggle('rotate');
checkActiveCategory = false;
break;
}
}
}
coll[i].addEventListener('click', function() {
var arrow = this.childNodes[1];
arrow.classList.toggle('rotate');
var content = this.nextElementSibling;
content.classList.toggle('hide');
});
}
document.addEventListener('DOMContentLoaded', function() {
createToggler('#navToggler', '#docsNav', 'docsSliderActive');
createToggler('#tocToggler', 'body', 'tocActive');
var headings = document.querySelector('.toc-headings');
headings && headings.addEventListener('click', function(event) {
var el = event.target;
while(el !== headings){
if (el.tagName === 'A') {
document.body.classList.remove('tocActive');
break;
} else{
el = el.parentNode;
}
}
}, false);
function createToggler(togglerSelector, targetSelector, className) {
var toggler = document.querySelector(togglerSelector);
var target = document.querySelector(targetSelector);
if (!toggler) {
return;
}
toggler.onclick = function(event) {
event.preventDefault();
target.classList.toggle(className);
};
}
});
</script></nav></div><div class="container mainContainer docsContainer"><div class="wrapper"><div class="post"><header class="postHeader"><a class="edit-page-link button" href="https://github.com/apache/pulsar/edit/master/site2/docs/client-libraries-node.md" target="_blank" rel="noreferrer noopener">Edit</a><h1 id="__docusaurus" class="postHeaderTitle">The Pulsar Node.js client</h1></header><article><div><span><p>The Pulsar Node.js client can be used to create Pulsar <a href="#producers">producers</a>, <a href="#consumers">consumers</a>, and <a href="#readers">readers</a> in Node.js.</p>
<p>All the methods in <a href="#producers">producers</a>, <a href="#consumers">consumers</a>, and <a href="#readers">readers</a> of a Node.js client are thread-safe.</p>
<p>For 1.3.0 or later versions, <a href="https://github.com/apache/pulsar-client-node/blob/master/index.d.ts">type definitions</a> used in TypeScript are available.</p>
<h2><a class="anchor" aria-hidden="true" id="installation"></a><a href="#installation" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Installation</h2>
<p>You can install the <a href="https://www.npmjs.com/package/pulsar-client"><code>pulsar-client</code></a> library via <a href="https://www.npmjs.com/">npm</a>.</p>
<h3><a class="anchor" aria-hidden="true" id="requirements"></a><a href="#requirements" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Requirements</h3>
<p>Pulsar Node.js client library is based on the C++ client library.
Follow <a href="/docs/en/2.9.2/client-libraries-cpp#compilation">these instructions</a> and install the Pulsar C++ client library.</p>
<h3><a class="anchor" aria-hidden="true" id="compatibility"></a><a href="#compatibility" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Compatibility</h3>
<p>Compatibility between each version of the Node.js client and the C++ client is as follows:</p>
<table>
<thead>
<tr><th style="text-align:left">Node.js client</th><th style="text-align:left">C++ client</th></tr>
</thead>
<tbody>
<tr><td style="text-align:left">1.0.0</td><td style="text-align:left">2.3.0 or later</td></tr>
<tr><td style="text-align:left">1.1.0</td><td style="text-align:left">2.4.0 or later</td></tr>
<tr><td style="text-align:left">1.2.0</td><td style="text-align:left">2.5.0 or later</td></tr>
</tbody>
</table>
<p>If an incompatible version of the C++ client is installed, you may fail to build or run this library.</p>
<h3><a class="anchor" aria-hidden="true" id="installation-using-npm"></a><a href="#installation-using-npm" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Installation using npm</h3>
<p>Install the <code>pulsar-client</code> library via <a href="https://www.npmjs.com/">npm</a>:</p>
<pre><code class="hljs css language-shell"><span class="hljs-meta">$</span><span class="bash"> npm install pulsar-client</span>
</code></pre>
<blockquote>
<h4><a class="anchor" aria-hidden="true" id="note"></a><a href="#note" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Note</h4>
<p>Also, this library works only in Node.js 10.x or later because it uses the <a href="https://github.com/nodejs/node-addon-api"><code>node-addon-api</code></a> module to wrap the C++ library.</p>
</blockquote>
<h2><a class="anchor" aria-hidden="true" id="connection-urls"></a><a href="#connection-urls" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Connection URLs</h2>
<p>To connect to Pulsar using client libraries, you need to specify a <a href="/docs/en/2.9.2/developing-binary-protocol">Pulsar protocol</a> URL.</p>
<p>Pulsar protocol URLs are assigned to specific clusters, use the <code>pulsar</code> scheme and have a default port of 6650. Here is an example for <code>localhost</code>:</p>
<pre><code class="hljs css language-http">pulsar://localhost:6650
</code></pre>
<p>A URL for a production Pulsar cluster may look something like this:</p>
<pre><code class="hljs css language-http">pulsar://pulsar.us-west.example.com:6650
</code></pre>
<p>If you are using <a href="/docs/en/2.9.2/security-tls-transport">TLS encryption</a> or <a href="/docs/en/2.9.2/security-tls-authentication">TLS Authentication</a>, the URL looks like this:</p>
<pre><code class="hljs css language-http">pulsar+ssl://pulsar.us-west.example.com:6651
</code></pre>
<h2><a class="anchor" aria-hidden="true" id="create-a-client"></a><a href="#create-a-client" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Create a client</h2>
<p>In order to interact with Pulsar, you first need a client object. You can create a client instance using a <code>new</code> operator and the <code>Client</code> method, passing in a client options object (more on configuration <a href="#client-configuration">below</a>).</p>
<p>Here is an example:</p>
<pre><code class="hljs css language-JavaScript"><span class="hljs-keyword">const</span> Pulsar = <span class="hljs-built_in">require</span>(<span class="hljs-string">'pulsar-client'</span>);
<span class="hljs-function">(<span class="hljs-params"><span class="hljs-keyword">async</span> (</span>) =&gt;</span> {
<span class="hljs-keyword">const</span> client = <span class="hljs-keyword">new</span> Pulsar.Client({
<span class="hljs-attr">serviceUrl</span>: <span class="hljs-string">'pulsar://localhost:6650'</span>,
});
<span class="hljs-keyword">await</span> client.close();
})();
</code></pre>
<h3><a class="anchor" aria-hidden="true" id="client-configuration"></a><a href="#client-configuration" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Client configuration</h3>
<p>The following configurable parameters are available for Pulsar clients:</p>
<table>
<thead>
<tr><th style="text-align:left">Parameter</th><th style="text-align:left">Description</th><th style="text-align:left">Default</th></tr>
</thead>
<tbody>
<tr><td style="text-align:left"><code>serviceUrl</code></td><td style="text-align:left">The connection URL for the Pulsar cluster. See <a href="#connection-urls">above</a> for more info.</td><td style="text-align:left"></td></tr>
<tr><td style="text-align:left"><code>authentication</code></td><td style="text-align:left">Configure the authentication provider. (default: no authentication). See <a href="/docs/en/2.9.2/security-tls-authentication">TLS Authentication</a> for more info.</td><td style="text-align:left"></td></tr>
<tr><td style="text-align:left"><code>operationTimeoutSeconds</code></td><td style="text-align:left">The timeout for Node.js client operations (creating producers, subscribing to and unsubscribing from <a href="/docs/en/2.9.2/reference-terminology#topic">topics</a>). Retries occur until this threshold is reached, at which point the operation fails.</td><td style="text-align:left">30</td></tr>
<tr><td style="text-align:left"><code>ioThreads</code></td><td style="text-align:left">The number of threads to use for handling connections to Pulsar <a href="/docs/en/2.9.2/reference-terminology#broker">brokers</a>.</td><td style="text-align:left">1</td></tr>
<tr><td style="text-align:left"><code>messageListenerThreads</code></td><td style="text-align:left">The number of threads used by message listeners (<a href="#consumers">consumers</a> and <a href="#readers">readers</a>).</td><td style="text-align:left">1</td></tr>
<tr><td style="text-align:left"><code>concurrentLookupRequest</code></td><td style="text-align:left">The number of concurrent lookup requests that can be sent on each broker connection. Setting a maximum helps to keep from overloading brokers. You should set values over the default of 50000 only if the client needs to produce and/or subscribe to thousands of Pulsar topics.</td><td style="text-align:left">50000</td></tr>
<tr><td style="text-align:left"><code>tlsTrustCertsFilePath</code></td><td style="text-align:left">The file path for the trusted TLS certificate.</td><td style="text-align:left"></td></tr>
<tr><td style="text-align:left"><code>tlsValidateHostname</code></td><td style="text-align:left">The boolean value of setup whether to enable TLS hostname verification.</td><td style="text-align:left"><code>false</code></td></tr>
<tr><td style="text-align:left"><code>tlsAllowInsecureConnection</code></td><td style="text-align:left">The boolean value of setup whether the Pulsar client accepts untrusted TLS certificate from broker.</td><td style="text-align:left"><code>false</code></td></tr>
<tr><td style="text-align:left"><code>statsIntervalInSeconds</code></td><td style="text-align:left">Interval between each stat info. Stats is activated with positive statsInterval. The value should be set to 1 second at least</td><td style="text-align:left">600</td></tr>
<tr><td style="text-align:left"><code>log</code></td><td style="text-align:left">A function that is used for logging.</td><td style="text-align:left"><code>console.log</code></td></tr>
</tbody>
</table>
<h2><a class="anchor" aria-hidden="true" id="producers"></a><a href="#producers" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Producers</h2>
<p>Pulsar producers publish messages to Pulsar topics. You can <a href="#producer-configuration">configure</a> Node.js producers using a producer configuration object.</p>
<p>Here is an example:</p>
<pre><code class="hljs css language-JavaScript"><span class="hljs-keyword">const</span> producer = <span class="hljs-keyword">await</span> client.createProducer({
<span class="hljs-attr">topic</span>: <span class="hljs-string">'my-topic'</span>,
});
<span class="hljs-keyword">await</span> producer.send({
<span class="hljs-attr">data</span>: Buffer.from(<span class="hljs-string">"Hello, Pulsar"</span>),
});
<span class="hljs-keyword">await</span> producer.close();
</code></pre>
<blockquote>
<h4><a class="anchor" aria-hidden="true" id="promise-operation"></a><a href="#promise-operation" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Promise operation</h4>
<p>When you create a new Pulsar producer, the operation returns <code>Promise</code> object and get producer instance or an error through executor function.<br>
In this example, using await operator instead of executor function.</p>
</blockquote>
<h3><a class="anchor" aria-hidden="true" id="producer-operations"></a><a href="#producer-operations" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Producer operations</h3>
<p>Pulsar Node.js producers have the following methods available:</p>
<table>
<thead>
<tr><th style="text-align:left">Method</th><th style="text-align:left">Description</th><th style="text-align:left">Return type</th></tr>
</thead>
<tbody>
<tr><td style="text-align:left"><code>send(Object)</code></td><td style="text-align:left">Publishes a <a href="#messages">message</a> to the producer's topic. When the message is successfully acknowledged by the Pulsar broker, or an error is thrown, the Promise object whose result is the message ID runs executor function.</td><td style="text-align:left"><code>Promise&lt;Object&gt;</code></td></tr>
<tr><td style="text-align:left"><code>flush()</code></td><td style="text-align:left">Sends message from send queue to Pulsar broker. When the message is successfully acknowledged by the Pulsar broker, or an error is thrown, the Promise object runs executor function.</td><td style="text-align:left"><code>Promise&lt;null&gt;</code></td></tr>
<tr><td style="text-align:left"><code>close()</code></td><td style="text-align:left">Closes the producer and releases all resources allocated to it. Once <code>close()</code> is called, no more messages are accepted from the publisher. This method returns a Promise object. It runs the executor function when all pending publish requests are persisted by Pulsar. If an error is thrown, no pending writes are retried.</td><td style="text-align:left"><code>Promise&lt;null&gt;</code></td></tr>
<tr><td style="text-align:left"><code>getProducerName()</code></td><td style="text-align:left">Getter method of the producer name.</td><td style="text-align:left"><code>string</code></td></tr>
<tr><td style="text-align:left"><code>getTopic()</code></td><td style="text-align:left">Getter method of the name of the topic.</td><td style="text-align:left"><code>string</code></td></tr>
</tbody>
</table>
<h3><a class="anchor" aria-hidden="true" id="producer-configuration"></a><a href="#producer-configuration" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Producer configuration</h3>
<table>
<thead>
<tr><th style="text-align:left">Parameter</th><th style="text-align:left">Description</th><th style="text-align:left">Default</th></tr>
</thead>
<tbody>
<tr><td style="text-align:left"><code>topic</code></td><td style="text-align:left">The Pulsar <a href="/docs/en/2.9.2/reference-terminology#topic">topic</a> to which the producer publishes messages.</td><td style="text-align:left"></td></tr>
<tr><td style="text-align:left"><code>producerName</code></td><td style="text-align:left">A name for the producer. If you do not explicitly assign a name, Pulsar automatically generates a globally unique name. If you choose to explicitly assign a name, it needs to be unique across <em>all</em> Pulsar clusters, otherwise the creation operation throws an error.</td><td style="text-align:left"></td></tr>
<tr><td style="text-align:left"><code>sendTimeoutMs</code></td><td style="text-align:left">When publishing a message to a topic, the producer waits for an acknowledgment from the responsible Pulsar <a href="/docs/en/2.9.2/reference-terminology#broker">broker</a>. If a message is not acknowledged within the threshold set by this parameter, an error is thrown. If you set <code>sendTimeoutMs</code> to -1, the timeout is set to infinity (and thus removed). Removing the send timeout is recommended when using Pulsar's <a href="/docs/en/2.9.2/cookbooks-deduplication">message de-duplication</a> feature.</td><td style="text-align:left">30000</td></tr>
<tr><td style="text-align:left"><code>initialSequenceId</code></td><td style="text-align:left">The initial sequence ID of the message. When producer send message, add sequence ID to message. The ID is increased each time to send.</td><td style="text-align:left"></td></tr>
<tr><td style="text-align:left"><code>maxPendingMessages</code></td><td style="text-align:left">The maximum size of the queue holding pending messages (i.e. messages waiting to receive an acknowledgment from the <a href="/docs/en/2.9.2/reference-terminology#broker">broker</a>). By default, when the queue is full all calls to the <code>send</code> method fails <em>unless</em> <code>blockIfQueueFull</code> is set to <code>true</code>.</td><td style="text-align:left">1000</td></tr>
<tr><td style="text-align:left"><code>maxPendingMessagesAcrossPartitions</code></td><td style="text-align:left">The maximum size of the sum of partition's pending queue.</td><td style="text-align:left">50000</td></tr>
<tr><td style="text-align:left"><code>blockIfQueueFull</code></td><td style="text-align:left">If set to <code>true</code>, the producer's <code>send</code> method waits when the outgoing message queue is full rather than failing and throwing an error (the size of that queue is dictated by the <code>maxPendingMessages</code> parameter); if set to <code>false</code> (the default), <code>send</code> operations fails and throw a error when the queue is full.</td><td style="text-align:left"><code>false</code></td></tr>
<tr><td style="text-align:left"><code>messageRoutingMode</code></td><td style="text-align:left">The message routing logic (for producers on <a href="/docs/en/2.9.2/concepts-messaging#partitioned-topics">partitioned topics</a>). This logic is applied only when no key is set on messages. The available options are: round robin (<code>RoundRobinDistribution</code>), or publishing all messages to a single partition (<code>UseSinglePartition</code>, the default).</td><td style="text-align:left"><code>UseSinglePartition</code></td></tr>
<tr><td style="text-align:left"><code>hashingScheme</code></td><td style="text-align:left">The hashing function that determines the partition on which a particular message is published (partitioned topics only). The available options are: <code>JavaStringHash</code> (the equivalent of <code>String.hashCode()</code> in Java), <code>Murmur3_32Hash</code> (applies the <a href="https://en.wikipedia.org/wiki/MurmurHash">Murmur3</a> hashing function), or <code>BoostHash</code> (applies the hashing function from C++'s <a href="https://www.boost.org/doc/libs/1_62_0/doc/html/hash.html">Boost</a> library).</td><td style="text-align:left"><code>BoostHash</code></td></tr>
<tr><td style="text-align:left"><code>compressionType</code></td><td style="text-align:left">The message data compression type used by the producer. The available options are <a href="https://github.com/lz4/lz4"><code>LZ4</code></a>, and <a href="https://zlib.net/"><code>Zlib</code></a>, <a href="https://github.com/facebook/zstd/">ZSTD</a>, <a href="https://github.com/google/snappy/">SNAPPY</a>.</td><td style="text-align:left">Compression None</td></tr>
<tr><td style="text-align:left"><code>batchingEnabled</code></td><td style="text-align:left">If set to <code>true</code>, the producer send message as batch.</td><td style="text-align:left"><code>true</code></td></tr>
<tr><td style="text-align:left"><code>batchingMaxPublishDelayMs</code></td><td style="text-align:left">The maximum time of delay sending message in batching.</td><td style="text-align:left">10</td></tr>
<tr><td style="text-align:left"><code>batchingMaxMessages</code></td><td style="text-align:left">The maximum size of sending message in each time of batching.</td><td style="text-align:left">1000</td></tr>
<tr><td style="text-align:left"><code>properties</code></td><td style="text-align:left">The metadata of producer.</td><td style="text-align:left"></td></tr>
</tbody>
</table>
<h3><a class="anchor" aria-hidden="true" id="producer-example"></a><a href="#producer-example" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Producer example</h3>
<p>This example creates a Node.js producer for the <code>my-topic</code> topic and sends 10 messages to that topic:</p>
<pre><code class="hljs css language-JavaScript"><span class="hljs-keyword">const</span> Pulsar = <span class="hljs-built_in">require</span>(<span class="hljs-string">'pulsar-client'</span>);
<span class="hljs-function">(<span class="hljs-params"><span class="hljs-keyword">async</span> (</span>) =&gt;</span> {
<span class="hljs-comment">// Create a client</span>
<span class="hljs-keyword">const</span> client = <span class="hljs-keyword">new</span> Pulsar.Client({
<span class="hljs-attr">serviceUrl</span>: <span class="hljs-string">'pulsar://localhost:6650'</span>,
});
<span class="hljs-comment">// Create a producer</span>
<span class="hljs-keyword">const</span> producer = <span class="hljs-keyword">await</span> client.createProducer({
<span class="hljs-attr">topic</span>: <span class="hljs-string">'my-topic'</span>,
});
<span class="hljs-comment">// Send messages</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i += <span class="hljs-number">1</span>) {
<span class="hljs-keyword">const</span> msg = <span class="hljs-string">`my-message-<span class="hljs-subst">${i}</span>`</span>;
producer.send({
<span class="hljs-attr">data</span>: Buffer.from(msg),
});
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Sent message: <span class="hljs-subst">${msg}</span>`</span>);
}
<span class="hljs-keyword">await</span> producer.flush();
<span class="hljs-keyword">await</span> producer.close();
<span class="hljs-keyword">await</span> client.close();
})();
</code></pre>
<h2><a class="anchor" aria-hidden="true" id="consumers"></a><a href="#consumers" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Consumers</h2>
<p>Pulsar consumers subscribe to one or more Pulsar topics and listen for incoming messages produced on that topic/those topics. You can <a href="#consumer-configuration">configure</a> Node.js consumers using a consumer configuration object.</p>
<p>Here is an example:</p>
<pre><code class="hljs css language-JavaScript"><span class="hljs-keyword">const</span> consumer = <span class="hljs-keyword">await</span> client.subscribe({
<span class="hljs-attr">topic</span>: <span class="hljs-string">'my-topic'</span>,
<span class="hljs-attr">subscription</span>: <span class="hljs-string">'my-subscription'</span>,
});
<span class="hljs-keyword">const</span> msg = <span class="hljs-keyword">await</span> consumer.receive();
<span class="hljs-built_in">console</span>.log(msg.getData().toString());
consumer.acknowledge(msg);
<span class="hljs-keyword">await</span> consumer.close();
</code></pre>
<blockquote>
<h4><a class="anchor" aria-hidden="true" id="promise-operation-1"></a><a href="#promise-operation-1" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Promise operation</h4>
<p>When you create a new Pulsar consumer, the operation returns <code>Promise</code> object and get consumer instance or an error through executor function.<br>
In this example, using await operator instead of executor function.</p>
</blockquote>
<h3><a class="anchor" aria-hidden="true" id="consumer-operations"></a><a href="#consumer-operations" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Consumer operations</h3>
<p>Pulsar Node.js consumers have the following methods available:</p>
<table>
<thead>
<tr><th style="text-align:left">Method</th><th style="text-align:left">Description</th><th style="text-align:left">Return type</th></tr>
</thead>
<tbody>
<tr><td style="text-align:left"><code>receive()</code></td><td style="text-align:left">Receives a single message from the topic. When the message is available, the Promise object run executor function and get message object.</td><td style="text-align:left"><code>Promise&lt;Object&gt;</code></td></tr>
<tr><td style="text-align:left"><code>receive(Number)</code></td><td style="text-align:left">Receives a single message from the topic with specific timeout in milliseconds.</td><td style="text-align:left"><code>Promise&lt;Object&gt;</code></td></tr>
<tr><td style="text-align:left"><code>acknowledge(Object)</code></td><td style="text-align:left"><a href="/docs/en/2.9.2/reference-terminology#acknowledgment-ack">Acknowledges</a> a message to the Pulsar <a href="/docs/en/2.9.2/reference-terminology#broker">broker</a> by message object.</td><td style="text-align:left"><code>void</code></td></tr>
<tr><td style="text-align:left"><code>acknowledgeId(Object)</code></td><td style="text-align:left"><a href="/docs/en/2.9.2/reference-terminology#acknowledgment-ack">Acknowledges</a> a message to the Pulsar <a href="/docs/en/2.9.2/reference-terminology#broker">broker</a> by message ID object.</td><td style="text-align:left"><code>void</code></td></tr>
<tr><td style="text-align:left"><code>acknowledgeCumulative(Object)</code></td><td style="text-align:left"><a href="/docs/en/2.9.2/reference-terminology#acknowledgment-ack">Acknowledges</a> <em>all</em> the messages in the stream, up to and including the specified message. The <code>acknowledgeCumulative</code> method returns void, and send the ack to the broker asynchronously. After that, the messages are <em>not</em> redelivered to the consumer. Cumulative acking can not be used with a <a href="/docs/en/2.9.2/concepts-messaging#shared">shared</a> subscription type.</td><td style="text-align:left"><code>void</code></td></tr>
<tr><td style="text-align:left"><code>acknowledgeCumulativeId(Object)</code></td><td style="text-align:left"><a href="/docs/en/2.9.2/reference-terminology#acknowledgment-ack">Acknowledges</a> <em>all</em> the messages in the stream, up to and including the specified message ID.</td><td style="text-align:left"><code>void</code></td></tr>
<tr><td style="text-align:left"><code>negativeAcknowledge(Message)</code></td><td style="text-align:left"><a href="/docs/en/2.9.2/reference-terminology#negative-acknowledgment-nack">Negatively acknowledges</a> a message to the Pulsar broker by message object.</td><td style="text-align:left"><code>void</code></td></tr>
<tr><td style="text-align:left"><code>negativeAcknowledgeId(MessageId)</code></td><td style="text-align:left"><a href="/docs/en/2.9.2/reference-terminology#negative-acknowledgment-nack">Negatively acknowledges</a> a message to the Pulsar broker by message ID object.</td><td style="text-align:left"><code>void</code></td></tr>
<tr><td style="text-align:left"><code>close()</code></td><td style="text-align:left">Closes the consumer, disabling its ability to receive messages from the broker.</td><td style="text-align:left"><code>Promise&lt;null&gt;</code></td></tr>
<tr><td style="text-align:left"><code>unsubscribe()</code></td><td style="text-align:left">Unsubscribes the subscription.</td><td style="text-align:left"><code>Promise&lt;null&gt;</code></td></tr>
</tbody>
</table>
<h3><a class="anchor" aria-hidden="true" id="consumer-configuration"></a><a href="#consumer-configuration" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Consumer configuration</h3>
<table>
<thead>
<tr><th style="text-align:left">Parameter</th><th style="text-align:left">Description</th><th style="text-align:left">Default</th></tr>
</thead>
<tbody>
<tr><td style="text-align:left"><code>topic</code></td><td style="text-align:left">The Pulsar topic on which the consumer establishes a subscription and listen for messages.</td><td style="text-align:left"></td></tr>
<tr><td style="text-align:left"><code>topics</code></td><td style="text-align:left">The array of topics.</td><td style="text-align:left"></td></tr>
<tr><td style="text-align:left"><code>topicsPattern</code></td><td style="text-align:left">The regular expression for topics.</td><td style="text-align:left"></td></tr>
<tr><td style="text-align:left"><code>subscription</code></td><td style="text-align:left">The subscription name for this consumer.</td><td style="text-align:left"></td></tr>
<tr><td style="text-align:left"><code>subscriptionType</code></td><td style="text-align:left">Available options are <code>Exclusive</code>, <code>Shared</code>, <code>Key_Shared</code>, and <code>Failover</code>.</td><td style="text-align:left"><code>Exclusive</code></td></tr>
<tr><td style="text-align:left"><code>subscriptionInitialPosition</code></td><td style="text-align:left">Initial position at which to set cursor when subscribing to a topic at first time.</td><td style="text-align:left"><code>SubscriptionInitialPosition.Latest</code></td></tr>
<tr><td style="text-align:left"><code>ackTimeoutMs</code></td><td style="text-align:left">Acknowledge timeout in milliseconds.</td><td style="text-align:left">0</td></tr>
<tr><td style="text-align:left"><code>nAckRedeliverTimeoutMs</code></td><td style="text-align:left">Delay to wait before redelivering messages that failed to be processed.</td><td style="text-align:left">60000</td></tr>
<tr><td style="text-align:left"><code>receiverQueueSize</code></td><td style="text-align:left">Sets the size of the consumer's receiver queue, i.e. the number of messages that can be accumulated by the consumer before the application calls <code>receive</code>. A value higher than the default of 1000 could increase consumer throughput, though at the expense of more memory utilization.</td><td style="text-align:left">1000</td></tr>
<tr><td style="text-align:left"><code>receiverQueueSizeAcrossPartitions</code></td><td style="text-align:left">Set the max total receiver queue size across partitions. This setting is used to reduce the receiver queue size for individual partitions if the total exceeds this value.</td><td style="text-align:left">50000</td></tr>
<tr><td style="text-align:left"><code>consumerName</code></td><td style="text-align:left">The name of consumer. Currently(v2.4.1), <a href="/docs/en/2.9.2/concepts-messaging#failover">failover</a> mode use consumer name in ordering.</td><td style="text-align:left"></td></tr>
<tr><td style="text-align:left"><code>properties</code></td><td style="text-align:left">The metadata of consumer.</td><td style="text-align:left"></td></tr>
<tr><td style="text-align:left"><code>listener</code></td><td style="text-align:left">A listener that is called for a message received.</td><td style="text-align:left"></td></tr>
<tr><td style="text-align:left"><code>readCompacted</code></td><td style="text-align:left">If enabling <code>readCompacted</code>, a consumer reads messages from a compacted topic rather than reading a full message backlog of a topic.<br/><br/>A consumer only sees the latest value for each key in the compacted topic, up until reaching the point in the topic message when compacting backlog. Beyond that point, send messages as normal.<br/><br/> <code>readCompacted</code> can only be enabled on subscriptions to persistent topics, which have a single active consumer (like failure or exclusive subscriptions).<br/><br/>Attempting to enable it on subscriptions to non-persistent topics or on shared subscriptions leads to a subscription call throwing a <code>PulsarClientException</code>.</td><td style="text-align:left">false</td></tr>
</tbody>
</table>
<h3><a class="anchor" aria-hidden="true" id="consumer-example"></a><a href="#consumer-example" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Consumer example</h3>
<p>This example creates a Node.js consumer with the <code>my-subscription</code> subscription on the <code>my-topic</code> topic, receives messages, prints the content that arrive, and acknowledges each message to the Pulsar broker for 10 times:</p>
<pre><code class="hljs css language-JavaScript"><span class="hljs-keyword">const</span> Pulsar = <span class="hljs-built_in">require</span>(<span class="hljs-string">'pulsar-client'</span>);
<span class="hljs-function">(<span class="hljs-params"><span class="hljs-keyword">async</span> (</span>) =&gt;</span> {
<span class="hljs-comment">// Create a client</span>
<span class="hljs-keyword">const</span> client = <span class="hljs-keyword">new</span> Pulsar.Client({
<span class="hljs-attr">serviceUrl</span>: <span class="hljs-string">'pulsar://localhost:6650'</span>,
});
<span class="hljs-comment">// Create a consumer</span>
<span class="hljs-keyword">const</span> consumer = <span class="hljs-keyword">await</span> client.subscribe({
<span class="hljs-attr">topic</span>: <span class="hljs-string">'my-topic'</span>,
<span class="hljs-attr">subscription</span>: <span class="hljs-string">'my-subscription'</span>,
<span class="hljs-attr">subscriptionType</span>: <span class="hljs-string">'Exclusive'</span>,
});
<span class="hljs-comment">// Receive messages</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i += <span class="hljs-number">1</span>) {
<span class="hljs-keyword">const</span> msg = <span class="hljs-keyword">await</span> consumer.receive();
<span class="hljs-built_in">console</span>.log(msg.getData().toString());
consumer.acknowledge(msg);
}
<span class="hljs-keyword">await</span> consumer.close();
<span class="hljs-keyword">await</span> client.close();
})();
</code></pre>
<p>Instead a consumer can be created with <code>listener</code> to process messages.</p>
<pre><code class="hljs css language-JavaScript"><span class="hljs-comment">// Create a consumer</span>
<span class="hljs-keyword">const</span> consumer = <span class="hljs-keyword">await</span> client.subscribe({
<span class="hljs-attr">topic</span>: <span class="hljs-string">'my-topic'</span>,
<span class="hljs-attr">subscription</span>: <span class="hljs-string">'my-subscription'</span>,
<span class="hljs-attr">subscriptionType</span>: <span class="hljs-string">'Exclusive'</span>,
<span class="hljs-attr">listener</span>: <span class="hljs-function">(<span class="hljs-params">msg, msgConsumer</span>) =&gt;</span> {
<span class="hljs-built_in">console</span>.log(msg.getData().toString());
msgConsumer.acknowledge(msg);
},
});
</code></pre>
<h2><a class="anchor" aria-hidden="true" id="readers"></a><a href="#readers" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Readers</h2>
<p>Pulsar readers process messages from Pulsar topics. Readers are different from consumers because with readers you need to explicitly specify which message in the stream you want to begin with (consumers, on the other hand, automatically begin with the most recently unacked message). You can <a href="#reader-configuration">configure</a> Node.js readers using a reader configuration object.</p>
<p>Here is an example:</p>
<pre><code class="hljs css language-JavaScript"><span class="hljs-keyword">const</span> reader = <span class="hljs-keyword">await</span> client.createReader({
<span class="hljs-attr">topic</span>: <span class="hljs-string">'my-topic'</span>,
<span class="hljs-attr">startMessageId</span>: Pulsar.MessageId.earliest(),
});
<span class="hljs-keyword">const</span> msg = <span class="hljs-keyword">await</span> reader.readNext();
<span class="hljs-built_in">console</span>.log(msg.getData().toString());
<span class="hljs-keyword">await</span> reader.close();
</code></pre>
<h3><a class="anchor" aria-hidden="true" id="reader-operations"></a><a href="#reader-operations" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Reader operations</h3>
<p>Pulsar Node.js readers have the following methods available:</p>
<table>
<thead>
<tr><th style="text-align:left">Method</th><th style="text-align:left">Description</th><th style="text-align:left">Return type</th></tr>
</thead>
<tbody>
<tr><td style="text-align:left"><code>readNext()</code></td><td style="text-align:left">Receives the next message on the topic (analogous to the <code>receive</code> method for <a href="#consumer-operations">consumers</a>). When the message is available, the Promise object run executor function and get message object.</td><td style="text-align:left"><code>Promise&lt;Object&gt;</code></td></tr>
<tr><td style="text-align:left"><code>readNext(Number)</code></td><td style="text-align:left">Receives a single message from the topic with specific timeout in milliseconds.</td><td style="text-align:left"><code>Promise&lt;Object&gt;</code></td></tr>
<tr><td style="text-align:left"><code>hasNext()</code></td><td style="text-align:left">Return whether the broker has next message in target topic.</td><td style="text-align:left"><code>Boolean</code></td></tr>
<tr><td style="text-align:left"><code>close()</code></td><td style="text-align:left">Closes the reader, disabling its ability to receive messages from the broker.</td><td style="text-align:left"><code>Promise&lt;null&gt;</code></td></tr>
</tbody>
</table>
<h3><a class="anchor" aria-hidden="true" id="reader-configuration"></a><a href="#reader-configuration" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Reader configuration</h3>
<table>
<thead>
<tr><th style="text-align:left">Parameter</th><th style="text-align:left">Description</th><th style="text-align:left">Default</th></tr>
</thead>
<tbody>
<tr><td style="text-align:left"><code>topic</code></td><td style="text-align:left">The Pulsar <a href="/docs/en/2.9.2/reference-terminology#topic">topic</a> on which the reader establishes a subscription and listen for messages.</td><td style="text-align:left"></td></tr>
<tr><td style="text-align:left"><code>startMessageId</code></td><td style="text-align:left">The initial reader position, i.e. the message at which the reader begins processing messages. The options are <code>Pulsar.MessageId.earliest</code> (the earliest available message on the topic), <code>Pulsar.MessageId.latest</code> (the latest available message on the topic), or a message ID object for a position that is not earliest or latest.</td><td style="text-align:left"></td></tr>
<tr><td style="text-align:left"><code>receiverQueueSize</code></td><td style="text-align:left">Sets the size of the reader's receiver queue, i.e. the number of messages that can be accumulated by the reader before the application calls <code>readNext</code>. A value higher than the default of 1000 could increase reader throughput, though at the expense of more memory utilization.</td><td style="text-align:left">1000</td></tr>
<tr><td style="text-align:left"><code>readerName</code></td><td style="text-align:left">The name of the reader.</td><td style="text-align:left"></td></tr>
<tr><td style="text-align:left"><code>subscriptionRolePrefix</code></td><td style="text-align:left">The subscription role prefix.</td><td style="text-align:left"></td></tr>
<tr><td style="text-align:left"><code>readCompacted</code></td><td style="text-align:left">If enabling <code>readCompacted</code>, a consumer reads messages from a compacted topic rather than reading a full message backlog of a topic.<br/><br/>A consumer only sees the latest value for each key in the compacted topic, up until reaching the point in the topic message when compacting backlog. Beyond that point, send messages as normal.<br/><br/> <code>readCompacted</code> can only be enabled on subscriptions to persistent topics, which have a single active consumer (like failure or exclusive subscriptions).<br/><br/>Attempting to enable it on subscriptions to non-persistent topics or on shared subscriptions leads to a subscription call throwing a <code>PulsarClientException</code>.</td><td style="text-align:left"><code>false</code></td></tr>
</tbody>
</table>
<h3><a class="anchor" aria-hidden="true" id="reader-example"></a><a href="#reader-example" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Reader example</h3>
<p>This example creates a Node.js reader with the <code>my-topic</code> topic, reads messages, and prints the content that arrive for 10 times:</p>
<pre><code class="hljs css language-JavaScript"><span class="hljs-keyword">const</span> Pulsar = <span class="hljs-built_in">require</span>(<span class="hljs-string">'pulsar-client'</span>);
<span class="hljs-function">(<span class="hljs-params"><span class="hljs-keyword">async</span> (</span>) =&gt;</span> {
<span class="hljs-comment">// Create a client</span>
<span class="hljs-keyword">const</span> client = <span class="hljs-keyword">new</span> Pulsar.Client({
<span class="hljs-attr">serviceUrl</span>: <span class="hljs-string">'pulsar://localhost:6650'</span>,
<span class="hljs-attr">operationTimeoutSeconds</span>: <span class="hljs-number">30</span>,
});
<span class="hljs-comment">// Create a reader</span>
<span class="hljs-keyword">const</span> reader = <span class="hljs-keyword">await</span> client.createReader({
<span class="hljs-attr">topic</span>: <span class="hljs-string">'my-topic'</span>,
<span class="hljs-attr">startMessageId</span>: Pulsar.MessageId.earliest(),
});
<span class="hljs-comment">// read messages</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i += <span class="hljs-number">1</span>) {
<span class="hljs-keyword">const</span> msg = <span class="hljs-keyword">await</span> reader.readNext();
<span class="hljs-built_in">console</span>.log(msg.getData().toString());
}
<span class="hljs-keyword">await</span> reader.close();
<span class="hljs-keyword">await</span> client.close();
})();
</code></pre>
<h2><a class="anchor" aria-hidden="true" id="messages"></a><a href="#messages" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Messages</h2>
<p>In Pulsar Node.js client, you have to construct producer message object for producer.</p>
<p>Here is an example message:</p>
<pre><code class="hljs css language-JavaScript"><span class="hljs-keyword">const</span> msg = {
<span class="hljs-attr">data</span>: Buffer.from(<span class="hljs-string">'Hello, Pulsar'</span>),
<span class="hljs-attr">partitionKey</span>: <span class="hljs-string">'key1'</span>,
<span class="hljs-attr">properties</span>: {
<span class="hljs-string">'foo'</span>: <span class="hljs-string">'bar'</span>,
},
<span class="hljs-attr">eventTimestamp</span>: <span class="hljs-built_in">Date</span>.now(),
<span class="hljs-attr">replicationClusters</span>: [
<span class="hljs-string">'cluster1'</span>,
<span class="hljs-string">'cluster2'</span>,
],
}
<span class="hljs-keyword">await</span> producer.send(msg);
</code></pre>
<p>The following keys are available for producer message objects:</p>
<table>
<thead>
<tr><th style="text-align:left">Parameter</th><th style="text-align:left">Description</th></tr>
</thead>
<tbody>
<tr><td style="text-align:left"><code>data</code></td><td style="text-align:left">The actual data payload of the message.</td></tr>
<tr><td style="text-align:left"><code>properties</code></td><td style="text-align:left">A Object for any application-specific metadata attached to the message.</td></tr>
<tr><td style="text-align:left"><code>eventTimestamp</code></td><td style="text-align:left">The timestamp associated with the message.</td></tr>
<tr><td style="text-align:left"><code>sequenceId</code></td><td style="text-align:left">The sequence ID of the message.</td></tr>
<tr><td style="text-align:left"><code>partitionKey</code></td><td style="text-align:left">The optional key associated with the message (particularly useful for things like topic compaction).</td></tr>
<tr><td style="text-align:left"><code>replicationClusters</code></td><td style="text-align:left">The clusters to which this message is replicated. Pulsar brokers handle message replication automatically; you should only change this setting if you want to override the broker default.</td></tr>
<tr><td style="text-align:left"><code>deliverAt</code></td><td style="text-align:left">The absolute timestamp at or after which the message is delivered.</td><td></td></tr>
<tr><td style="text-align:left"><code>deliverAfter</code></td><td style="text-align:left">The relative delay after which the message is delivered.</td><td></td></tr>
</tbody>
</table>
<h3><a class="anchor" aria-hidden="true" id="message-object-operations"></a><a href="#message-object-operations" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Message object operations</h3>
<p>In Pulsar Node.js client, you can receive (or read) message object as consumer (or reader).</p>
<p>The message object have the following methods available:</p>
<table>
<thead>
<tr><th style="text-align:left">Method</th><th style="text-align:left">Description</th><th style="text-align:left">Return type</th></tr>
</thead>
<tbody>
<tr><td style="text-align:left"><code>getTopicName()</code></td><td style="text-align:left">Getter method of topic name.</td><td style="text-align:left"><code>String</code></td></tr>
<tr><td style="text-align:left"><code>getProperties()</code></td><td style="text-align:left">Getter method of properties.</td><td style="text-align:left"><code>Array&lt;Object&gt;</code></td></tr>
<tr><td style="text-align:left"><code>getData()</code></td><td style="text-align:left">Getter method of message data.</td><td style="text-align:left"><code>Buffer</code></td></tr>
<tr><td style="text-align:left"><code>getMessageId()</code></td><td style="text-align:left">Getter method of <a href="#message-id-object-operations">message id object</a>.</td><td style="text-align:left"><code>Object</code></td></tr>
<tr><td style="text-align:left"><code>getPublishTimestamp()</code></td><td style="text-align:left">Getter method of publish timestamp.</td><td style="text-align:left"><code>Number</code></td></tr>
<tr><td style="text-align:left"><code>getEventTimestamp()</code></td><td style="text-align:left">Getter method of event timestamp.</td><td style="text-align:left"><code>Number</code></td></tr>
<tr><td style="text-align:left"><code>getRedeliveryCount()</code></td><td style="text-align:left">Getter method of redelivery count.</td><td style="text-align:left"><code>Number</code></td></tr>
<tr><td style="text-align:left"><code>getPartitionKey()</code></td><td style="text-align:left">Getter method of partition key.</td><td style="text-align:left"><code>String</code></td></tr>
</tbody>
</table>
<h3><a class="anchor" aria-hidden="true" id="message-id-object-operations"></a><a href="#message-id-object-operations" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Message ID object operations</h3>
<p>In Pulsar Node.js client, you can get message id object from message object.</p>
<p>The message id object have the following methods available:</p>
<table>
<thead>
<tr><th style="text-align:left">Method</th><th style="text-align:left">Description</th><th style="text-align:left">Return type</th></tr>
</thead>
<tbody>
<tr><td style="text-align:left"><code>serialize()</code></td><td style="text-align:left">Serialize the message id into a Buffer for storing.</td><td style="text-align:left"><code>Buffer</code></td></tr>
<tr><td style="text-align:left"><code>toString()</code></td><td style="text-align:left">Get message id as String.</td><td style="text-align:left"><code>String</code></td></tr>
</tbody>
</table>
<p>The client has static method of message id object. You can access it as <code>Pulsar.MessageId.someStaticMethod</code> too.</p>
<p>The following static methods are available for the message id object:</p>
<table>
<thead>
<tr><th style="text-align:left">Method</th><th style="text-align:left">Description</th><th style="text-align:left">Return type</th></tr>
</thead>
<tbody>
<tr><td style="text-align:left"><code>earliest()</code></td><td style="text-align:left">MessageId representing the earliest, or oldest available message stored in the topic.</td><td style="text-align:left"><code>Object</code></td></tr>
<tr><td style="text-align:left"><code>latest()</code></td><td style="text-align:left">MessageId representing the latest, or last published message in the topic.</td><td style="text-align:left"><code>Object</code></td></tr>
<tr><td style="text-align:left"><code>deserialize(Buffer)</code></td><td style="text-align:left">Deserialize a message id object from a Buffer.</td><td style="text-align:left"><code>Object</code></td></tr>
</tbody>
</table>
<h2><a class="anchor" aria-hidden="true" id="end-to-end-encryption"></a><a href="#end-to-end-encryption" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>End-to-end encryption</h2>
<p><a href="https://pulsar.apache.org/docs/en/next/cookbooks-encryption/#docsNav">End-to-end encryption</a> allows applications to encrypt messages at producers and decrypt at consumers.</p>
<h3><a class="anchor" aria-hidden="true" id="configuration"></a><a href="#configuration" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Configuration</h3>
<p>If you want to use the end-to-end encryption feature in the Node.js client, you need to configure <code>publicKeyPath</code> and <code>privateKeyPath</code> for both producer and consumer.</p>
<pre><code class="hljs"><span class="hljs-symbol">publicKeyPath:</span> <span class="hljs-string">"./public.pem"</span>
<span class="hljs-symbol">privateKeyPath:</span> <span class="hljs-string">"./private.pem"</span>
</code></pre>
<h3><a class="anchor" aria-hidden="true" id="tutorial"></a><a href="#tutorial" aria-hidden="true" class="hash-link"><svg class="hash-link-icon" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg></a>Tutorial</h3>
<p>This section provides step-by-step instructions on how to use the end-to-end encryption feature in the Node.js client.</p>
<p><strong>Prerequisite</strong></p>
<ul>
<li>Pulsar C++ client 2.7.1 or later</li>
</ul>
<p><strong>Step</strong></p>
<ol>
<li><p>Create both public and private key pairs.</p>
<p><strong>Input</strong></p>
<pre><code class="hljs css language-shell">openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -pubout -out public.pem
</code></pre></li>
<li><p>Create a producer to send encrypted messages.</p>
<p><strong>Input</strong></p>
<pre><code class="hljs css language-nodejs"><span class="hljs-keyword">const</span> Pulsar = <span class="hljs-built_in">require</span>(<span class="hljs-string">'pulsar-client'</span>);
<span class="hljs-function">(<span class="hljs-params"><span class="hljs-keyword">async</span> (<span class="hljs-params"></span>) =&gt; {
<span class="hljs-comment">// Create a client</span>
<span class="hljs-keyword">const</span> client = <span class="hljs-keyword">new</span> Pulsar.Client(<span class="hljs-params">{
serviceUrl: 'pulsar:<span class="hljs-comment">//localhost:6650',</span>
operationTimeoutSeconds: 30,
}</span>);
<span class="hljs-comment">// Create a producer</span>
<span class="hljs-keyword">const</span> producer = <span class="hljs-keyword">await</span> client.createProducer(<span class="hljs-params">{
topic: 'persistent:<span class="hljs-comment">//public/default/my-topic',</span>
sendTimeoutMs: 30000,
batchingEnabled: <span class="hljs-literal">true</span>,
publicKeyPath: "./<span class="hljs-keyword">public</span>.pem",
privateKeyPath: "./<span class="hljs-keyword">private</span>.pem",
encryptionKey: "encryption-key"
}</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-params">producer.ProducerConfig</span>)
<span class="hljs-comment">// Send messages</span>
<span class="hljs-keyword">for</span> (<span class="hljs-params"><span class="hljs-keyword">let</span> i = 0; i &lt; 10; i += 1</span>) {
<span class="hljs-keyword">const</span> msg = `my-message-${i}`;
producer.send(<span class="hljs-params">{
data: Buffer.<span class="hljs-keyword">from</span>(<span class="hljs-params">msg</span>),
}</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-params">`Sent message: ${msg}`</span>);
}
<span class="hljs-keyword">await</span> producer.flush(<span class="hljs-params"></span>);
<span class="hljs-keyword">await</span> producer.close(<span class="hljs-params"></span>);
<span class="hljs-keyword">await</span> client.close(<span class="hljs-params"></span>);
}</span>)<span class="hljs-params">()</span>;
</span></code></pre></li>
<li><p>Create a consumer to receive encrypted messages.</p>
<p><strong>Input</strong></p>
<pre><code class="hljs css language-nodejs"><span class="hljs-keyword">const</span> Pulsar = <span class="hljs-built_in">require</span>(<span class="hljs-string">'pulsar-client'</span>);
<span class="hljs-function">(<span class="hljs-params"><span class="hljs-keyword">async</span> (<span class="hljs-params"></span>) =&gt; {
<span class="hljs-comment">// Create a client</span>
<span class="hljs-keyword">const</span> client = <span class="hljs-keyword">new</span> Pulsar.Client(<span class="hljs-params">{
serviceUrl: 'pulsar:<span class="hljs-comment">//172.25.0.3:6650',</span>
operationTimeoutSeconds: 30
}</span>);
<span class="hljs-comment">// Create a consumer</span>
<span class="hljs-keyword">const</span> consumer = <span class="hljs-keyword">await</span> client.subscribe(<span class="hljs-params">{
topic: 'persistent:<span class="hljs-comment">//public/default/my-topic',</span>
subscription: 'sub1',
subscriptionType: 'Shared',
ackTimeoutMs: 10000,
publicKeyPath: "./<span class="hljs-keyword">public</span>.pem",
privateKeyPath: "./<span class="hljs-keyword">private</span>.pem"
}</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-params">consumer</span>)
<span class="hljs-comment">// Receive messages</span>
<span class="hljs-keyword">for</span> (<span class="hljs-params"><span class="hljs-keyword">let</span> i = 0; i &lt; 10; i += 1</span>) {
<span class="hljs-keyword">const</span> msg = <span class="hljs-keyword">await</span> consumer.receive(<span class="hljs-params"></span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-params">msg.getData(<span class="hljs-params"></span>).toString(<span class="hljs-params"></span>)</span>);
consumer.acknowledge(<span class="hljs-params">msg</span>);
}
<span class="hljs-keyword">await</span> consumer.close(<span class="hljs-params"></span>);
<span class="hljs-keyword">await</span> client.close(<span class="hljs-params"></span>);
}</span>)<span class="hljs-params">()</span>;
</span></code></pre></li>
<li><p>Run the consumer to receive encrypted messages.</p>
<p><strong>Input</strong></p>
<pre><code class="hljs css language-shell">node consumer.js
</code></pre></li>
<li><p>In a new terminal tab, run the producer to produce encrypted messages.</p>
<p><strong>Input</strong></p>
<pre><code class="hljs css language-shell">node producer.js
</code></pre>
<p>Now you can see the producer sends messages and the consumer receives messages successfully.</p>
<p><strong>Output</strong></p>
<p>This is from the producer side.</p>
<pre><code class="hljs">Sent <span class="hljs-keyword">message</span>: my-<span class="hljs-keyword">message</span>-<span class="hljs-number">0</span>
Sent <span class="hljs-keyword">message</span>: my-<span class="hljs-keyword">message</span>-<span class="hljs-number">1</span>
Sent <span class="hljs-keyword">message</span>: my-<span class="hljs-keyword">message</span>-<span class="hljs-number">2</span>
Sent <span class="hljs-keyword">message</span>: my-<span class="hljs-keyword">message</span>-<span class="hljs-number">3</span>
Sent <span class="hljs-keyword">message</span>: my-<span class="hljs-keyword">message</span>-<span class="hljs-number">4</span>
Sent <span class="hljs-keyword">message</span>: my-<span class="hljs-keyword">message</span>-<span class="hljs-number">5</span>
Sent <span class="hljs-keyword">message</span>: my-<span class="hljs-keyword">message</span>-<span class="hljs-number">6</span>
Sent <span class="hljs-keyword">message</span>: my-<span class="hljs-keyword">message</span>-<span class="hljs-number">7</span>
Sent <span class="hljs-keyword">message</span>: my-<span class="hljs-keyword">message</span>-<span class="hljs-number">8</span>
Sent <span class="hljs-keyword">message</span>: my-<span class="hljs-keyword">message</span>-<span class="hljs-number">9</span>
</code></pre>
<p>This is from the consumer side.</p>
<pre><code class="hljs">my-message<span class="hljs-number">-0</span>
my-message<span class="hljs-number">-1</span>
my-message<span class="hljs-number">-2</span>
my-message<span class="hljs-number">-3</span>
my-message<span class="hljs-number">-4</span>
my-message<span class="hljs-number">-5</span>
my-message<span class="hljs-number">-6</span>
my-message<span class="hljs-number">-7</span>
my-message<span class="hljs-number">-8</span>
my-message<span class="hljs-number">-9</span>
</code></pre></li>
</ol>
</span></div></article></div><div class="docs-prevnext"><a class="docs-prev button" href="/docs/en/2.9.2/client-libraries-cpp"><span class="arrow-prev"></span><span>C++</span></a><a class="docs-next button" href="/docs/en/2.9.2/client-libraries-websocket"><span class="function-name-prevnext">WebSocket</span><span class="arrow-next"></span></a></div></div></div><nav class="onPageNav"><ul class="toc-headings"><li><a href="#installation">Installation</a><ul class="toc-headings"><li><a href="#requirements">Requirements</a></li><li><a href="#compatibility">Compatibility</a></li><li><a href="#installation-using-npm">Installation using npm</a></li></ul></li><li><a href="#connection-urls">Connection URLs</a></li><li><a href="#create-a-client">Create a client</a><ul class="toc-headings"><li><a href="#client-configuration">Client configuration</a></li></ul></li><li><a href="#producers">Producers</a><ul class="toc-headings"><li><a href="#producer-operations">Producer operations</a></li><li><a href="#producer-configuration">Producer configuration</a></li><li><a href="#producer-example">Producer example</a></li></ul></li><li><a href="#consumers">Consumers</a><ul class="toc-headings"><li><a href="#consumer-operations">Consumer operations</a></li><li><a href="#consumer-configuration">Consumer configuration</a></li><li><a href="#consumer-example">Consumer example</a></li></ul></li><li><a href="#readers">Readers</a><ul class="toc-headings"><li><a href="#reader-operations">Reader operations</a></li><li><a href="#reader-configuration">Reader configuration</a></li><li><a href="#reader-example">Reader example</a></li></ul></li><li><a href="#messages">Messages</a><ul class="toc-headings"><li><a href="#message-object-operations">Message object operations</a></li><li><a href="#message-id-object-operations">Message ID object operations</a></li></ul></li><li><a href="#end-to-end-encryption">End-to-end encryption</a><ul class="toc-headings"><li><a href="#configuration">Configuration</a></li><li><a href="#tutorial">Tutorial</a></li></ul></li></ul></nav></div><footer class="nav-footer" id="footer"><section class="copyright">Copyright © 2022 The Apache Software Foundation. All Rights Reserved. Apache, Apache Pulsar and the Apache feather logo are trademarks of The Apache Software Foundation.</section><span><script>
const community = document.querySelector("a[href='#community']").parentNode;
const communityMenu =
'<li>' +
'<a id="community-menu" href="#">Community <span style="font-size: 0.75em">&nbsp;▼</span></a>' +
'<div id="community-dropdown" class="hide">' +
'<ul id="community-dropdown-items">' +
'<li><a href="/en/contact">Contact</a></li>' +
'<li><a href="/en/contributing">Contributing</a></li>' +
'<li><a href="/en/coding-guide">Coding guide</a></li>' +
'<li><a href="/en/events">Events</a></li>' +
'<li><a href="https://twitter.com/Apache_Pulsar" target="_blank">Twitter &#x2750</a></li>' +
'<li><a href="https://github.com/apache/pulsar/wiki" target="_blank">Wiki &#x2750</a></li>' +
'<li><a href="https://github.com/apache/pulsar/issues" target="_blank">Issue tracking &#x2750</a></li>' +
'<li><a href="https://pulsar-summit.org/" target="_blank">Pulsar Summit &#x2750</a></li>' +
'<li>&nbsp;</li>' +
'<li><a href="/en/resources">Resources</a></li>' +
'<li><a href="/en/team">Team</a></li>' +
'<li><a href="/en/powered-by">Powered By</a></li>' +
'</ul>' +
'</div>' +
'</li>';
community.innerHTML = communityMenu;
const communityMenuItem = document.getElementById("community-menu");
const communityDropDown = document.getElementById("community-dropdown");
communityMenuItem.addEventListener("click", function(event) {
event.preventDefault();
if (communityDropDown.className == 'hide') {
communityDropDown.className = 'visible';
} else {
communityDropDown.className = 'hide';
}
});
</script></span></footer></div><script>window.twttr=(function(d,s, id){var js,fjs=d.getElementsByTagName(s)[0],t=window.twttr||{};if(d.getElementById(id))return t;js=d.createElement(s);js.id=id;js.src='https://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js, fjs);t._e = [];t.ready = function(f) {t._e.push(f);};return t;}(document, 'script', 'twitter-wjs'));</script></body></html>