Hyperapp Framework
How it feels to use hyperware
Table of Contents
- Part 1: User Guide
- Part 2: Technical Implementation
Part 1: User Guide
Overview
This is a process framework abstracting away most of the boilerplate for developing hyperware processes. It unlocks async support by implementing a custom async runtime, and in conjunction with hyper-bindgen, it allows the automatic generation of wit files from defined function endpoints, as well as functions stubs in caller-utils in order to be able to have a process asynchronously call another endpoint in another process as if it were a function.
RPC style, but for WASI.
So this includes:
- Defining functions as endpoints (http, remote, local, ws and init)
- Async support
- Automated state persistence with different options
Getting Started
To create a Hyperware process, you need to:
- Define your process state as a struct
- Implement the struct with the
hyperappmacro - Define handlers for different types of requests
Here's a minimal example:
State Management
Your state should implement the Default and State traits, and be serializable with serde.
Hyperprocess Macro Parameters
The hyperapp macro accepts the following parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
name |
String | Yes | Human-readable name of your process |
icon |
String | No | Icon to display in UI |
widget |
String | No | Widget type to display in UI |
ui |
Option<HttpBindingConfig> | Yes | UI configuration |
endpoints |
Vec<Binding> | Yes | HTTP and WebSocket endpoints |
save_config |
SaveOptions | Yes | When to persist state |
wit_world |
String | Yes | WIT world name for component model |
Example:
Handler Types
Hyperware processes can handle four types of requests, specified by attributes:
| Attribute | Description |
|---|---|
#[local] |
Handles local (same-node) requests |
#[remote] |
Handles remote (cross-node) requests |
#[http] |
Handles ALL HTTP requests (GET, POST, PUT, DELETE, etc.) |
#[eth] |
Handles Ethereum subscription updates from your RPC provider |
These attributes can be combined to make a handler respond to multiple request types:
async
The function arguments and the return values have to be serializable with Serde.
HTTP Method Support and Smart Routing
The #[http] attribute supports intelligent routing with priority-based matching:
// Specific path handlers (highest priority)
async
// Dynamic method-only handlers (medium priority)
async
// Ultimate fallback (lowest priority)
Supported Methods: GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS
Smart Routing System
The framework uses intelligent priority-based routing that automatically chooses the best handler based on the request:
Priority Logic:
-
Has Request Body → Tries parameterized handlers first
- Deserializes body to determine the correct handler
- Falls back to parameter-less handlers if deserialization fails
-
No Request Body → Tries parameter-less handlers first
- Routes based on path and method matching
- Never attempts body deserialization for performance
Two-Phase Matching:
Phase 1: Direct Path/Method Matching
// These are matched instantly without body parsing
Phase 2: Body-Based Handler Discovery
// These are matched by deserializing the request body
async
async
Context Preservation in Async Handlers:
The framework automatically preserves request context in async handlers:
async
Routing Priority Examples:
// Request: POST /api/upload (with body)
// 1. ✅ Tries: create_item handler if body matches {"CreateItem": ...}
// 2. ✅ Tries: update_settings handler if body matches {"UpdateSettings": ...}
// 3. ✅ Falls back to: handle_post_with_data for unmatched bodies
// 4. ✅ Ultimate fallback: handle_any_method
// Request: GET /api/users (no body)
// 1. ✅ Tries: list_users (exact path match)
// 2. ✅ Falls back to: handle_get_fallback (method-only match)
// 3. ✅ Ultimate fallback: handle_any_method
Path-Specific Routing
You can bind HTTP handlers to specific paths using the path parameter:
Path Binding: When you specify a path, the handler will ONLY respond to requests for that exact path.
Routing Priority:
- Parameter-less handlers with exact path and method match
- Parameter-less handlers with exact path (any method)
- Handlers with parameters matched by request body deserialization
- Error responses (404 Not Found or 405 Method Not Allowed)
Compile-Time Validations:
- All handler names must be unique when converted to CamelCase (e.g.,
get_userandget_userconflict) - Init methods must be async and take only
&mut self - WebSocket methods must have exactly 3 parameters:
channel_id: u32,message_type: WsMessageType,blob: LazyLoadBlob - ETH handlers must take exactly 1 parameter:
eth_sub_result: EthSubResult - Only one ETH handler is allowed per hyperprocess
- At least one handler must be defined (
#[http],#[local],#[remote], or#[eth]) - The macro provides comprehensive error messages with debugging tips for all validation failures
Current Limitations:
- All requests with parameters expect JSON request bodies in the format
{"HandlerName": parameter_value} - No automatic query parameter binding (use
get_query_params()to access them manually) - WebSocket path routing requires manual checking with
get_path()in the WebSocket handler
Special Methods
Init Method
To run code on process startup, define:
async
WebSocket Handler
For defining a ws endpoint (server-side WebSocket), do:
// Synchronous WebSocket handler
// Asynchronous WebSocket handler
async
Both sync and async variants are supported. If you have multiple ws endpoints, you can match on the ws endpoints with get_path(), which will give you an Option<String>.
If you want to access the http server, you can call get_server(), giving you access to HttpServer.
WebSocket Client Handler
For handling WebSocket client connections (when your process acts as a WebSocket client), use:
// Synchronous WebSocket client handler
// Asynchronous WebSocket client handler
async
Both sync and async variants are supported. This handler receives messages from WebSocket servers that your process has connected to using the http-client:distro:sys service.
The signature matches that of #[ws] for consistency.
ETH Handler
For handling Ethereum subscription updates from the eth:distro:sys service, use:
// Synchronous ETH handler with resubscription
// Asynchronous ETH handler with resubscription
async
Important Notes:
- Only one ETH handler is allowed per hyperprocess
- The handler must take exactly one parameter:
eth_sub_result: EthSubResult - Both sync and async variants are supported
- The handler receives subscription updates and errors from the ETH module
EthSubResultis aResult<EthSub, EthSubError>type where:EthSubcontains subscription updates withid: u64andresult: serde_json::ValueEthSubErrorcontains subscription errors withid: u64anderror: String
- Resubscription Pattern: Always unsubscribe first, then resubscribe with current state
Binding Endpoints
The endpoints parameter configures HTTP and WebSocket endpoints:
endpoints = vec!
Persistence Options
The save_config parameter controls when to persist state:
save_config = EveryMessage
Available options:
| Option | Description |
|---|---|
SaveOptions::Never |
Never persist state |
SaveOptions::EveryMessage |
Persist after every message |
SaveOptions::EveryNMessage(n) |
Persist every n messages |
SaveOptions::EveryNSeconds(n) |
Persist every n seconds |
Example Application
If you want to call a function from another process, you run hyper-bindgen (see hyper-bindgen repo), which will automatically generate wit files in /api, and a caller-utils rust project. If you import it like so, you'll be able to call any endpoint as an async function!
use increment_counter_remote_rpc;
use receiver_address;
async
Query Parameters
For GET requests, query parameters can be accessed using the get_query_params() helper function from hyperware_app_common:
use get_query_params;
Example: Query Parameter Parsing
For a request to /api/search?q=rust&limit=20&sort=date:
Note: All query parameter values are strings, so you need to parse them to other types as needed.
Error Handling and Debugging
The framework provides comprehensive error handling and debugging capabilities:
Comprehensive Logging
The macro generates detailed logging for all operations:
// Automatically generated logs help track request flow:
// Phase 1: Checking parameter-less handlers for path: '/api/users', method: 'GET'
// Successfully parsed HTTP path: '/api/users'
// Set current_path to: Some("/api/users")
// Set current_http_method to: Some("GET")
Request Body Parsing Errors:
// Wrong handler name
Invalid request format. Expected one of the parameterized handler formats,
but got: {"WrongHandler":{"message":"test"}}
// Invalid JSON syntax
Invalid JSON in request body. Expected: {"CreateUser":[ ...parameters... ]}.
Parse error: expected value at line 1 column 1
// Empty body for parameterized handler
Request body is empty. This handler expects a JSON object with the handler name and parameters.
Handler-Specific Errors:
// If a parameterized handler expects a body but doesn't get one:
POST /api/users → "Handler CreateUser requires a request body"
// If no handler matches the method + path combination:
PUT /nonexistent → "No handler found for PUT /nonexistent"
Development Tips:
Failed to deserialize HTTP request into HPMRequest enum.
Error: missing field `CreateUser` at line 1 column 15
Path: /api/users
Method: POST
Body received:
{
"createuser": "john_doe" // ❌ Wrong case! Should be "CreateUser"
}
Debugging tips:
- Handler names are converted to CamelCase: create_user → CreateUser
- JSON keys are case-sensitive: use exact CamelCase handler names
- For handlers with parameters, use format {"HandlerName": parameter_value}
- For parameter-less handlers, use get_path() and get_http_method() for routing
Compile-Time Validation
The macro catches configuration errors at compile time:
// This will fail to compile:
Context Access Helpers
Use these functions to access request context within handlers:
| Function | Returns | Description |
|---|---|---|
get_path() |
Option<String> |
Current HTTP path |
get_http_method() |
Option<String> |
Current HTTP method |
get_query_params() |
Option<HashMap<String, String>> |
Query parameters |
source() |
Address |
Address of the message sender |
⚠️ Known Limitations and Gotchas
Request Body Format Requirements:
// ❌ This won't work - body expects specific JSON format
POST /api/users
Content-Type: application/json
// ✅ This works - body must wrap parameters in handler name
POST /api/users
Content-Type: application/json
Why: The framework uses the outer JSON key to determine which handler to invoke. This enables multiple handlers to respond to the same HTTP method + path combination.
Handler Name Case Sensitivity:
async
// ❌ Won't match - wrong case
// ✅ Will match - exact CamelCase
Solution: Always use exact CamelCase conversion: snake_case → CamelCase
Async Context Limitations:
async
Why: The framework preserves context by capturing it before async execution, but very long-running tasks might exceed context lifetime.
Query Parameter Encoding:
// URL: /api/search?q=hello%20world&tags=rust,web
let params = get_query_params.unwrap;
// ❌ Raw values - URL encoding not automatically decoded
assert_eq!; // Still encoded
// ✅ Manual decoding needed for special characters
let query = params.get
.map
.unwrap_or_default; // "hello world"
Solution: Use a URL decoding library for complex query parameters.
No Built-in Content Negotiation:
Workaround: Use parameter-less handlers and manually parse request bodies:
Single Handler Per Variant:
// ❌ This won't compile - duplicate handler names become same variant
// ERROR: Duplicate CreateUser variant
Solution: Use different method names or combine logic:
// Or combine with method checking:
Performance Considerations:
- Body parsing overhead: Every parameterized request requires JSON deserialization
- Context preservation: Async handlers have slight overhead from context capture/restore
- Priority matching: Requests with bodies try parameterized handlers first, which may be slower
Optimization tips:
- Use parameter-less handlers for high-frequency endpoints (health checks, metrics)
- Use specific paths instead of catch-all handlers when possible
- Batch multiple operations into single handlers when possible
- Consider caching for expensive operations within handlers
Two-Phase HTTP Routing (Updated!)
The routing system now uses intelligent request-body-aware routing:
Smart Phase Selection:
For requests WITH body data:
- Phase 1: Body Deserialization - Tries to deserialize body to find matching parameterized handler
- Phase 2: Parameter-less Fallback - Falls back to parameter-less handlers if deserialization fails
For requests WITHOUT body data:
- Phase 1: Direct Matching - Matches parameter-less handlers by path and HTTP method
- Phase 2: Not Used - No body parsing attempted (performance optimization)
Routing Flow Examples:
// High-priority specific handlers
async
// Medium-priority dynamic handlers
async
// Low-priority catch-all
Request: GET /health (no body)
- ✅ Matches
health_checkdirectly (exact path + method) - ❌ No body parsing attempted
Request: POST /api/users with body {"CreateSpecificUser": {...}}
- ✅ Matches
create_specific_user(path + method + body deserialization) - ❌ No fallback needed
Request: POST /api/items with body {"CreateGeneral": {...}}
- ❌ No exact path match for
/api/items - ✅ Deserializes body → matches
create_general(method + body)
Request: PUT /anything (no body)
- ❌ No parameter-less handler for
PUT /anything - ✅ Matches
catch_allhandler
This intelligent routing ensures optimal performance by avoiding unnecessary body parsing for requests without bodies, while providing maximum flexibility for complex routing scenarios.
Common Patterns and Best Practices
Parameter-less vs Parameter-based Handlers
Choose the right handler type for your use case:
// ✅ Good: Parameter-less handler for simple endpoints
// ✅ Good: Parameter-less handler with dynamic routing
// ✅ Good: Parameter-based handler for complex data
async
Error Handling Patterns
// ✅ Good: Use Result types for handlers that can fail
// ✅ Good: Use custom error types for better error handling
State Management Best Practices
Async Handler Patterns
async
Part 2: Technical Implementation
Architecture Overview
Barebones hyperware processes use erlang style message passing. When wanting to send messages asynchronously, you had to send off a request, but handle the response in the response handler residing in another part of the code, adding a context switching cost. Being able to call things asynchronously makes things much more linear and easier to read and process, both for humans and LMs.
This was achieved by implementing our own async runtime. Given that processes are always single threaded, and the only real event occurs is when a message (either a request or response) is read with await_message(), we managed to implement a runtime through callbacks and other tricks.
Macro Implementation
The hyperapp macro transforms a struct implementation into a fully-featured process:
1. Parsing Phase
The macro will parse arguments like so:
It also checks the method signatures:
2. Metadata Collection
It then builds metadata:
3. Code Generation
Under the hood, everything is still regular hyperware message passing, with the body being either a Request or Response enum. Whenever you define a new function/endpoint, it generates appropriate request and response variants, with the name of the function being the variant in CamelCase.
The inner values of the request variants will be the function arguments as tuples, the inner valus of the response variants will be return value of the defined function.
Request/Response Flow
The flow of a request through the system:
- Message arrives (HTTP, local, or remote)
- For HTTP requests:
- First attempts to match parameter-less handlers by path and method
- If no match, attempts to deserialize body and match handlers with parameters
- For local/remote requests:
- Deserializes body into Request enum
- Appropriate handler is dispatched based on message type
- For async handlers, the future is spawned on the executor
- When handler completes, response is serialized and sent back
- For async handlers, awaiting futures are resumed with the response
HTTP Request Routing Details
The HTTP routing system uses a two-phase approach:
Phase 1: Parameter-less Handler Matching
- Checks incoming path and HTTP method against parameter-less handlers
- These handlers are matched directly without body deserialization
- Useful for GET endpoints, health checks, and other body-less requests
Phase 2: Body-Based Handler Matching
- Only triggered if no parameter-less handler matches
- Deserializes request body to determine which handler to invoke
- Matches handlers that expect parameters
This design allows clean APIs for simple endpoints while maintaining flexibility for complex requests:
// Phase 1: Matched by path/method
// Phase 2: Matched by body deserialization
Async Runtime
Here is how the async runtime works on a high level.
ResponseFuture Implementation
Core type that suspends execution until a response arrives:
Correlation System
The correlation system works by generating a unique correlation ID (UUID) for each request and attaching it to outgoing requests in the context field. Responses are stored in RESPONSE_REGISTRY keyed by their correlation ID. The ResponseFuture polls RESPONSE_REGISTRY until the matching response arrives.
This design enables async handlers to suspend execution while waiting for responses, with multiple requests able to be in flight simultaneously. When responses come back, they can be correctly routed to the handler that is awaiting them based on the correlation ID. The system also supports timeouts by tracking how long responses have been pending.
The implementation uses thread locals to avoid synchronization overhead, since the process runs in a single-threaded environment. This lock-free approach keeps the correlation system lightweight and efficient while maintaining the ability to track request/response pairs reliably.
pub async
Executor and Task Management
The executor manages async tasks within the single-threaded environment:
The executor is polled in the main event loop, right before we await a message. So if a response comes back, we make sure that everything is properly 'linked'.
loop
Handler Generation
The macro generates specialized code for each handler method.
Request and Response Enum Generation
The macro extracts parameter and return types from each method:
For example, given these handlers:
async
The macro generates these enums:
WIT Bindings Generation
We parse the wit_world in our /api folder with:
generate!;
Note: The wit files will always get generated with hyper-bindgen, which you have to call before kit b. More info in the hyper-bindgen repository.
;
Here is an overly complex llm generated graph that looks cool.
graph TB
%% STYLE DEFINITIONS
classDef default fill:#333333,color:#ffffff,stroke:#444444,stroke-width:1px
classDef accent fill:#FF6600,color:#ffffff,stroke:#CC5500,stroke-width:2px
classDef mainflow fill:#444444,color:#ffffff,stroke:#666666,stroke-width:2px
classDef asyncflow fill:#2A2A2A,color:#ffffff,stroke:#444444,stroke-width:1px
classDef external fill:#222222,color:#ffffff,stroke:#444444,stroke-width:1px
classDef dataflow fill:#008CBA,color:#ffffff,stroke:#0077A3,stroke-width:1px
classDef annotation fill:none,color:#FF6600,stroke:none,stroke-width:0px
%% BUILD PHASE - Where components are generated
subgraph BuildPhase["⚙️ BUILD PHASE"]
UserSrc[/"User Source Code
#[hyperapp] macro
#[http], #[local], #[remote] methods"/]
subgraph CodeGen["Code Generation Pipeline"]
direction TB
HyperBindgen["hyper-bindgen CLI
Scans for #[hyperapp]"]
subgraph BindgenOutputs["hyper-bindgen Outputs"]
direction LR
WitFiles["WIT Files
WebAssembly Interfaces"]
CallerUtils["caller-utils Crate
Type-safe RPC Stubs"]
EnumStructs["Shared Enums & Structs
Cross-process types"]
end
ProcMacro["hyperapp Macro
AST Transformation"]
subgraph MacroOutputs["Macro Generated Code"]
direction LR
ReqResEnums["Request/Response Enums
- Generated variants per handler
- Parameter & return mappings"]
HandlerDisp["Handler Dispatch Logic
- HTTP/Local/Remote routing
- Async handler spawning
- Message serialization"]
AsyncRuntime["Async Runtime Components
- ResponseFuture impl
- Correlation ID system
- Executor & task management"]
MainLoop["Component Implementation
- Message loop
- Task polling
- Error handling"]
end
end
%% Dev-time Connections
UserSrc --> HyperBindgen
UserSrc --> ProcMacro
HyperBindgen --> BindgenOutputs
ProcMacro --> MacroOutputs
%% Final Compilation
MacroOutputs --> WasmComp["WebAssembly Component
WASI Preview 2"]
BindgenOutputs --> WasmComp
end
%% RUNTIME PHASE - How processes execute
subgraph RuntimePhase["⚡ RUNTIME PHASE"]
subgraph Process["Process A"]
direction TB
InMsg[/"Incoming Messages"/] --> MsgLoop["Message Loop
await_message()"]
subgraph ProcessInternals["Process Internals"]
direction LR
MsgLoop --> MsgRouter{"Message Router"}
MsgRouter -->|"HTTP"| HttpHandler["HTTP Handlers"]
MsgRouter -->|"Local"| LocalHandler["Local Handlers"]
MsgRouter -->|"Remote"| RemoteHandler["Remote Handlers"]
MsgRouter -->|"WebSocket"| WsHandler["WebSocket Handlers"]
MsgRouter -->|"Response"| RespHandler["Response Handler"]
%% State management
HttpHandler & LocalHandler & RemoteHandler & WsHandler --> AppState[("Application State
SaveOptions::EveryMessage")]
%% Async handling
RespHandler --> RespRegistry["Response Registry
correlation_id → response"]
CallStub["RPC Stub Calls
e.g. increment_counter_rpc()"]
end
%% Asynchronous execution
AppState -.->|"Persist"| Storage[(Persistent Storage)]
MsgLoop -.->|"Poll Tasks"| Executor["Async Executor
poll_all_tasks()"]
ProcessInternals -->|"Generate"| OutMsg[/"Outgoing Messages"/]
end
%% External communication points
ExtClient1["HTTP Client"] & ExtClient2["WebSocket Client"] --> InMsg
OutMsg --> Process2["Process B"]
Process2 --> InMsg
end
%% ASYNC FLOW - Detailed sequence of async communication
subgraph AsyncFlow["⚡ ASYNC MESSAGE EXCHANGE"]
direction LR
AF1["1️⃣ Call RPC Stub
increment_counter_rpc(target, 42)"] -->
AF2["2️⃣ Generate UUID
correlation_id = uuid::new_v4()"] -->
AF3["3️⃣ Create Future
ResponseFuture(correlation_id)"] -->
AF4["4️⃣ Send Request
context=correlation_id"] -->
AF5["5️⃣ Target Process
Handle request & generate result"] -->
AF6["6️⃣ Send Response
context=correlation_id"] -->
AF7["7️⃣ Message Loop
Receives response with correlation_id"] -->
AF8["8️⃣ Response Registry
Store response by correlation_id"] -->
AF9["9️⃣ Future Polling
ResponseFuture finds response and completes"]
end
%% KEY CONNECTIONS BETWEEN SECTIONS
%% Build to Runtime
WasmComp ==>|"Load Component"| Process
%% Runtime to Async Flow
CallStub ==>|"Initiates"| AF1
AF9 ==>|"Resume Future in"| Executor
RespRegistry ===|"Powers"| AF8
%% Annotation for the Correlation ID system
CorrelationNote["CORRELATION SYSTEM
Tracks request→response with UUIDs"] -.-> RespRegistry
%% Style elements
class UserSrc,WitFiles,CallerUtils,EnumStructs,ReqResEnums,HandlerDisp,AsyncRuntime,MainLoop,WasmComp mainflow
class MsgLoop,Executor,RespRegistry,RespHandler,AF2,AF8 accent
class MsgRouter,HttpHandler,LocalHandler,RemoteHandler,WsHandler,CallStub,AppState dataflow
class AF1,AF3,AF4,AF5,AF6,AF7,AF9 asyncflow
class ExtClient1,ExtClient2,Process2,Storage,InMsg,OutMsg external
class CorrelationNote annotation
%% Subgraph styling
style BuildPhase fill:#171717,stroke:#333333,color:#ffffff
style CodeGen fill:#222222,stroke:#444444,color:#ffffff
style BindgenOutputs fill:#2A2A2A,stroke:#444444,color:#ffffff
style MacroOutputs fill:#2A2A2A,stroke:#444444,color:#ffffff
style RuntimePhase fill:#171717,stroke:#333333,color:#ffffff
style Process fill:#222222,stroke:#444444,color:#ffffff
style ProcessInternals fill:#2A2A2A,stroke:#444444,color:#ffffff
style AsyncFlow fill:#222222,stroke:#FF6600,color:#ffffff
Todos
- Let the new kit templates make use of the new framework