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: JavaScript SDK packages for developing Dapr applications
7
-
no_list: true
6
+
description: How to get up and running with Actors using the Dapr JavaScript SDK
8
7
---
9
8
10
-
The Dapr JS SDK will allow you to interface with the Dapr process that abstracts several commonly used functionalities such as Service-to-Service invocation, State Management, PubSub, and more.
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.
11
10
12
-
## Installation
11
+
For a more in-depth overview of Dapr actors, visit the [actors overview page]({{< ref actors-overview >}}).
13
12
14
-
To get started with the Javascript SDK, you can download the Dapr Javascript SDK package from [NPM](https://npmjs.org/package/dapr-client) by running the following:
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:
The Dapr Javascript SDK contains two major components:
23
-
24
-
***DaprServer:** The Dapr Server manages all communication from the Dapr Sidecar to your application
25
-
***DaprClient:** The Dapr Client manages all communication from your application to the Dapr Sidecar
26
-
27
-
The above communication can be configured to use either of the gRPC or HTTP protocols.
28
-
29
-

30
-

31
-
32
-
## Get Started
33
-
34
-
To help you get started, check out the resources below:
35
-
36
-
<divclass="card-deck">
37
-
<divclass="card">
38
-
<div class="card-body">
39
-
<h5 class="card-title"><b>Client</b></h5>
40
-
<p class="card-text">Create a JavaScript client and interact with a 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>
-[DaprClient]({{< ref "js-client#installing-and-importing-daprs-js-sdk" >}}) is a package that for how your application interacts with the Dapr sidecar, or other Dapr powered applications.
69
-
70
-
-[DaprServer]({{< ref "js-client#installing-and-importing-daprs-js-sdk" >}}) is a package for how the Dapr sidecar interacts with your application, forwarding event subscriptions, invokes and more.
200
+
For a full guide on actors, visit [How-To: Use virtual actors in Dapr]({{< ref howto-actors.md >}}).
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.
@@ -39,7 +39,7 @@ An actor implementation defines a class by extending the base type `AbstractActo
39
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.
0 commit comments