You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: How to get up and running with Actors using the Dapr JavaScript SDK
6
+
description: JavaScript SDK packages for developing Dapr applications
7
+
no_list: true
7
8
---
8
9
9
-
The Dapr actors package allows you to interact with Dapr virtual actors from a JavaScript application. The examples below demonstrate how to use the JavaScript SDK for interacting with virtual actors.
10
+
A client library for building Dapr apps in JavaScript and TypeScript. This client abstracts the public Dapr APIs like service to service invocation, state management, pub/sub, secrets, and much more, and provides a simple, intuitive API for building applications.
10
11
11
-
For a more in-depth overview of Dapr actors, visit the [actors overview page]({{< ref actors-overview >}}).
To get started with the Javascript SDK, install the Dapr JavaScript SDK package from [NPM](https://www.npmjs.com/package/@dapr/dapr):
18
15
19
-
## Scenario
20
-
The below code examples loosely describe the scenario of a Parking Garage Spot Monitoring System, which can be seen in this [video](https://www.youtube.com/watch?v=eJCu6a-x9uo&t=3785) by Mark Russinovich.
21
-
22
-
A parking garage consists of hundreds of parking spaces, where each parking space includes a sensor that provides updates to a centralized monitoring system. The parking space sensors (our actors) detect if a parking space is occupied or available.
23
-
24
-
To jump in and run this example yourself, clone the source code, which can be found in the [JavaScript SDK examples directory](https://github.com/dapr/js-sdk/tree/master/examples/http/actor-parking-sensor).
25
-
26
-
## Actor Interface
27
-
The actor interface defines the contract that is shared between the actor implementation and the clients calling the actor. In the example below, we have created an interace for a parking garage sensor. Each sensor has 2 methods: `carEnter` and `carLeave`, which defines the state of the parking space:
28
-
29
-
```ts
30
-
exportdefaultinterfaceParkingSensorInterface {
31
-
carEnter():Promise<void>;
32
-
carLeave():Promise<void>;
33
-
}
34
-
```
35
-
36
-
## Actor Implementation
37
-
An actor implementation defines a class by extending the base type `AbstractActor` and implementing the actor interface (`ParkingSensorInterface` in this case).
38
-
39
-
The following code describes an actor implementation along with a few helper methods.
After Actors are registered, create a Proxy object that implements `ParkingSensorInterface` using the `ActorProxyBuilder`. You can invoke the actor methods by directly calling methods on the Proxy object. Internally, it translates to making a network call to the Actor API and fetches the result back.
The JS SDK supports actors that can schedule periodic work on themselves by registering either timers or reminders. The main difference between timers and reminders is that the Dapr actor runtime does not retain any information about timers after deactivation, but persists reminders information using the Dapr actor state provider.
140
-
141
-
This distinction allows users to trade off between light-weight but stateless timers versus more resource-demanding but stateful reminders.
142
-
143
-
The scheduling interface of timers and reminders is identical. For an more in-depth look at the scheduling configurations see the [actors timers and reminders docs]({{< ref "howto-actors.md#actor-timers-and-reminders" >}}).
144
-
145
-
### Actor Timers
146
-
```javascript
147
-
// ...
148
-
149
-
constactor=builder.build(newActorId("my-actor"));
150
-
151
-
// Register a timer
152
-
awaitactor.registerActorTimer(
153
-
"timer-id", // Unique name of the timer.
154
-
"cb-method", // Callback method to execute when timer is fired.
155
-
Temporal.Duration.from({ seconds:2 }), // DueTime
156
-
Temporal.Duration.from({ seconds:1 }), // Period
157
-
Temporal.Duration.from({ seconds:1 }), // TTL
158
-
50// State to be sent to timer callback.
159
-
);
160
-
161
-
// Delete the timer
162
-
awaitactor.unregisterActorTimer("timer-id");
163
-
```
164
-
165
-
### Actor Reminders
166
-
```javascript
167
-
// ...
168
-
169
-
constactor=builder.build(newActorId("my-actor"));
170
-
171
-
// Register a reminder, it has a default callback: `receiveReminder`
To handle the callback, you need to override the default `receiveReminder` implementation in your actor. For example, from our original actor implementation:
For a full guide on actors, visit [How-To: Use virtual actors in Dapr]({{< ref howto-actors.md >}}).
20
+
> ⚠️ [dapr-client](https://www.npmjs.com/package/dapr-client) is now deprecated. Please see [#259](https://github.com/dapr/js-sdk/issues/259) for more information.
21
+
22
+
## Structure
23
+
24
+
The Dapr Javascript SDK contains two major components:
25
+
26
+
***DaprServer**: to manage all Dapr sidecar to application communication.
27
+
***DaprClient**: to manage all application to Dapr sidecar communication.
28
+
29
+
The above communication can be configured to use either of the gRPC or HTTP protocols.
To help you get started, check out the resources below:
41
+
42
+
<divclass="card-deck">
43
+
<divclass="card">
44
+
<div class="card-body">
45
+
<h5 class="card-title"><b>Client</b></h5>
46
+
<p class="card-text">Create a JavaScript client and interact with the Dapr sidecar and other Dapr applications (e.g., publishing events, output binding support, etc.). </p>
<p class="card-text">Create a JavaScript server and let the Dapr sidecar interact with your application (e.g., subscribing to events, input binding support, etc.). </p>
0 commit comments