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

CRNS-264: Connection related fixes/changes #626

Merged
merged 19 commits into from
Mar 10, 2021
Merged

Conversation

vishalnarkhede
Copy link
Contributor

@vishalnarkhede vishalnarkhede commented Mar 6, 2021

Submit a pull request

  • Call setHealth(true) only after you receive first ws event (so moved from onopen to onmessage). Only after onmessage, we can be sure that connection is ready. This is mentioned in one of the existing code comments as well:

we wait till the first message before we consider the connection open..
the reason for this is that auth errors and similar errors trigger a ws.onopen and immediately
after that a ws.onclose.

Introducing this change, because we don't get connectionId from onopen callback on websocket. We only get
connectionId from onmessage callback. So with existing implementation, if you want to rewatch/requery channels upon
network recovery, you can't rely on health of websocket to do that. Because watching channels requires connectionId.

After the change, you can easily do following:

client.on('connection.changed', (event) => {
  if (event.online) {
    // rewatch your channels or implement your recovery logic
  }
})

client.reconnectUser();
  • Introduced two aliases

    • client.closeConnection for client.wsConnection.disconnect // Updated description
    • client.openConnection for client._setupConnection // Updated description

    This makes it easier for push notification handling - as mentioned in caveats

  • Removed client.connectionId to avoid hassle of keeping it in sync with client.wsConnection.connectionId. Lets only access it from client.wsConnection.connectionId moving forward

TODO

  • Run unit tests
  • Run integration tests

CLA

  • I have signed the Stream CLA (required).
  • The code changes follow best practices
  • Code changes are tested (add some information if not applicable)

Description of the pull request

@github-actions
Copy link
Contributor

github-actions bot commented Mar 6, 2021

Size Change: +2.35 kB (1%)

Total Size: 216 kB

Filename Size Change
dist/browser.es.js 47 kB +563 B (1%)
dist/browser.full-bundle.min.js 27.3 kB +143 B (0%)
dist/browser.js 47.5 kB +539 B (1%)
dist/index.es.js 47 kB +564 B (1%)
dist/index.js 47.5 kB +543 B (1%)

compressed-size-action

@vishalnarkhede vishalnarkhede changed the title Connection related fixes/changes CRNS-264: Connection related fixes/changes Mar 6, 2021
@vishalnarkhede vishalnarkhede requested a review from DanC5 March 6, 2021 19:56
thesyncim
thesyncim previously approved these changes Mar 7, 2021
Copy link
Member

@thesyncim thesyncim left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -492,6 +491,8 @@ export class StableWSConnection<
return;
} else {
this.resolvePromise?.(event);
// set healthy..
this._setHealth(true);
Copy link
Member

Choose a reason for hiding this comment

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

this should fix the bug "watch or presence requires an active WebSocket connection"

Copy link
Contributor

@AnatolyRugalev AnatolyRugalev left a comment

Choose a reason for hiding this comment

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

LGTM

AnatolyRugalev
AnatolyRugalev previously approved these changes Mar 8, 2021
ferhatelmas
ferhatelmas previously approved these changes Mar 8, 2021
Copy link
Contributor

@ferhatelmas ferhatelmas left a comment

Choose a reason for hiding this comment

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

LGTM

@vishalnarkhede
Copy link
Contributor Author

vishalnarkhede commented Mar 8, 2021

@AnatolyRugalev @ferhatelmas @thesyncim

Updated some aliases after discussion with @mahboubii :

  • connectUser - connects a user
  • disconnectUser - disconnects a user, without removing user or its data from client
  • reconnectUser - reconnects a current user
  • disconnect - completely destroys entire session/client. We should probably rename this to client.destroy or something.

AnatolyRugalev
AnatolyRugalev previously approved these changes Mar 8, 2021
src/client.ts Outdated
if (this.wsConnection) {
return this.wsConnection.disconnect(timeout);
}
this.disconnectUser(timeout);
Copy link
Contributor

@mahboubii mahboubii Mar 8, 2021

Choose a reason for hiding this comment

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

this needs to be run on the function entry point before we reset the activeChannels state

Suggested change
this.disconnectUser(timeout);
const returnPromise = this.disconnectUser(timeout);
.....
cleanup client
...
return returnPromise;

@vishalnarkhede
Copy link
Contributor Author

vishalnarkhede commented Mar 9, 2021

Ok so finalising following endpoints as per discussion:

  • connectUser
  • disconnectUser (alias for disconnect)
  • closeConnection
  • openConnection

Will add these changes tomorrow !!

client._setupConnection -> client.openConnection
client.wsConnection.disconnect -> client.closeConnection
client.connectUser -> client.connectUser
client.disconnect -> client.disconnectUser
src/client.ts Outdated
};

/**
* Reconnects the current user.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Reconnects the current user.
* Creates a new WebSocket connection with the current user. throws if there is an active connection

@vishalnarkhede vishalnarkhede changed the base branch from master to dev March 10, 2021 13:47
Copy link
Contributor

@mahboubii mahboubii left a comment

Choose a reason for hiding this comment

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

LGTM 🌮

@vishalnarkhede vishalnarkhede merged commit cbe2b42 into dev Mar 10, 2021
@vishalnarkhede vishalnarkhede deleted the vishal/sethealth-fix branch March 10, 2021 16:35
@vishalnarkhede vishalnarkhede mentioned this pull request Mar 10, 2021
Merged
2 tasks
vishalnarkhede added a commit that referenced this pull request Mar 10, 2021
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.

5 participants