Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for converting HTTP connections into SSE connections #1256

Merged
merged 6 commits into from
Mar 23, 2024

Conversation

Badgerati
Copy link
Owner

Description of the Change

Adds support to be able to convert an HTTP connection into an SSE connection, allowing you to stream events from the server to connected clients.

At its simplest, you can convert an HTTP connection in a Route's scriptblock into an SSE connection using the new ConvertTo-PodeSseConnection function. You'll then be able to send events to one or more connected clients using Send-PodeSseEvent.

SSE connections can be Locally or Globally scoped. Locally scoped connections will follow the normal HTTP request lifecycle, whereby the request will be closed once the Route has been dealt with. Globally scoped connections will be kept open and cached internally so that events can be streamed to them later on. Global is the default.

SSE connections can be used mostly on the frontend, using the EventSource class in JavaScript.

Related Issue

Resolves #1245

Examples

The following will convert requests to /events into global SSE connections, and then a Schedule will send events to them every minute:

Start-PodeServer {
    Add-PodeEndpoint -Address * -Port 8090 -Protocol Http

    Add-PodeRoute -Method Get -Path '/events' -ScriptBlock {
        ConvertTo-PodeSseConnection -Name 'Events'
    }

    Add-PodeSchedule -Name 'Example' -Cron (New-PodeCron -Every Minute) -ScriptBlock {
        Send-PodeSseEvent -Name 'Events' -Data "Hello there! The datetime is: $([datetime]::Now.TimeOfDay)"
    }
}

The following will convert requests to /events into local SSE connections, and two events will be sent back to the client before the connection is closed:

Start-PodeServer {
    Add-PodeEndpoint -Address * -Port 8090 -Protocol Http

    Add-PodeRoute -Method Get -Path '/events' -ScriptBlock {
        ConvertTo-PodeSseConnection -Name 'Events' -Scope Local
        Send-PodeSseEvent -FromEvent -Data "Hello there! The datetime is: $([datetime]::Now.TimeOfDay)"
        Start-Sleep -Seconds 10
        Send-PodeSseEvent -FromEvent -Data "Hello there! The datetime is: $([datetime]::Now.TimeOfDay)"
    }
}

@Badgerati Badgerati added this to the 2.10.0 milestone Mar 23, 2024
@Badgerati Badgerati self-assigned this Mar 23, 2024
@Badgerati Badgerati merged commit 8cb6feb into develop Mar 23, 2024
8 checks passed
@Badgerati Badgerati deleted the Issue-1245 branch March 23, 2024 20:09
@Badgerati Badgerati mentioned this pull request Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for SSE Routes
1 participant