Skip to content

Commit dbbb0f0

Browse files
committed
Update examples
1 parent a302c22 commit dbbb0f0

File tree

1 file changed

+91
-62
lines changed
  • daprdocs/content/en/js-sdk-docs/js-actors

1 file changed

+91
-62
lines changed

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

Lines changed: 91 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description: How to get up and running with Actors using the Dapr JavaScript SDK
88

99
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.
1010

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

1313
## Pre-requisites
1414
- [Dapr CLI]({{< ref install-dapr-cli.md >}}) installed
@@ -17,14 +17,14 @@ For a more in-depth overview of Dapr actors and supported scenarios, visit the [
1717
- [JavaScript NPM package installed](https://www.npmjs.com/package/dapr-client)
1818

1919
## Scenario
20-
The below code examples loosely describe the scenario of a Parking Garage Spot Monitoring System, which can be seen in this [video by Mark Russinovich](https://www.youtube.com/watch?v=eJCu6a-x9uo&t=3785).
20+
The below code examples loosely describe the scenario of a Parking Garage Spot Monitoring System, which can be seen in this [video] by Mark Russinovich(https://www.youtube.com/watch?v=eJCu6a-x9uo&t=3785).
2121

22-
A 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.
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.
2323

24-
To jump in and run this example for yourself, the source code can be found in the [JavaScript SDK examples directory](https://github.com/dapr/js-sdk/tree/master/examples/http/actor-parking-sensor).
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).
2525

2626
## 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:
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:
2828

2929
```javascript
3030
export default interface ParkingSensorInterface {
@@ -34,7 +34,7 @@ export default interface ParkingSensorInterface {
3434
```
3535

3636
## Actor Implementation
37-
An actor implementation defines a class by extending the base type `AbstractActor` and implements the interfaces defined in the actor interface. The following code describes what an actor implmentation consists of by implementing the methods defined in the `ParkingSensorInterface`. It also defines a few extra helper methods:
37+
An actor implementation defines a class by extending the base type `AbstractActor` and implements the actor interface. The following code describes what an actor implmentation consists of by implementing the methods defined in the `ParkingSensorInterface`. It also defines a few extra helper methods:
3838

3939
```javascript
4040
import { AbstractActor } from "dapr-client";
@@ -60,19 +60,14 @@ export default class ParkingSensorImpl extends AbstractActor implements ParkingS
6060
```
6161

6262
## Registering Actors
63-
Use the DaprServer package to create your actors bindings, which will initalize and register your actors.
63+
Initialize and register your actors by using the DaprServer package:
6464

6565
```javascript
66-
import { DaprServer, DaprClient } from "dapr-client";
66+
import { DaprServer } from "dapr-server";
6767
import ParkingSensorImpl from "./ParkingSensorImpl";
6868

69-
const daprHost = "127.0.0.1";
70-
const daprPort = "50000"; // Dapr Sidecar Port of this Example Server
71-
const serverHost = "127.0.0.1"; // App Host of this Example Server
72-
const serverPort = "50001"; // App Port of this Example Server
73-
7469
async function start() {
75-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
70+
const server = new DaprServer(`server-host`, `server-port`, `dapr-host`, `dapr-port`);
7671

7772
await server.actor.init(); // Let the server know we need actors
7873
server.actor.registerActor(ParkingSensorImpl); // Register the actor
@@ -84,40 +79,58 @@ async function start() {
8479
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.
8580

8681
```javascript
87-
...
88-
// Initialize Dapr client
89-
const client = new DaprClient(daprHost, daprPort);
82+
import { DaprClient, DaprServer } from "dapr-client";
83+
import ParkingSensorImpl from "./ParkingSensorImpl";
9084

91-
await client.actor.invoke("PUT", ParkingSensorImpl.name, `actor-id`, "carEnter");
92-
...
85+
async function start() {
86+
const server = new DaprServer(`server-host`, `server-port`, `dapr-host`, `dapr-port`);
87+
const client = new DaprClient(`dapr-host`, `dapr-port`);
88+
89+
await server.actor.init();
90+
server.actor.registerActor(ParkingSensorImpl);
91+
await server.startServer();
92+
93+
94+
await client.actor.invoke("PUT", ParkingSensorImpl.name, `actor-id`, "carEnter"); // Invoke the ParkingSensor Actor by calling the carEnter function
95+
}
9396
```
9497

9598
## Saving and Getting State
9699

97100
```javascript
98-
...
99-
const client = new DaprClient(`dapr-host`, `dapr-port`);
100-
101-
await client.actor.stateTransaction("ParkingSensorImpl", `actor-id`, [
102-
{
103-
operation: "upsert",
104-
request: {
105-
key: "parking-sensor-location-lat",
106-
value: "location-x"
107-
}
108-
},
109-
{
110-
operation: "upsert",
111-
request: {
112-
key: "parking-sensor-location-lang",
113-
value: "location-y"
114-
}
115-
}
116-
]);
101+
import { DaprClient, DaprServer } from "dapr-client";
102+
import ParkingSensorImpl from "./ParkingSensorImpl";
117103

104+
async function start() {
105+
const server = new DaprServer(`server-host`, `server-port`, `dapr-host`, `dapr-port`);
106+
const client = new DaprClient(`dapr-host`, `dapr-port`);
107+
108+
await server.actor.init();
109+
server.actor.registerActor(ParkingSensorImpl);
110+
await server.startServer();
111+
112+
// Perform state transaction
113+
await client.actor.stateTransaction("ParkingSensorImpl", `actor-id`, [
114+
{
115+
operation: "upsert",
116+
request: {
117+
key: "parking-sensor-location-lat",
118+
value: "location-x"
119+
}
120+
},
121+
{
122+
operation: "upsert",
123+
request: {
124+
key: "parking-sensor-location-lang",
125+
value: "location-y"
126+
}
127+
}
128+
]);
118129

119-
await client.actor.stateGet("ParkingSensorImpl", `actor-id`, `parking-sensor-location-lat`)
120-
await client.actor.stateGet("ParkingSensorImpl", `actor-id`, `parking-sensor-location-lang`)
130+
// GET state from an actor
131+
await client.actor.stateGet("ParkingSensorImpl", `actor-id`, `parking-sensor-location-lat`)
132+
await client.actor.stateGet("ParkingSensorImpl", `actor-id`, `parking-sensor-location-lang`)
133+
}
121134
...
122135
```
123136

@@ -130,37 +143,53 @@ The scheduling interface of timers and reminders is identical. For an more in-de
130143

131144
### Actor Timers
132145
```javascript
133-
...
134-
const client = new DaprClient(`dapr-host`, `dapr-port`);
146+
import { DaprClient, DaprServer } from "dapr-client";
147+
import ParkingSensorImpl from "./ParkingSensorImpl";
135148

136-
// Register a timer
137-
await client.actor.timerCreate(ParkingSensorImpl.name, `actor-id`, `timer-id`, {
138-
callback: "method-to-excute-on-actor",
139-
dueTime: Temporal.Duration.from({ seconds: 2 }),
140-
period: Temporal.Duration.from({ seconds: 1 })
141-
});
149+
async function start()
150+
const server = new DaprServer(`server-host`, `server-port`, `dapr-host`, `dapr-port`);
151+
const client = new DaprClient(`dapr-host`, `dapr-port`);
142152

143-
// Delete the timer
144-
await client.actor.timerDelete(ParkingSensorImpl.name, `actor-id`, `timer-id`);
145-
...
153+
await server.actor.init();
154+
server.actor.registerActor(ParkingSensorImpl);
155+
await server.startServer();
156+
157+
// Register a timer
158+
await client.actor.timerCreate(ParkingSensorImpl.name, `actor-id`, `timer-id`, {
159+
callback: "method-to-excute-on-actor",
160+
dueTime: Temporal.Duration.from({ seconds: 2 }),
161+
period: Temporal.Duration.from({ seconds: 1 })
162+
});
163+
164+
// Delete the timer
165+
await client.actor.timerDelete(ParkingSensorImpl.name, `actor-id`, `timer-id`);
146166
}
147167
```
148168

149169
### Actor Reminders
150170
```javascript
151-
...
152-
const client = new DaprClient(`dapr-host`, `dapr-port`);
171+
import { DaprClient, DaprServer } from "dapr-client";
172+
import ParkingSensorImpl from "./ParkingSensorImpl";
153173

154-
// Register a reminder, it has a default callback
155-
await client.actor.reminderCreate(DemoActorImpl.name, `actor-id`, `timer-id`, {
156-
dueTime: Temporal.Duration.from({ seconds: 2 }),
157-
period: Temporal.Duration.from({ seconds: 1 }),
158-
data: 100
159-
});
174+
async function start()
175+
const server = new DaprServer(`server-host`, `server-port`, `dapr-host`, `dapr-port`);
176+
const client = new DaprClient(`dapr-host`, `dapr-port`);
160177

161-
// Delete the reminder
162-
await client.actor.reminderDelete(DemoActorImpl.name, `actor-id`, `timer-id`);
163-
...
178+
await server.actor.init();
179+
server.actor.registerActor(ParkingSensorImpl);
180+
await server.startServer();
181+
182+
183+
// Register a reminder, it has a default callback
184+
await client.actor.reminderCreate(DemoActorImpl.name, `actor-id`, `timer-id`, {
185+
dueTime: Temporal.Duration.from({ seconds: 2 }),
186+
period: Temporal.Duration.from({ seconds: 1 }),
187+
data: 100
188+
});
189+
190+
// Delete the reminder
191+
await client.actor.reminderDelete(DemoActorImpl.name, `actor-id`, `timer-id`);
192+
}
164193
```
165194

166195
- For a full guide on actors visit [How-To: Use virtual actors in Dapr]({{< ref howto-actors.md >}}).

0 commit comments

Comments
 (0)