1
- <!-- ---
1
+ ---
2
2
type : docs
3
- # title: "JavaScript SDK for Actors"
3
+ title : " JavaScript SDK for Actors"
4
4
linkTitle : " Actors"
5
5
weight : 1000
6
- description: JavaScript SDK package for Actors
6
+ description : How to get up and running with Actors using the Dapr JavaScript SDK
7
7
---
8
8
9
- ### Actors
9
+ The Dapr actor package allows you to interact with Dapr virtual actors from a Python application.
10
+
11
+ ## Pre-requisites
12
+ - [ Dapr CLI] ({{< ref install-dapr-cli.md >}}) installed
13
+ - Initialized [ Dapr environment] ({{< ref install-dapr-selfhost.md >}})
14
+ - [ Latest LTS version of Node or greater] ( https://nodejs.org/en/ ) d
15
+ - [ JavaScript NPM package installed] ( https://www.npmjs.com/package/dapr-client )
16
+
17
+ ## Actor Interface
18
+ The interface defines the actor contract that is shared between the actor implementation and the clients calling the actor. Because a client may depend on it, it typically makes sense to define it in an assembly that is separate from the actor implementation.
19
+
20
+ ``` javascript
21
+ export default interface DemoActorSayInterface {
22
+ sayString (msg: string): string;
23
+ }
24
+ ```
25
+
26
+ ## Actor Implementation
27
+ An actor service hosts the virtual actor. It defines a class by extending the base type Actor and implements the interfaces defined in the actor interface.
28
+
29
+ ``` javascript
30
+ import { AbstractActor } from " dapr-client" ;
31
+ import DemoActorSayInterface from " ./DemoActorSayInterface" ;
32
+
33
+ export default class DemoActorSayImpl extends AbstractActor implements DemoActorSayInterface {
34
+ say (msg: string): string {
35
+ return ` Actor said: "${ msg} "` ;
36
+ }
37
+ }
38
+ ```
39
+
40
+ ## Invoking Actors
41
+ An actor client contains the implementation of the actor client which calls the actor methods defined in the actor interface.
10
42
11
43
``` javascript
12
44
import { DaprServer , DaprClient , HttpMethod } from " dapr-client" ;
13
- import DemoActorCounterImpl from "./actor/DemoActorCounterImpl";
14
- import DemoActorReminderImpl from "./actor/DemoActorReminderImpl";
15
45
import DemoActorSayImpl from " ./actor/DemoActorSayImpl" ;
16
- import DemoActorTimerImpl from "./actor/DemoActorTimerImpl";
17
46
18
47
const daprHost = " 127.0.0.1" ;
19
48
const daprPort = " 50000" ; // Dapr Sidecar Port of this Example Server
20
49
const serverHost = " 127.0.0.1" ; // App Host of this Example Server
21
50
const serverPort = " 50001" ; // App Port of this Example Server
51
+ const daprAppId = " example-hello-world" ;
22
52
23
53
async function start () {
24
54
const server = new DaprServer (serverHost, serverPort, daprHost, daprPort);
@@ -27,23 +57,56 @@ async function start() {
27
57
// Creating actor bindings
28
58
await server .actor .init ();
29
59
server .actor .registerActor (DemoActorCounterImpl);
30
- server.actor.registerActor(DemoActorSayImpl);
31
- server.actor.registerActor(DemoActorTimerImpl);
32
- server.actor.registerActor(DemoActorReminderImpl);
33
60
34
- // We initialize after registering our listeners since these should be defined upfront
35
- // this is how Dapr works, it waits until we are listening on the port. Once that is detected
36
- // it will scan the binding list and pubsub subscriptions list to process
61
+ const actorId = " actor-id" ;
62
+ const timerId = " actor-timer-id" ;
63
+
64
+ await server .startServer ();
65
+
66
+ // Invoke method 'say' with msg 'Hello World'");
67
+ const resActorInvokeSay = await client .actor .invoke (" PUT" , DemoActorSayImpl .name , actorId, " method-to-invoke" , " Hello World" );
68
+ }
69
+ ```
70
+
71
+ ## Actor Timers
72
+
73
+ ``` javascript
74
+ import { DaprServer , DaprClient } from " dapr-client" ;
75
+ import DemoActorTimerImpl from " ./actor/DemoActorSayImpl" ;
76
+
77
+ const daprHost = " 127.0.0.1" ;
78
+ const daprPort = " 50000" ; // Dapr Sidecar Port of this Example Server
79
+ const serverHost = " 127.0.0.1" ; // App Host of this Example Server
80
+ const serverPort = " 50001" ; // App Port of this Example Server
81
+ const daprAppId = " example-hello-world" ;
82
+
83
+ async function start () {
84
+ const server = new DaprServer (serverHost, serverPort, daprHost, daprPort);
85
+ const client = new DaprClient (daprHost, daprPort);
86
+
87
+ // Creating actor bindings
88
+ await server .actor .init ();
89
+ server .actor .registerActor (DemoActorCounterImpl);
37
90
await server .startServer ();
38
91
39
- console.log("===============================================================");
40
- console.log("EXECUTING CLIENT - ACTORS");
41
- console.log("Note: we create new client for now since Actors are not supported internally!")
42
- console.log("===============================================================");
92
+ const actorId = " actor-id" ;
93
+ const timerId = " actor-timer-id" ;
43
94
95
+ // Activate actor
96
+ await client .actor .invoke (" PUT" , DemoActorTimerImpl .name , actorId, " init" );
44
97
45
- const resRegisteredActors = await server.actor.getRegisteredActors();
46
- console.log(`Registered Actor Types: ${JSON.stringify(resRegisteredActors)}`);
98
+ // Register a timer
99
+ await client .actor .timerCreate (DemoActorTimerImpl .name , actorId, timerId, {
100
+ callback: " method-to-excute-on-actor" ,
101
+ dueTime: Temporal .Duration .from ({ seconds: 2 }),
102
+ period: Temporal .Duration .from ({ seconds: 1 })
103
+ });
104
+
105
+ // Stop the timer
106
+ await client .actor .timerDelete (DemoActorTimerImpl .name , actorId, timerId);
107
+ }
47
108
```
48
109
49
- - For a full guide on actors visit [How-To: Use virtual actors in Dapr]({{< ref howto-actors.md >}}). -->
110
+ ## Actor Reminders
111
+
112
+ - For a full guide on actors visit [ How-To: Use virtual actors in Dapr] ({{< ref howto-actors.md >}}).
0 commit comments