Skip to content

Commit 500a739

Browse files
committed
Continue adding actors examples
1 parent 937ea18 commit 500a739

File tree

1 file changed

+58
-6
lines changed
  • daprdocs/content/en/js-sdk-docs/js-actors

1 file changed

+58
-6
lines changed

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

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ weight: 1000
66
description: How to get up and running with Actors using the Dapr JavaScript SDK
77
---
88

9-
The Dapr actor package allows you to interact with Dapr virtual actors from a Python application. The below examples demonstarte how to use virtual actors through the Python SDK.
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.
1010

11-
For a more indepth overview of Dapr actors and supported scenarios, visit the [actors overview]({{< ref actors-overview >}}) page.
11+
For a more in-depth overview of Dapr actors and supported scenarios, visit the [actors overview page]({{< ref actors-overview >}}).
1212

1313
## Pre-requisites
1414
- [Dapr CLI]({{< ref install-dapr-cli.md >}}) installed
@@ -70,10 +70,62 @@ async function start() {
7070
}
7171
```
7272

73+
## Saving and Getting State
74+
75+
```javascript
76+
import { DaprServer, DaprClient } from "dapr-client";
77+
import DemoActor from "./actor/DemoActor";
78+
79+
const daprHost = "127.0.0.1";
80+
const daprPort = "50000"; // Dapr Sidecar Port of this Example Server
81+
const serverHost = "127.0.0.1"; // App Host of this Example Server
82+
const serverPort = "50001"; // App Port of this Example Server
83+
const daprAppId = "example-hello-world";
84+
85+
async function start() {
86+
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
87+
const client = new DaprClient(daprHost, daprPort);
88+
89+
// Creating actor bindings
90+
await server.actor.init();
91+
server.actor.registerActor(DemoActor);
92+
await server.startServer();
93+
94+
const actorId = "actor-id";
95+
96+
await client.actor.stateTransaction("DemoActor", actorId, [
97+
{
98+
operation: "upsert",
99+
request: {
100+
key: "key-1",
101+
value: "my-new-data-1"
102+
}
103+
},
104+
{
105+
operation: "upsert",
106+
request: {
107+
key: "key-to-delete",
108+
value: "my-new-data-1"
109+
}
110+
},
111+
{
112+
operation: "delete",
113+
request: {
114+
key: "key-to-delete"
115+
}
116+
}
117+
]);
118+
119+
const ActorStateGet = await client.actor.stateGet("DemoActor", actorId, "key-to-delete");
120+
}
121+
```
122+
73123
## Actor Timers and Reminders
74-
The JS SDK supports actors that can schedule periodic work on themselves by registering either timers or reminders..
124+
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 is not retaining any information about timers after deactivation, while persisting the information about reminders using the Dapr actor state provider.
125+
126+
This distintcion allows users to trade off between light-weight but stateless timers vs. more resource-demanding but stateful reminders.
75127

76-
The scheduling interface of timers and reminders is identical.
128+
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" >}}).
77129

78130
### Actor Timers
79131
```javascript
@@ -116,7 +168,7 @@ async function start() {
116168
### Actor Reminders
117169
```javascript
118170
import { DaprServer, DaprClient } from "dapr-client";
119-
import ActorTimerImpl from "./actor/ActorSayImpl";
171+
import ActorReminderImpl from "./actor/ActorReminderImpl";
120172

121173
const daprHost = "127.0.0.1";
122174
const daprPort = "50000"; // Dapr Sidecar Port of this Example Server
@@ -130,7 +182,7 @@ async function start() {
130182

131183
// Creating actor bindings
132184
await server.actor.init();
133-
server.actor.registerActor(ActorCounterImpl);
185+
server.actor.registerActor(ActorReminderImpl);
134186
await server.startServer();
135187

136188
const actorId = "actor-id";

0 commit comments

Comments
 (0)