Stateful Actors

One of the major advantages of an actor system is that it offers a safe way of creating stateful services. A stateful actor is created using the spawn function.

In this example, the state is initialized to an empty object using state = {}. Each time a message is received by the actor, the current state is passed in as the first argument to the actor. Whenever the actor encounters a name it hasn't encountered yet, it returns a copy of previous state with the name added. If it has already encountered the name it simply returns the unchanged current state. The return value is used as the next state.

const statefulGreeter = spawn(
  (state = {}, msg, ctx) => {
    const hasPreviouslyGreetedMe = state[] !== undefined;
    if(hasPreviouslyGreetedMe) {
      console.log(`Hello again ${}.`);  
      return state;
    } else {
        `Good to meet you, ${}.\nI am the ${} service!`
      return { ...state, []: true };

If no state is returned or the state returned is undefined or null, stateful actors automatically shut down.