Skip to content

Stand-alone snippets

When you want to see one concept in isolation, the stand-alone snippets are the right place. Each is a single file demonstrating one pattern — no Docker, no peers, runnable with bun.

Find them under examples/snippets/ in the repo.

SnippetConcept
01-counter.tsThe minimal Actor — extends Actor, handles two message kinds.
02-ask-pattern.tsRequest/response via ask.
03-supervision.tsCustom strategy with decideBy.
04-become-state-machine.tscontext.become(...) for state transitions.
05-event-stream.tsSystem-wide pub/sub.
06-timers.tscontext.timers for scheduled work.
07-receive-timeout.tsIdle detection.
08-typed-counter.tsThe same counter via Behaviors.*.
09-routing.tsLocal pool router.
10-circuit-breaker.tsCircuitBreaker wrapping an HTTP call.
11-backoff-supervisor.tsExponential-backoff restart.
12-persistent-actor.tsEvent-sourced bank account.
13-durable-state.tsSnapshot-style persistence.
14-projection.tsRead-side view via ProjectionActor.
15-cluster-singleton.tsSingleton with manager + proxy.
16-cluster-sharding.tsPer-key sharded entities.
17-pubsub.tsCluster-wide DistributedPubSub.
18-distributed-data.tsGCounter + ORSet over the cluster.
19-saga.tsMulti-step PersistentFSM.
20-management-http.tsHealth checks + /metrics.

Each snippet is 50-200 lines. Read in a few minutes; modify to play with the concept.

Terminal window
git clone https://github.com/pathosDev/actor-ts.git
cd actor-ts/examples/snippets
bun install
bun 01-counter.ts

Each snippet runs to completion and exits. No long-running processes; no external dependencies.

For cluster-related snippets (singleton, sharding, pubsub, DD), the snippets use MultiNodeSpec to simulate a cluster inside one process — no Docker required.

Most snippets follow the same shape:

import { ActorSystem, ... } from 'actor-ts';
// 1. Set up the system + actors.
const system = ActorSystem.create('demo');
const ref = system.actorOf(Props.create(() => new MyActor()));
// 2. Send some messages.
ref.tell({ ... });
ref.tell({ ... });
// 3. Wait for processing (a small await or expect on a probe).
await new Promise(r => setTimeout(r, 100));
// 4. Print result + clean up.
console.log(...);
await system.terminate();

For more complex flows (cluster, persistence), the snippets use TestKit + TestProbe for clean teardown.

Snippets are the easiest place to experiment:

Terminal window
# Copy, edit, run:
cp 01-counter.ts my-experiment.ts
# Edit my-experiment.ts
bun my-experiment.ts

The framework is set up via the local repo’s actor-ts package — your edits run against the live source.

If you’ve worked out a clever pattern, PR a snippet. Criteria:

  • One file, ≤ 200 lines.
  • One concept demonstrated clearly.
  • Self-contained — runnable without external services.
  • Comments explaining what the code shows.

Mark your snippet’s number from the next available slot in the existing sequence; update the README in snippets/.