Actors

    For a more in-depth overview of Dapr actors, visit the actors overview page.

    Scenario

    The below code examples loosely describe the scenario of a Parking Garage Spot Monitoring System, which can be seen in this by Mark Russinovich.

    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.

    To jump in and run this example yourself, clone the source code, which can be found in the JavaScript SDK examples directory.

    Actor Implementation

    An actor implementation defines a class by extending the base type AbstractActor and implementing the actor interface (ParkingSensorInterface in this case).

    The following code describes an actor implementation along with a few helper methods.

    1. import { AbstractActor } from "@dapr/dapr";
    2. import ParkingSensorInterface from "./ParkingSensorInterface";
    3. export default class ParkingSensorImpl extends AbstractActor implements ParkingSensorInterface {
    4. async carEnter(): Promise<void> {
    5. // Implementation that updates state that this parking spaces is occupied.
    6. }
    7. async carLeave(): Promise<void> {
    8. // Implementation that updates state that this parking spaces is available.
    9. }
    10. private async getInfo(): Promise<object> {
    11. // Implementation of requesting an update from the parking space sensor.
    12. }
    13. /**
    14. * @override
    15. */
    16. async onActivate(): Promise<void> {
    17. // Initialization logic called by AbstractActor.
    18. }
    19. }

    To configure actor runtime, use the DaprClientOptions. The various parameters and their default values are documented at How-to: Use virtual actors in Dapr.

    Note, the timeouts and intervals should be formatted as strings.

    1. import { CommunicationProtocolEnum, DaprClient, DaprServer } from "@dapr/dapr";
    2. // Configure the actor runtime with the DaprClientOptions.
    3. const clientOptions = {
    4. actor: {
    5. actorScanInterval: "30s",
    6. drainOngoingCallTimeout: "1m",
    7. drainRebalancedActors: true,
    8. reentrancy: {
    9. enabled: true,
    10. maxStackDepth: 32,
    11. },
    12. },
    13. };
    14. // Use the options when creating DaprServer and DaprClient.
    15. // Note, DaprServer creates a DaprClient internally, which needs to be configured with clientOptions.
    16. const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, clientOptions);
    17. const client = new DaprClient(daprHost, daprPort, CommunicationProtocolEnum.HTTP, clientOptions);

    Invoking Actor Methods

    After Actors are registered, create a Proxy object that implements ParkingSensorInterface using the ActorProxyBuilder. You can invoke the actor methods by directly calling methods on the Proxy object. Internally, it translates to making a network call to the Actor API and fetches the result back.

    1. import { ActorId, DaprClient } from "@dapr/dapr";
    2. import ParkingSensorImpl from "./ParkingSensorImpl";
    3. import ParkingSensorInterface from "./ParkingSensorInterface";
    4. const daprHost = "127.0.0.1";
    5. const daprPort = "50000";
    6. const client = new DaprClient(daprHost, daprPort);
    7. // Create a new actor builder. It can be used to create multiple actors of a type.
    8. const builder = new ActorProxyBuilder<ParkingSensorInterface>(ParkingSensorImpl, client);
    9. // Create a new actor instance.
    10. const actor = builder.build(new ActorId("my-actor"));
    11. // Or alternatively, use a random ID
    12. // const actor = builder.build(ActorId.createRandomId());
    13. // Invoke the method.
    14. await actor.carEnter();
    1. import { AbstractActor } from "@dapr/dapr";
    2. import ActorStateInterface from "./ActorStateInterface";
    3. export default class ActorStateExample extends AbstractActor implements ActorStateInterface {
    4. async setState(key: string, value: any): Promise<void> {
    5. await this.getStateManager().setState(key, value);
    6. await this.getStateManager().saveState();
    7. await this.getStateManager().removeState(key);
    8. await this.getStateManager().saveState();
    9. }
    10. // getState with a specific type
    11. async getState<T>(key: string): Promise<T | null> {
    12. return await this.getStateManager<T>().getState(key);
    13. }
    14. // getState without type as `any`
    15. async getState(key: string): Promise<any> {
    16. return await this.getStateManager().getState(key);
    17. }
    18. }

    Actor Timers and Reminders

    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 does not retain any information about timers after deactivation, but persists reminders information using the Dapr actor state provider.

    This distinction allows users to trade off between light-weight but stateless timers versus more resource-demanding but stateful reminders.

    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.

    1. // ...
    2. const actor = builder.build(new ActorId("my-actor"));
    3. // Register a reminder, it has a default callback: `receiveReminder`
    4. await actor.registerActorReminder(
    5. "reminder-id", // Unique name of the reminder.
    6. Temporal.Duration.from({ seconds: 2 }), // DueTime
    7. Temporal.Duration.from({ seconds: 1 }), // Period
    8. Temporal.Duration.from({ seconds: 1 }), // TTL
    9. 100, // State to be sent to reminder callback.
    10. );
    11. // Delete the reminder
    12. await actor.unregisterActorReminder("reminder-id");
    1. export default class ParkingSensorImpl extends AbstractActor implements ParkingSensorInterface {
    2. // ...
    3. /**
    4. * @override
    5. */
    6. async receiveReminder(state: any): Promise<void> {
    7. // handle stuff here
    8. }
    9. }

    For a full guide on actors, visit .