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
7
7
---
8
8
9
-
The Dapr actors package allows you to interact with Dapr virtual actors from a JavaScript application. The below examples demonstarte how to use the JavaScript SDK for interacting with virtual actors.
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
10
11
11
For a more in-depth overview of Dapr actors and supported scenarios, visit the [actors overview page]({{< ref actors-overview >}}).
12
12
@@ -16,35 +16,53 @@ For a more in-depth overview of Dapr actors and supported scenarios, visit the [
16
16
-[Latest LTS version of Node or greater](https://nodejs.org/en/) d
The below code examples loosely describe the scenario of a Parking Garage Spot Monitoring System, which can be explained in detail by Mark Russinovich in [this video](https://www.youtube.com/watch?v=eJCu6a-x9uo&t=3785). The parking garage consists of hundreds of parking spots, where each parking spot 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.
21
+
22
+
To run this example, source code can be found [here](https://github.com/XavierGeerinck/js-sdk/tree/23e1f0ee2bd4c60a4906e38427547c4b3840f89e/examples/http/actor-parking-sensor).
23
+
19
24
## Actor Interface
20
-
The actor interface defines the contract that is shared between the actor implementation and the clients calling the actor.
25
+
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 sensor:
21
26
22
27
```javascript
23
-
exportdefaultinterface ActorSayInterface {
24
-
say(msg: string): string;
28
+
exportdefaultinterface ParkingSensorInterface {
29
+
carEnter():Promise<void>;
30
+
carLeave():Promise<void>;
25
31
}
26
32
```
27
33
28
34
## Actor Implementation
29
-
An actor implementation defines a class by extending the base type `AbstractActor` and implements the interfaces defined in the actor interface.
35
+
An actor implementation defines a class by extending the base type `AbstractActor` and implements the interfaces defined in the actor interface. The example below implements the methods defined in the `ParkingSensorInterface`, but also adds extra helper methods:
// Implementation that updates state that this parking spaces is occupied.
44
+
}
45
+
46
+
asynccarLeave():Promise<void> {
47
+
// Implementation that updates state that this parking spaces is available.
48
+
}
49
+
50
+
asyncgetParkingSpaceUpdate():Promise<object> {
51
+
// Implementation of requesting an update from the parking space sensor.
52
+
}
53
+
54
+
asynconActivate():Promise<void> {
55
+
// Initialization logic called by AbstractActor.
56
+
}
39
57
}
40
58
```
41
59
42
-
## Invoking Actors
43
-
Use the DaprServer package to create your actors bindings, which will initalize and register your actors. After Actors are registered, use the DaprClient to invoke methods on an actor. The client will call the actor methods defined in the actor interface.
60
+
## Registering Actors
61
+
Use the DaprServer package to create your actors bindings, which will initalize and register your actors.
0 commit comments