<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Publish and subscribe with the JavaScript SDK on Dapr Docs</title><link>https://docs.dapr.io/developing-applications/sdks/js/js-pubsub/</link><description>Recent content in Publish and subscribe with the JavaScript SDK on Dapr Docs</description><generator>Hugo</generator><language>en</language><atom:link href="https://docs.dapr.io/developing-applications/sdks/js/js-pubsub/index.xml" rel="self" type="application/rss+xml"/><item><title>Publish events with the JavaScript SDK</title><link>https://docs.dapr.io/developing-applications/sdks/js/js-pubsub/js-pubsub-publish-howto/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://docs.dapr.io/developing-applications/sdks/js/js-pubsub/js-pubsub-publish-howto/</guid><description>&lt;p>Use &lt;code>DaprClient.pubsub&lt;/code> to publish events over HTTP or gRPC with &lt;code>@dapr/dapr&lt;/code> 3.19.0 or later. Complete the &lt;a href="https://docs.dapr.io/developing-applications/sdks/js/js-pubsub/">pub/sub prerequisites&lt;/a> before following this guide.&lt;/p>
&lt;h2 id="configure-a-publisher">Configure a publisher&lt;/h2>
&lt;p>The examples use this HTTP client, component &lt;code>pubsub&lt;/code>, and topic &lt;code>orders&lt;/code>:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-javascript" data-lang="javascript">&lt;span class="line">&lt;span class="cl">&lt;span class="kr">import&lt;/span> &lt;span class="p">{&lt;/span> &lt;span class="nx">CommunicationProtocolEnum&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nx">DaprClient&lt;/span> &lt;span class="p">}&lt;/span> &lt;span class="nx">from&lt;/span> &lt;span class="s2">&amp;#34;@dapr/dapr&amp;#34;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kr">const&lt;/span> &lt;span class="nx">client&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="k">new&lt;/span> &lt;span class="nx">DaprClient&lt;/span>&lt;span class="p">({&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">daprHost&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s2">&amp;#34;127.0.0.1&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">daprPort&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s2">&amp;#34;3500&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">communicationProtocol&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="nx">CommunicationProtocolEnum&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">HTTP&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">});&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>For gRPC, select &lt;code>CommunicationProtocolEnum.GRPC&lt;/code> and port &lt;code>&amp;quot;50001&amp;quot;&lt;/code>. Run statements containing &lt;code>await&lt;/code> in an async function or an ES module. Reuse the client and stop it during application shutdown; see &lt;a href="https://docs.dapr.io/developing-applications/sdks/js/js-pubsub/js-pubsub-usage/">configuration and usage&lt;/a>.&lt;/p></description></item><item><title>Receive events with a JavaScript application server</title><link>https://docs.dapr.io/developing-applications/sdks/js/js-pubsub/js-pubsub-subscribe-howto/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://docs.dapr.io/developing-applications/sdks/js/js-pubsub/js-pubsub-subscribe-howto/</guid><description>&lt;p>Use &lt;code>DaprServer.pubsub&lt;/code> to receive events through HTTP or gRPC callbacks with &lt;code>@dapr/dapr&lt;/code> 3.19.0 or later. Complete the &lt;a href="https://docs.dapr.io/developing-applications/sdks/js/js-pubsub/">pub/sub prerequisites&lt;/a> before following this guide. For workers without an application callback listener, use &lt;a href="https://docs.dapr.io/developing-applications/sdks/js/js-pubsub/js-pubsub-streaming-howto/">streaming subscriptions&lt;/a> instead.&lt;/p>
&lt;h2 id="register-subscriptions-before-startup">Register subscriptions before startup&lt;/h2>
&lt;p>Save the following as &lt;code>subscriber.js&lt;/code> in an ES module application:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-javascript" data-lang="javascript">&lt;span class="line">&lt;span class="cl">&lt;span class="kr">import&lt;/span> &lt;span class="p">{&lt;/span> &lt;span class="nx">CommunicationProtocolEnum&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nx">DaprPubSubStatusEnum&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nx">DaprServer&lt;/span> &lt;span class="p">}&lt;/span> &lt;span class="nx">from&lt;/span> &lt;span class="s2">&amp;#34;@dapr/dapr&amp;#34;&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kr">const&lt;/span> &lt;span class="nx">server&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="k">new&lt;/span> &lt;span class="nx">DaprServer&lt;/span>&lt;span class="p">({&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">serverHost&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s2">&amp;#34;127.0.0.1&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">serverPort&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s2">&amp;#34;50002&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">communicationProtocol&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="nx">CommunicationProtocolEnum&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">HTTP&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">clientOptions&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="p">{&lt;/span> &lt;span class="nx">daprHost&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s2">&amp;#34;127.0.0.1&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nx">daprPort&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="s2">&amp;#34;3500&amp;#34;&lt;/span> &lt;span class="p">},&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">});&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kr">await&lt;/span> &lt;span class="nx">server&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">pubsub&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">subscribe&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;pubsub&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s2">&amp;#34;orders&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="kr">async&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="nx">data&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nx">headers&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">log&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;Order received:&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nx">data&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nx">headers&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="nx">DaprPubSubStatusEnum&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">SUCCESS&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">});&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kr">await&lt;/span> &lt;span class="nx">server&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">start&lt;/span>&lt;span class="p">();&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Run the subscriber with an application port matching &lt;code>serverPort&lt;/code>:&lt;/p></description></item><item><title>Receive events with JavaScript streaming subscriptions</title><link>https://docs.dapr.io/developing-applications/sdks/js/js-pubsub/js-pubsub-streaming-howto/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://docs.dapr.io/developing-applications/sdks/js/js-pubsub/js-pubsub-streaming-howto/</guid><description>&lt;p>Use &lt;code>DaprClient.pubsub.subscribeStream()&lt;/code> to open a bidirectional gRPC stream from your application to the sidecar with &lt;code>@dapr/dapr&lt;/code> 3.19.0 or later. It does not require &lt;code>DaprServer&lt;/code>, an application callback port, or subscription discovery.&lt;/p>
&lt;p>Complete the &lt;a href="https://docs.dapr.io/developing-applications/sdks/js/js-pubsub/">pub/sub prerequisites&lt;/a> first. Streaming uses &lt;code>SubscribeTopicEventsAlpha1&lt;/code>, an alpha gRPC API; your Dapr runtime must implement it. HTTP clients reject this operation with &lt;code>HTTPNotSupportedError&lt;/code>. Streaming subscriptions do not support routing rules or bulk subscription options.&lt;/p>
&lt;h2 id="run-a-streaming-worker">Run a streaming worker&lt;/h2>
&lt;p>Save the following as &lt;code>worker.js&lt;/code> in an ES module application. The worker runs until SIGINT or SIGTERM, or until the stream fails:&lt;/p></description></item><item><title>JavaScript pub/sub configuration and usage</title><link>https://docs.dapr.io/developing-applications/sdks/js/js-pubsub/js-pubsub-usage/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://docs.dapr.io/developing-applications/sdks/js/js-pubsub/js-pubsub-usage/</guid><description>&lt;p>This guide covers shared configuration and operational guidance for pub/sub with &lt;code>@dapr/dapr&lt;/code> 3.19.0 or later. See the &lt;a href="https://docs.dapr.io/developing-applications/sdks/js/js-pubsub/">overview&lt;/a> to choose between server callbacks and streaming subscriptions.&lt;/p>
&lt;h2 id="client-lifetime">Client lifetime&lt;/h2>
&lt;p>Reuse a long-lived &lt;code>DaprClient&lt;/code> rather than creating one per message. Await outstanding operations before calling &lt;code>await client.stop()&lt;/code> at shutdown.&lt;/p>
&lt;p>For server subscriptions, stop the application listener with &lt;code>await server.stop()&lt;/code>. For streaming subscriptions, close each subscription and await its &lt;code>closed&lt;/code> promise before stopping the client; &lt;code>client.stop()&lt;/code> does not close streams. See &lt;a href="https://docs.dapr.io/developing-applications/sdks/js/js-pubsub/js-pubsub-streaming-howto/#supervise-failures-and-shut-down">streaming lifecycle guidance&lt;/a>.&lt;/p></description></item></channel></rss>