H3 – rationale for events rather than requests
Would someone be kind enough to explain to me the rationale for H3 using events vs requests?
I'm a "fairweather" backend guy; i.e. I know enough to make it work but often not the rationale behind things.
From what I can gather about the "universal" nature of where h3 is designed to run, I assume that custom H3 Events were chosen to abstract and consolidate disparate properties of the various runtime targets and environments?
It's just a bit vague for me to see various mentions of requests in the docs, but no explanation of why and how we got from one to the other.
TIA!
11 Replies
Hi @davestewart. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
The rationale for using events in H3 instead of traditional requests is primarily about abstraction and flexibility across different runtime environments. H3 is designed to be a lightweight and composable server framework that can operate in various JavaScript runtimes, such as Node.js, Deno, and edge environments. This universality necessitates a more flexible approach to handling HTTP interactions.
Key Points:
1. Abstraction of Runtime Differences:
- H3's event system abstracts the differences between various runtime environments. By using a unified event object, H3 can provide a consistent interface for handling HTTP requests, regardless of the underlying platform. This abstraction allows developers to write code that is portable across different environments without worrying about the specific details of each runtime's request and response handling.
2. Composability and Flexibility:
- The event-driven model in H3 allows for greater composability. Each event object carries the incoming request and context, which can be passed through various lifecycle hooks and composable utilities. This design makes it easier to extend functionality and manage the flow of data through the application in a clean and explicit manner.
3. Unified Interface:
- By using events, H3 provides a unified interface that can handle both Node.js-style
(req, res)
handlers and web-style Request => Response
handlers. This flexibility is achieved through utilities like fromNodeListener
and fromWebHandler
, which convert traditional handlers into H3-compatible event handlers. This approach simplifies the integration of existing middleware and utilities into H3 applications.
4. Avoiding Direct Dependency on Node.js:
- The event object in H3 is designed to minimize direct dependency on Node.js-specific features. While event.node
provides access to the native Node.js request and response, H3 encourages the use of its own utilities to maintain compatibility across different environments.
For more detailed information, you can refer to the H3 Guide on Event Object and Converting to H3 Handler.
These design choices make H3 a versatile framework that can adapt to the needs of modern web applications running in diverse environments.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@350257396503019542> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai can you provide a table of runtimes / environments, the incoming event / request type, and any other details that it would be good to understand, to explain how H3 can successfully make all inputs universal?
Let me check the knowledge base for you again. One moment! :books:
----------------------
Certainly! Below is a table that outlines various runtimes/environments where H3 can operate, the type of incoming event/request, and relevant details that help explain how H3 achieves universality across different platforms.
| Runtime/Environment | Incoming Event/Request Type | Details |
|---------------------|-----------------------------|---------|
| Node.js |
event.node
| H3 provides a compatibility layer using event.node
to access native Node.js request and response objects. However, it encourages using H3 utilities to maintain cross-environment compatibility. |
| Web (Edge Runtimes) | Request
and Response
| H3 uses the toWebHandler
adapter to convert H3 apps into a fetch-like function, compatible with environments supporting the Fetch API. This allows H3 to run in edge environments with Web API compatibility. |
| Bun | Request
and Response
| Bun is fully compatible with Node.js API, and H3 can use the Web Adapter to handle requests in a similar manner to web environments. |
| Cloudflare Workers | Request
and Response
| H3 can be hosted on Cloudflare Workers using the Web Adapter, allowing it to handle requests in a serverless environment with Web API compatibility. |
| Deno | Request
and Response
| Similar to other web-compatible environments, H3 can run in Deno using the Web Adapter, leveraging Deno's support for the Fetch API. |
| Plain (Unknown Runtimes) | Object Interface | The Plain adapter allows H3 to run in unknown or custom environments by using an object input/output interface, making it suitable for testing or lambda-like environments. |
Additional Details:
- Event Object: In H3, the event object is a central concept that abstracts the incoming request and context. It provides a consistent interface across different runtimes, allowing developers to access request details like method, path, headers, and more, without worrying about the underlying platform specifics.
- Adapters: H3 uses adapters to bridge the gap between its internal event system and the external request/response handling of various runtimes. This modular approach allows H3 to be flexible and adaptable to different environments.
For more information on how H3 handles events and requests across different environments, you can refer to the H3 Adapters documentation and the H3 Guide on Event Object.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@350257396503019542> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai I'm pasting that table here as it didn't format correctly in the chat.
Perhaps you can render it as a nested list instead?

Let me check the knowledge base for you again. One moment! :books:
----------------------
Certainly! Here's the table formatted in Markdown:
Additional Details:
- Event Object: In H3, the event object is a central concept that abstracts the incoming request and context. It provides a consistent interface across different runtimes, allowing developers to access request details like method, path, headers, and more, without worrying about the underlying platform specifics.
- Adapters: H3 uses adapters to bridge the gap between its internal event system and the external request/response handling of various runtimes. This modular approach allows H3 to be flexible and adaptable to different environments.
For more information on how H3 handles events and requests across different environments, you can refer to the H3 Adapters documentation and the H3 Guide on Event Object.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
<@350257396503019542> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai Also, I think you misunderstood what I meant by "input". It seems you are mixing concepts from before and after H3's abstraction layer.
What I wanted to know was more about the native requests from the particular environment.
Claude AI gave me this:

Let me check the knowledge base for you again. One moment! :books:
----------------------
Hmm, I don't know enough to give you a confident answer yet. For more detailed information on how H3 handles events and requests across different environments, you might want to explore the H3 Adapters documentation and the H3 Guide on Event Object. These resources can provide insights into the native requests from particular environments and how H3 abstracts them.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
<@350257396503019542> Kapa.ai is still learning and improving, please let me know how I did by reacting below.