Publish and subscribe

The JavaScript SDK supports publishing events over HTTP or gRPC, programmatic subscriptions through DaprServer, and client-initiated streaming subscriptions through DaprClient. These guides describe @dapr/dapr 3.19.0 or later.

Prerequisites

Installation

Install the 3.19.0 release:

npm install @dapr/dapr@3.19.0

For a later release, substitute its version.

Code snippets use ES module imports. Run statements containing await in an async function or an ES module. Each guide identifies the client or server configuration used by its examples.

Choose a delivery mode

CapabilityHTTPgRPC
client.pubsub.publish()SupportedSupported
client.pubsub.publishBulk()SupportedSupported
server.pubsub.subscribe() and subscribeWithOptions()Inbound HTTP callbacksInbound gRPC callbacks
Content-based routing and subscribeToRoute()SupportedSupported
server.pubsub.subscribeBulk()Per-message callback from each batchPer-message callback from each batch
client.pubsub.subscribeStream()Not supportedClient-initiated stream; no application server

Choose server subscriptions when Dapr should deliver events to your application’s HTTP or gRPC listener. Subscriptions are discovered at application startup. Choose streaming subscriptions for workers that initiate the connection to Dapr and do not need an application callback listener. Streams can be created and closed at runtime, but do not support routing rules or bulk subscription options.

Runtime compatibility

Bulk publishing uses the stable API on Dapr 1.17 or later, with automatic fallback to the deprecated alpha1 API on older runtimes.

Streaming subscriptions use SubscribeTopicEventsAlpha1, an alpha gRPC API. Use a runtime that implements this API; installing the JavaScript package alone does not enable it. API availability and broker-specific features depend on the Dapr runtime and component.

Guides

GuideContents
Publishing eventsSingle and bulk publishing, metadata, CloudEvents, raw payloads, and publication failures.
Server subscriptionsRegistration, callback data, acknowledgements, routing, wildcards, bulk delivery, and dead-letter topics.
Streaming subscriptionsA complete worker, streaming options, cancellation, failure supervision, and cleanup.
Configuration and usageClient lifetime, endpoint and authentication settings, serialization, and reliable processing.

Implementation references

For implementation details, see the published 3.19.0 package and the upstream SDK implementation: