Arizona's event system connects the JavaScript client with server-side Erlang processes via WebSocket. Every user interaction follows a predictable path through the framework:
- The user interacts with the page (click, input, form submission, etc.).
- The JavaScript client sends an event message over the WebSocket connection.
- The
arizona_liveGenServer receives the event on the server. - The appropriate
handle_event/3callback is invoked -- either on the view (page-level) or on a specific stateful component, depending on how the event was sent. - The callback returns
{Actions, UpdatedState}, whereActionsis a list of instructions for the client andUpdatedStateis the new view or component state. - Arizona computes a diff of the changed template parts by comparing the previous and current bindings.
- The diff and any actions are sent back to the client over the WebSocket.
- The client patches the DOM via morphdom and processes the returned actions (dispatches, replies, redirects, etc.).
Events can also flow from server to client through PubSub. When a broadcast
message arrives, the view's handle_event/3 callback is invoked with the topic
as the event name. State changes made there trigger the same diff-and-patch
cycle described above. This means
a single user's action can update every connected client in real time without
any additional client-side code.
Client Server
| |
|-- pushEvent("inc", %{}) ----->|
| |-- handle_event/3
| |-- compute diff
|<----- diff + actions ---------|
| |
|-- patch DOM |
|-- process actions |
Arizona provides four categories of events. Each serves a different communication pattern between the client and the server.
| Type | Direction | API | Use Case |
|---|---|---|---|
| WebSocket | Client -> Server | pushEvent |
User interactions |
| Call | Client -> Server -> Client | callEvent |
Request-reply patterns |
| PubSub | Server -> Server -> Client | broadcast |
Real-time updates |
| Client | Server -> Client (JS) | arizona.on |
JS-side reactions |
WebSocket Events are the most common type. The client sends an event name and optional parameters; the server handles it and returns state changes.
Call Events extend WebSocket events with a request-reply pattern. The client receives a Promise that resolves when the server explicitly replies.
PubSub enables server-to-server-to-client communication. A broadcast message reaches every subscribed view process, which can then update its state and push changes to the connected client.
Client Events are JavaScript-side events emitted by the Arizona client library. They allow your JS code to react to framework lifecycle events (connection status, DOM patches) and custom events dispatched from the server.
See each dedicated guide for full details: