Improved SSE/WebSocket connection handling and clean-up; Added additional events for SSE, WebSockets, and Authentication; Added manual upgrade path for WebSocket connection, including custom ClientId support #1638
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This is a hefty PR that covers various improvements and fixes, but also new features, for #1547 and #1548 - but primarily for Pode.Web to resolve stability and clean-up issues with SSE and WebSocket (Signal) connections (for example: Badgerati/Pode.Web#660)
The Great Refactor
Ultimately a lot of the underlying logic for SSE/WebSockets has been refactored, to far better handle when clients disconnect and to enable Pode to be aware of this fact.
For WebSockets, fortunately, a lot of this is handled for us by inbuilt JS WebSocket libraries - if the client disconnects it will inform the server via a
closeevent. This said however, Pode now has a Timer which will periodically call connected clients with apingevent - same as how the clients "ping" the server - in case thatcloseevent is never called. If thepingevent to the client fails, the client has disconnected.For SSE we have to rely on a Timer, this timer will also periodically send a
pode.pingevent to connected clients - similar to the existingpode.openandpode.closeevents. If thepode.pingevent to the client fails, the client has disconnected.Knowing these disconnected clients, Pode can now clean-up the resources. These events are configured to occur roughly every 60 seconds.
Manual WebSocket Control
Unlike SSE where you have full control over when the HTTP request is upgraded via
ConvertTo-PodeSseConnection, WebSockets (Signals) have always been "auto-upgraded". This meant you had zero control over the ClientId sent back, no control over when the upgrade actually occurred, but more importantly zero HTTP authentication support on that upgrade.Well, there's now a
ConvertTo-PodeSignalConnectionwhich works in the same manner as the SSE counterpart. The auto-upgrading is still the default functionality for backwards compatibility, but you can disable this by supplying-NoAutoUpgradeWebSocketsto the relevant HTTP/WSAdd-PodeEndpoint. To upgrade the HTTP request to a Signal connection simply invokeConvertTo-PodeSignalConnectionfrom anAdd-PodeRoute.More Events
Now that we can track client connections and disconnections, there are some new Events (Connected/Disconnected) and Functions for SSE and Signals:
Register-PodeSseEventUnregister-PodeSseEventTest-PodeSseEventGet-PodeSseEventRegister-PodeSignalEventUnregister-PodeSignalEventTest-PodeSignalEventGet-PodeSignalEventWhenever an event is triggered, the event's scriptblock will be triggered and details about the client connection will be supplied - such as ClientId, Group, Type, etc. as a
$TriggeredEvent.Connection.[...]Furthermore, there are now also new Events (Login/Logout) and Functions for end-user authentication (eg,
Add-PodeAuth):Register-PodeAuthEventUnregister-PodeAuthEventTest-PodeAuthEventGet-PodeAuthEventWhenever an end-user logs in successfully, or logouts, these events will be triggered and supplied the User object as
$TriggeredEvent.User, and the Name of the Authentication method which triggered the login/logout - in the case of merged authentication strategies the event will be triggered for the valid authentication plus the chain of parent authentication methods.Added / Fixes