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

Fix isConnected #39

Merged
merged 1 commit into from
Feb 21, 2019
Merged

Fix isConnected #39

merged 1 commit into from
Feb 21, 2019

Conversation

ostap0207
Copy link
Contributor

Currently isConnected is true when connection exists. However, this
is different from JS and Swift implementations.

In JS and Swift implementations isConnected is true only when the
socket is actually connected.

One of the problems this causes was that channels were closed during the
following scenario:

  1. WebSocket connection established, one channel is joined..
  2. Internet/Wifi disabled -> connection fails.
  3. We periodically call phxSocket.connect() to reconnect.
  4. Channels try to rejoin as isConnected is true, but phx_join
    messages are being queued instead as connection is still not established.
  5. Internet/Wifi enabled -> multiple queued phx_join messages are sent,
    Phoniex Server relies phx_close to all join messages but last.

On phx_close the channel is removed from the socket, even tho the logs
show that the state is being received.

connect method is changed to use connection != null check, the same
way it's done in JS version.

Copy link
Owner

@dsrees dsrees left a comment

Choose a reason for hiding this comment

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

Good find, thanks for the fix. Small change in the tests and then we can merge this


socket.onOpen(mockSocket, null)

assertTrue(socket.isConnected)
Copy link
Owner

Choose a reason for hiding this comment

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

This project uses Google's Truth lib for assertions

assertThat(socket.isConnect).isTrue()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Missed that. Updated now.

Currently `isConnected` is true when `connection` exists. However, this
is different from JS and Swift implementations.

In [JS][1] and [Swift][2] implementations `isConnected` is true only when the
socket is actually connected.

One of the problems this causes was the channels were closed during the
following scenario:
1. WebSocket connection established, one channel is joined..
2. Internet/Wifi disabled -> connection fails.
3. We periodically call `phxSocket.connect()` to reconnect.
4. Channels [try to rejoin as `isConnected` is true][4], but `phx_join`
messages are being queued instead as connection is still not established.
5. Internet/Wifi enabled -> multiple queued `phx_join` messages are sent,
Phoniex Server relies `phx_close` to all join messages but last.

On `phx_close` the channel is removed from the socket, even tho the logs
show that the state is being received.

`connect` method is changed to use `connection != null` check, the same
way it's done in [JS version][3].

[1]: https://github.com/phoenixframework/phoenix/blob/93db5ff3adf4c91a1ff1996e819e7dd5dfbddf1a/assets/js/phoenix.js#L906
[2]: https://github.com/davidstump/SwiftPhoenixClient/blob/ade5c27051a96aeeedff1594cb3e176b57a02f96/Sources/Socket.swift#L180
[3]: https://github.com/phoenixframework/phoenix/blob/93db5ff3adf4c91a1ff1996e819e7dd5dfbddf1a/assets/js/phoenix.js#L782
[4]: https://github.com/dsrees/JavaPhoenixClient/blob/master/src/main/kotlin/org/phoenixframework/PhxChannel.kt#L74
@ostap0207
Copy link
Contributor Author

ostap0207 commented Feb 20, 2019

One question. In the case of calling disconnect from the client, does websocket triggers onClosed as well, so that isConnected updated to false? (I am not really familiar with okhttp websocket, so just wanted to clarify this).

@ostap0207
Copy link
Contributor Author

I checked ☝️myself, and onClosed does get called, so no problem there.

Copy link
Owner

@dsrees dsrees left a comment

Choose a reason for hiding this comment

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

👍

@dsrees dsrees merged commit 553785c into dsrees:master Feb 21, 2019
@ostap0207
Copy link
Contributor Author

Hey, would you be able to publish a new version with this fix any time soon?

@dsrees
Copy link
Owner

dsrees commented Feb 27, 2019

@ostap0207 0.1.7 has been released

@ostap0207
Copy link
Contributor Author

Thanks a lot!

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