|
1 | 1 | ---
|
2 |
| -title: The actor model |
| 2 | +title: The Actor model |
3 | 3 | ---
|
4 | 4 |
|
5 |
| -When you run a state machine, it becomes an actor. In the actor model, actors are “live” objects that can communicate with each other via asynchronous messages. In XState, we refer to these messages as [**events**](transitions.mdx). |
| 5 | +The [Actor model](https://en.wikipedia.org/wiki/Actor_model) in computer science is a mathematical model of concurrent computation in which an "actor" is the basic building block. |
6 | 6 |
|
7 |
| -Coming soon… what are some good examples of actors in software development? |
| 7 | +The actor model allows developers to build reliable message-based systems by using actors to communicate. State machines and statecharts can model the logic of actors. These actors can communicate with each other, and with other actors, in the same way. |
| 8 | + |
| 9 | +:::tip |
| 10 | +When you run a state machine in XState, it becomes an actor. |
| 11 | +::: |
| 12 | + |
| 13 | +### What Defines An "Actor"? |
| 14 | + |
| 15 | +Actors are independent "live" objects that can communicate with each other via asynchronous message passing. In XState, we refer to these messages as [_events_](transitions.mdx). |
| 16 | + |
| 17 | +- An actor has its own internal, encapsulated state that can only be updated by the actor itself. An actor may choose to update its internal state in response to a message it receives, but it cannot be updated by any other entity. |
| 18 | +- Actors communicate with other actors by sending and receiving events asynchronously. |
| 19 | +- Actors process one message at a time. They have an internal “mailbox” that acts like an event queue, processing events sequentially. |
| 20 | +- Internal actor state is not shared between actors. The only way for an actor to share any part of its internal state is by: |
| 21 | + - Sending events to other actors |
| 22 | + - Or emitting snapshots, which can be considered implicit events sent to subscribers. |
| 23 | +- Actors can create (spawn/invoke) new actors. |
| 24 | + |
| 25 | +You'll find strong similarities to the Actor model in software you may already be familiar with. The concept of objects encapsulating state and passing messages to each other may be familiar from Object-Oriented Programming. And actors are analagous to real-world physical concepts like cell biology, and communication in human relationships. |
8 | 26 |
|
9 | 27 | ## State
|
10 | 28 |
|
11 |
| -An actor has its own internal, encapsulated state that the actor itself can only update. An actor may update its internal state in response to a message it receives, but it cannot be updated by any other entity. Actors do not share state. The only way for an actor to share data is by sending events. |
| 29 | +An actor has its own internal, encapsulated state that only the actor itself can update. An actor may update its internal state in response to a message it receives, but it cannot be updated by any other entity. Actors do not share state. The only way for an actor to share data is by sending events. |
12 | 30 |
|
13 |
| -[Read more about actors and state](actors.mdx). |
| 31 | +[Read more about XState actors and state](actors.mdx). |
14 | 32 |
|
15 |
| -## Communication with events |
| 33 | +## Communication With Events |
16 | 34 |
|
17 | 35 | Actors communicate with other actors by sending and receiving events asynchronously. Actors use an internal “mailbox” that acts like an event queue, processing events one at a time.
|
18 | 36 |
|
19 |
| -[Read more about actors and events](actors.mdx). |
| 37 | +[Read more about XState events and transitions](transitions.mdx). |
20 | 38 |
|
21 | 39 | ## Spawning
|
22 | 40 |
|
23 |
| -Actors can spawn new actors. Actors will spawn new actors in situations where they need to delegate work to another actor or when they need to create a new actor to handle a new task. Spawning allows for a flexible and dynamic system where actors can be created and destroyed as needed to handle the workload efficiently. |
| 41 | +Actors can spawn new actors, which is useful in situations where an actor needs to delegate work to another actor. Spawning allows for a flexible and dynamic system where actors can be created and destroyed as needed to handle the workload efficiently. |
| 42 | + |
| 43 | +- [Read more about spawning actors in XState](spawn.mdx). |
| 44 | +- [Read about the difference between invoking and spawning actors in XState](actors.mdx#invoking-and-spawning-actors). |
| 45 | + |
| 46 | +## The Actor Model in Backend Development |
24 | 47 |
|
25 |
| -- [Read more about spawning actors](spawn.mdx). |
26 |
| -- [Read about the difference between invoking and spawning actors](actors.mdx#invoking-and-spawning-actors). |
| 48 | +The actor model is often used to coordinate backend systems. There are direct implementations of the Actor model, like [Akka](https://doc.akka.io/docs/akka/current/typed/guide/introduction.html) for the JVM. In [Erlang](https://www.erlang.org/docs), processes can be seen as actors, which can send and receive messages and spawn new processes. Erlang is used by massive distributed systems, like Discord and WhatsApp. |
27 | 49 |
|
28 |
| -## Backend development |
| 50 | +In [Stately Sky](https://stately.ai/docs/stately-sky-getting-started), a state machine actor be used to manage long-running backend processes like medical patient onboarding flows, inventory management, or multi-player collaborative experiences like whiteboard canvases or games. |
29 | 51 |
|
30 |
| -Coming soon… how do you use the actor model in backend dev? |
| 52 | +## The Actor Model in Frontend Development |
31 | 53 |
|
32 |
| -## Frontend development |
| 54 | +The Actor model is especially useful for coordinating the many moving parts of a front-end web application. |
33 | 55 |
|
34 |
| -Coming soon… how do you use the actor model in frontend dev? |
| 56 | +**Your front-end app is always a distributed system**, and managing distributed systems is where the actor model shines. This is because in a browser environment **you never really have a "global source of truth"**, you instead have **many independent sources of state and events**: 3rd-party components, local component state, local storage, query parameters, routers, network I/O, DOM events and their listeners, etc. |
| 57 | + |
| 58 | +> […] there is no such thing as a single source of truth in any non-trivial application. All applications, even front-end apps, are distributed at some level. – via: [Redux is Half of a Pattern (2/2)](https://dev.to/davidkpiano/redux-is-half-of-a-pattern-2-2-4jo3) |
| 59 | +
|
| 60 | +So even for simple web apps, with small app-specific state and a few known app-specific events, the actor model can be helpful. |
35 | 61 |
|
36 | 62 | ## XState
|
37 | 63 |
|
38 |
| -Coming soon… how does the actor model work in XState? |
| 64 | +Actors in XState can: |
| 65 | +- **Accept messages** as [events](transitions#event-objects) passed to their own internal logic, or for state machines as received by [transitions](transitions.mdx). |
| 66 | +- **Create more actors** within a state machine using `spawn` in an [`assign`](actions.mdx#assign-action), or using the `spawnChild` action creator. For details, see [Spawn](spawn.mdx). |
| 67 | +- **Send more messages** as events using `self.send` in their own logic, or [action creators](actions.mdx) like [`sendTo`](actions#send-to-action) or [`raise`](actions#raise-action) in a state machine. |
| 68 | + |
| 69 | +Actors in XState have their own [actor logic](actors.mdx#actor-logic) which they use to: |
| 70 | +- **Make local decisions** |
| 71 | +- **Determine how to repond to the next message received** |
| 72 | +- **Modify their own private state** (but only affect each other via messaging) |
| 73 | + |
| 74 | +Actors in XState exist in [systems](system.mdx) and can communicate with each other within and across those systems. |
| 75 | + |
| 76 | +## Reference |
| 77 | + |
| 78 | +- [What is the actor model and when should I use it?](https://stately.ai/blog/what-is-the-actor-model) |
| 79 | +- [The Actor Model Explained in 5 Minutes](https://www.youtube.com/watch?v=ELwEdb_pD0k) |
| 80 | +- [Wikipedia: Actor model](https://en.wikipedia.org/wiki/Actor_model) |
0 commit comments