Skip to content

Commit 57e3287

Browse files
committed
Start of adding Actors docs and examples
1 parent 1e23f32 commit 57e3287

File tree

2 files changed

+85
-22
lines changed

2 files changed

+85
-22
lines changed

daprdocs/content/en/js-sdk-docs/_index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ The Dapr JS SDK will allow you to interface with the Dapr process that abstracts
1818
<a href="{{< ref js-client >}}" class="stretched-link"></a>
1919
</div>
2020
</div>
21-
<!-- <div class="card">
21+
<div class="card">
2222
<div class="card-body">
2323
<h5 class="card-title"><b>Actors</b></h5>
2424
<p class="card-text">Create virtual actors with state, reminders/timers, and methods in JavaScript.</p>
2525
<a href="{{< ref js-actors >}}" class="stretched-link"></a>
2626
</div>
27-
</div> -->
27+
</div>
2828
<div class="card">
2929
<div class="card-body">
3030
<h5 class="card-title"><b>Examples</b></h5>
Lines changed: 83 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,54 @@
1-
<!-- ---
1+
---
22
type: docs
3-
# title: "JavaScript SDK for Actors"
3+
title: "JavaScript SDK for Actors"
44
linkTitle: "Actors"
55
weight: 1000
6-
description: JavaScript SDK package for Actors
6+
description: How to get up and running with Actors using the Dapr JavaScript SDK
77
---
88

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.
1042

1143
```javascript
1244
import { DaprServer, DaprClient, HttpMethod } from "dapr-client";
13-
import DemoActorCounterImpl from "./actor/DemoActorCounterImpl";
14-
import DemoActorReminderImpl from "./actor/DemoActorReminderImpl";
1545
import DemoActorSayImpl from "./actor/DemoActorSayImpl";
16-
import DemoActorTimerImpl from "./actor/DemoActorTimerImpl";
1746

1847
const daprHost = "127.0.0.1";
1948
const daprPort = "50000"; // Dapr Sidecar Port of this Example Server
2049
const serverHost = "127.0.0.1"; // App Host of this Example Server
2150
const serverPort = "50001"; // App Port of this Example Server
51+
const daprAppId = "example-hello-world";
2252

2353
async function start() {
2454
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
@@ -27,23 +57,56 @@ async function start() {
2757
// Creating actor bindings
2858
await server.actor.init();
2959
server.actor.registerActor(DemoActorCounterImpl);
30-
server.actor.registerActor(DemoActorSayImpl);
31-
server.actor.registerActor(DemoActorTimerImpl);
32-
server.actor.registerActor(DemoActorReminderImpl);
3360

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);
3790
await server.startServer();
3891

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";
4394

95+
// Activate actor
96+
await client.actor.invoke("PUT", DemoActorTimerImpl.name, actorId, "init");
4497

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+
}
47108
```
48109

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

Comments
 (0)