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

Allow custom app logic to trigger when a websocket connects/reconnects #75

Merged
merged 3 commits into from
Aug 21, 2023

Conversation

peterbroadhurst
Copy link
Contributor

@peterbroadhurst peterbroadhurst commented Aug 10, 2023

When using an ephemeral WebSocket to listen for completion of a FireFly action, you need to know the socket is connected before you submit the action. This isn't currently possible in the SDK.

It's also not possible to send custom commands over the WebSocket, or trigger other processing as a side-effect of a reconnect.

Also in unit tests in a framework like Jest, you cannot allow a test to write log statements after the test exits (see jestjs/jest#9324 (comment)).

This drives a requirement for a close() function that allows us to await the logging of the close, which is also included as an option in this PR.

Signed-off-by: Peter Broadhurst <peter.broadhurst@kaleido.io>
Signed-off-by: Peter Broadhurst <peter.broadhurst@kaleido.io>
@peterbroadhurst peterbroadhurst marked this pull request as draft August 11, 2023 17:47
Signed-off-by: Peter Broadhurst <peter.broadhurst@kaleido.io>
@peterbroadhurst peterbroadhurst marked this pull request as ready for review August 11, 2023 18:06
async close(wait?: boolean): Promise<void> {
const closedPromise = new Promise<void>(resolve => {
this.closed = resolve;
});
this.clearPingTimers();
if (this.socket) {
Copy link
Contributor

@awrichar awrichar Aug 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than relying on the other "close" handler and dealing with callbacks back and forth, would it be possible to just add another handler here?

const closedPromise = new Promise<void>(resolve => this.socket.on('close', resolve));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I can for the use case I'm fighting. Jest cannot handle any logging after a test completes, so what I need to know is that the existing callback that does logging has done it.

If I add another callback as you propose, then I'll be relying on the order of invocation of event handlers. Happy to do that if you'd prefer (I have written up the code for it ready to push, but I'll need to test it).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed in a message directly with @awrichar and deciding to merge.

Copy link
Contributor

@awrichar awrichar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks alright to me - left one suggestion for possible simplification.

@peterbroadhurst peterbroadhurst merged commit 8b14e0d into main Aug 21, 2023
1 check passed
@peterbroadhurst peterbroadhurst deleted the ws-connect branch August 21, 2023 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants