Skip to content

Improve isSocketClosingOrClosed Check to Handle WebSocket.CONNECTING State#83

Open
shahzaib-deriv wants to merge 3 commits into
deriv-com:masterfrom
shahzaib-deriv:fix/websocket-alive-connection-check
Open

Improve isSocketClosingOrClosed Check to Handle WebSocket.CONNECTING State#83
shahzaib-deriv wants to merge 3 commits into
deriv-com:masterfrom
shahzaib-deriv:fix/websocket-alive-connection-check

Conversation

@shahzaib-deriv

Copy link
Copy Markdown
Contributor

Description:

In the current implementation of the isSocketClosingOrClosed method, the state of the WebSocket connection is checked using the following logic:

isSocketClosingOrClosed() {
    return ![2, 3].includes(this.websocket.readyState);
}

This condition currently checks if the WebSocket connection is in the CLOSED (3) or CLOSING (2) states but does not handle the CONNECTING (0) state. As a result, if send is called while the WebSocket is still in the CONNECTING state, it may throw an error.

Suggested Improvement:

To enhance robustness, updating the condition to explicitly account for the CONNECTING state as follows:

isSocketReadyForSending() {
    return this.websocket.readyState === WebSocket.OPEN; // 1
}

This modification would prevent sending messages when the WebSocket is not fully open, reducing the likelihood of encountering runtime errors and improving the overall stability of the application.

@yashim-deriv yashim-deriv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit on naming. the rest lgtm

Comment thread src/client-library/deriv-api-client.ts Outdated
Comment thread src/client-library/deriv-api-client.ts Outdated
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.

3 participants