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

Await for a successful connection to the websocket before listening to messages #76

Merged

Conversation

miguelcmedeiros
Copy link
Contributor

@miguelcmedeiros miguelcmedeiros commented Apr 18, 2024

Similar to issues (issue 1 and issue 2) reported in web_socket_channel package.

When the websocket connection is created and there's an exception thrown at this point we won't be able to catch it, leaving the client in an unrecoverable state (i.e. since the exception is not caught, the websocket artefacts are not properly cleared up).

socket.ready will complete with an error in case something goes wrong during WebSocketChannel.connect().

It's not an obvious API, but it seems to be the way to go with this package.

@miguelcmedeiros
Copy link
Contributor Author

@matehat the integration sockets tests are failing. I will investigate what's happening there and let you know if I need help with them.

@@ -508,10 +516,6 @@ class PhoenixSocket {
void _onSocketData(message) => onSocketDataCallback(message);

void _onSocketError(dynamic error, dynamic stacktrace) {
if (_socketState == SocketState.closing ||
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@matehat
While testing the original change, I noticed that this check was preventing the socket from being reconnected, since _onSocketClosed() is called after it. The scheduling of the socket reconnection happens at that point.

Does it make sense?

@@ -171,7 +171,9 @@ class PhoenixSocket {
bool get isConnected => _ws != null && _socketState == SocketState.connected;

void _connect(Completer<PhoenixSocket?> completer) async {
if (_ws != null) {
if (_ws != null &&
(_socketState != SocketState.connected ||
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@matehat
In the case of a reconnection, we have a closed socket, then we would not attempt to reconnect.
If the socket state is unknown, closing or closed, then we are able to proceed with the reconnection of the socket.

@miguelcmedeiros miguelcmedeiros force-pushed the bugfix/await-socket-connect-ready branch from 49c79f9 to c964569 Compare April 23, 2024 17:20
@miguelcmedeiros miguelcmedeiros force-pushed the bugfix/await-socket-connect-ready branch from c964569 to be10e08 Compare April 24, 2024 10:23
@matehat
Copy link
Member

matehat commented Apr 24, 2024

Great investigation! Thanks @miguelcmedeiros for your work :)

@matehat matehat merged commit aba8e6e into braverhealth:master Apr 24, 2024
1 check passed
s6o pushed a commit to s6o/phoenix-socket-dart that referenced this pull request May 23, 2024
…o messages (braverhealth#76)

* Await for a successful connection to the websocket before listening to messages

* Fix socket reconnection

* Update socket state accordingly and reason about at the beginning of _connect()
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