Open grivera64 opened 1 month ago
I am currently working on a PR for this feature proposal.
Also: After considering the different names, I believe that either SendStreamWriter()
or SendWritableStream()
best fit the feature's API. Though since the code base maps SendStream()
to fasthttp'sSetBodyStream()
method, I will keep the name SendStreamWriter()
in the PR to map to fasthttp's SetBodyStreamWriter()
method.
What are your thoughts on this?
Feature Proposal Description
This feature proposal proposes to add buffered streaming support to Fiber v3. By adding this feature, Fiber users can send shorter segments of content over persistent HTTP connections. This functionality is important for several web-based applications such as:
To implement this feature, this would involve using the underlying
*fasthttp.RequestCtx
struct. Specifically, this feature can leverage theRequestCtx.SetBodyStreamWriter
method.The
StreamWriter
function passed intoRequestCtx.SetBodyStreamWriter
describes what a handler should write to an endpoint's response body as a buffered stream.Within Fiber's existing codebase, implementing this feature is the equivalent of:
It is also possible to implement this feature by directly using the fasthttp API:
For the feature's API, please view Feature Examples below.
Alignment with Express API
Using the Express API, buffered streaming is supported for
req
argument in an express route handler by using thewrite
, andend
methods. Below is an example usage of this feature for Server-Side Events (SSE):By providing a way to support buffered streams in Fiber, this will help make transitioning from Express much easier for more advanced use cases such as SSE as shown above.
To view the same Express example in Fiber with this new feature, please view Feature Examples.
HTTP RFC Standards Compliance
This feature does not modify how Fiber or fasthttp complies with HTTP RFC standards, as it just provides simplified access to an existing fasthttp feature.
API Stability
This feature's API will be introduced in v3 as a separate
Ctx
method, augmenting its interface without modifying existing methods. This will ensure that there are no breaking changes to old and new features for Fiber v3. (For more information, please view Feature Examples below.)Besides potential naming changes, this API feature will require very few changes in the future due to
fasthttp
methods it relies on have been stable since 9 years ago. Unlessfasthttp
makes big API changes to theirfasthttp.StreamWriter
type, maintenance will be limited.On the rare circumstance that the StreamWriter type does change, we can use an alias for that type and have no code change required. The only change that we would need is to update documentation.
Feature Examples
Function Signature:
Use in Server-Side Events (SSE):
Checklist: