-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move not essential property from route
- Loading branch information
Showing
8 changed files
with
241 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
## Server-Sent Events (SSE) | ||
|
||
Server-Sent Events (SSE) can be integrated with asynchronous routes to enable real-time updates to the client. This feature is especially useful for long-running tasks, where clients can receive continuous feedback without needing to poll the server. | ||
|
||
### Key Features | ||
|
||
- **Real-time Server Push**: Sends updates from the server to the client in real-time, keeping the client informed about the progress or completion of asynchronous tasks. | ||
- **Progress and Task Completion Events**: Automatically trigger events like `pode.taskCompleted` to notify clients when a task finishes, with the option to include result data or error details. | ||
- **Customizable SSE Groups**: SSE connections can be grouped, either by the route’s path or by a custom group name, for better organization and management. | ||
- **Send Task Results**: Optionally include the result of the asynchronous task in the SSE event when the task completes. | ||
- **Seamless Async Route Integration**: SSE routes work alongside async routes, enabling instant feedback without additional complexity. | ||
|
||
### Example Use Cases | ||
|
||
#### Real-time Progress Updates | ||
|
||
When processing long-running tasks such as file uploads, data processing, or background calculations, SSE provides a way to continuously send updates to the client. As the task progresses, events are pushed to inform the client of the current state, eliminating the need for polling. | ||
|
||
##### Example: Adding SSE to a Progressing Task | ||
|
||
```powershell | ||
Add-PodeRoute -PassThru -Method Post -Path '/process-data' -ScriptBlock { | ||
$data = Get-PodeBody | ||
Set-PodeAsyncRouteProgress -Start 0 -End 100 -Steps 5 | ||
# Simulate long-running task | ||
for ($i = 0; $i -le 100; $i += 5) { | ||
Set-PodeAsyncRouteProgress -Tick | ||
Start-Sleep 1 | ||
} | ||
return @{ message = "Processing completed" } | ||
} | Set-PodeAsyncRoute -MaxRunspaces 2 -PassThru | | ||
Add-PodeAsyncRouteSse -SseGroup 'DataProcess' | ||
``` | ||
|
||
In this example: | ||
- An async route processes data and sends progress updates using `Set-PodeAsyncRouteProgress`. | ||
- The route is enabled for SSE, pushing real-time progress events to clients grouped under 'DataProcess'. | ||
|
||
#### Task Completion and Result Delivery | ||
|
||
Once an async task completes, an SSE event can automatically be sent to the client with the task's result or an error if it failed. This ensures that clients are immediately informed when a task has finished, without requiring them to repeatedly check the task’s status. | ||
|
||
##### Example: Task Completion with Result Transmission | ||
|
||
```powershell | ||
Add-PodeRoute -PassThru -Method Get -Path '/generate-report' -ScriptBlock { | ||
# Simulate long-running report generation | ||
Start-Sleep 10 | ||
return @{ report = "Report generation completed" } | ||
} | Set-PodeAsyncRoute -MaxRunspaces 3 -PassThru | | ||
Add-PodeAsyncRouteSse -SendResult | ||
``` | ||
|
||
In this example: | ||
- A GET route generates a report asynchronously. | ||
- The SSE sends the final report result to the client upon task completion. | ||
|
||
#### Example: Sending Custom SSE Events with Delays | ||
|
||
```powershell | ||
Add-PodeRoute -PassThru -Method Get -Path '/sse' -ScriptBlock { | ||
# First SSE event with a message about the current time | ||
$msg = "Start - Hello there! The datetime is: $([datetime]::Now.TimeOfDay)" | ||
Send-PodeSseEvent -Data $msg -FromEvent | ||
# Simulate a delay between messages (10 seconds) | ||
Start-Sleep -Seconds 10 | ||
# Final SSE event after all operations are done | ||
$msg = "End - Hello there! The datetime is: $([datetime]::Now.TimeOfDay)" | ||
Send-PodeSseEvent -Data $msg -FromEvent | ||
# Return a JSON response to the client indicating the operation is complete | ||
return @{ message = 'Done' } | ||
} | Set-PodeAsyncRoute -ResponseContentType 'application/json' -MaxRunspaces 3 -PassThru | | ||
Add-PodeAsyncRouteSse -SseGroup 'Test events' -SendResult | ||
``` | ||
|
||
In this example: | ||
- Custom SSE events are sent to the client at specific intervals with messages that include the current time. | ||
- After a delay, a final SSE event is sent, and the client receives a JSON response once the task is complete. | ||
|
||
### Parameters | ||
|
||
- **Route**: A hashtable array representing the async route(s) to which SSE functionality will be added. This parameter is mandatory and accepts pipeline input. | ||
- **PassThru**: If specified, the modified route object is returned after adding the SSE route. | ||
- **SseGroup**: The group name for the SSE connection. If not provided, the route’s path will be used as the group name. | ||
- **SendResult**: If specified, the result of the async operation will be sent to the client when the task completes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.