Your first Actyx Pond program.
The main programming unit in Actyx Pond is a Fish. A fish is like a living entity: It "feeds on" little pieces of information – events – produced by connected ActyxOS nodes. From the events, it builds its state. So when a new event is produced somewhere, the state updates. In your application, you can observe a fish’s state meaning you get real-time updates!
Let’s start with a very simple fish.
onEvent handler we provide takes the existing state and an event, in order to produce an
updated state. It’s like
Array.reduce. In this case, we just set the state to the latest event we
have seen! Of course, this could be much more elaborate.
initialState defines what state we are in before having seen any events. Fittingly, we just
send out a general greeting: Hello, world!
Finally, we're providing a
where clause describing which events this fish is interested in. By
allEvents we do in fact select all events created by the application; we might also pass
noEvents to process no events and always stay at the initial state. But of course, there is
rich functionality for selecting only some events. We will get to that later.
For now, let's see how the friendly fish can give us a warm greeting:
Try it out!
You should see our initial greeting logged to the console – unless your swarm already has some events, in which case you will see the latest one!
To bring a fish to life we need to create a pond in which it can breathe.
Once the pond is created, we can ask the pond to observe our
helloWorldFish; the second argument is a callback, which
is called with every state update of the fish.
In this section we have seen how to create the most basic Actyx Pond program. ActyxOS is all about event streams, so in the next section we explore how events are emitted and thus populate those event streams.