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

Reconnecting Websocket Example and Updated Readme #138

Merged
merged 2 commits into from
Jul 14, 2018

Conversation

mbalex99
Copy link
Contributor

I updated the textarea example and updated the readme about how to handle reconnections.

@coveralls
Copy link

coveralls commented Jan 26, 2017

Coverage Status

Coverage remained the same at 96.106% when pulling e1f7ab0 on mbalex99:reconnectingwebsocket into 05c30eb on share:master.

Copy link
Contributor

@curran curran left a comment

Choose a reason for hiding this comment

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

Nice change.

@@ -27,10 +27,26 @@ tracker](https://github.com/share/sharedb/issues).
- Projections to select desired fields from documents and operations
- Middleware for implementing access control and custom extensions
- Ideal for use in browsers or on the server
- Reconnection of document and query subscriptions
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! It's high time this documentation be updated.

Choose a reason for hiding this comment

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

Actually, as per #121, this is correct.

When the WebSocket reconnects, the Connection middleware reconnects both documents and query subscriptions.

We might need more clear documentation, but this line is correct.

Choose a reason for hiding this comment

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

Perhaps a more clear wording would be - Reconnection of document and query subscriptions on socket reconnection (Check 'Reconnection' section below). Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

@pedrosanta I think that would be more clear. I'd welcome that addition here.

@nateps This PR seems to address a common source of frustration/confusion on ShareDB reconnection. Any chance it could be reviewed by the team?

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that the frequent confusion indicates we should change the wording.

However, I would like to highlight the fact that subscriptions state is re-established as soon as the connection resumes. This is a key feature of the ShareDB client.

I prefer the suggestion of more clear wording.

@curran
Copy link
Contributor

curran commented Apr 4, 2017

Anyone reviewing these PRs? This change looks good to me.


element.style.backgroundColor = "gray";
socket.onopen = function(){
status.innerHTML = "Connected"
Copy link
Contributor

Choose a reason for hiding this comment

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

This is quite useful stuff!

@curran
Copy link
Contributor

curran commented May 12, 2017

@nateps @rkstedman This PR looks like a nice improvement. Would merge it if I could. Any chance one of you could review this one? Thank you.

Copy link

@pedrosanta pedrosanta left a comment

Choose a reason for hiding this comment

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

Overall the code seems pretty nice @mbalex99, nice one. 👍 But I don't know if we, like, shouldn't really make the effort and add this (the reconnect handling) built-in on Connection/ShareDB.

Comment retracted, check new review.

@@ -27,10 +27,26 @@ tracker](https://github.com/share/sharedb/issues).
- Projections to select desired fields from documents and operations
- Middleware for implementing access control and custom extensions
- Ideal for use in browsers or on the server
- Reconnection of document and query subscriptions
- Offline change syncing upon reconnection
Copy link

@pedrosanta pedrosanta May 19, 2017

Choose a reason for hiding this comment

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

@mbalex99 I mean, if reconnection doesn't come out of the box, I think this line should also be removed, as it's misleading.

Choose a reason for hiding this comment

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

Actually I retract my comment, this actually happens too.

Ops are added to pending 'queue'/array and flushed/synced on socket reconnection.

This line is also correct.

Choose a reason for hiding this comment

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

Also, a better wording could be - Offline change syncing upon socket reconnection.

Copy link

@pedrosanta pedrosanta left a comment

Choose a reason for hiding this comment

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

Also we probably need to update connection.js comment here, as it seems misleading:

https://github.com/share/sharedb/blob/master/lib/client/connection.js#L90

Perhaps update to something like:

  // - 'disconnected' Connection is closed, and reconnect can be attempted
  // - 'closed'       The connection was closed by the client, a reconnect should not be attempted
  // - 'stopped'      The connection was closed by the server, a reconnect should not be attempted

Also, perhaps we should update the documentation regarding node-browserchanel here, wdyt?

https://github.com/share/sharedb/blob/master/lib/client/connection.js#L138

For more on this check #121 too.

@@ -27,10 +27,26 @@ tracker](https://github.com/share/sharedb/issues).
- Projections to select desired fields from documents and operations
- Middleware for implementing access control and custom extensions
- Ideal for use in browsers or on the server
- Reconnection of document and query subscriptions

Choose a reason for hiding this comment

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

Actually, as per #121, this is correct.

When the WebSocket reconnects, the Connection middleware reconnects both documents and query subscriptions.

We might need more clear documentation, but this line is correct.

@@ -27,10 +27,26 @@ tracker](https://github.com/share/sharedb/issues).
- Projections to select desired fields from documents and operations
- Middleware for implementing access control and custom extensions
- Ideal for use in browsers or on the server
- Reconnection of document and query subscriptions
- Offline change syncing upon reconnection

Choose a reason for hiding this comment

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

Actually I retract my comment, this actually happens too.

Ops are added to pending 'queue'/array and flushed/synced on socket reconnection.

This line is also correct.

@@ -27,10 +27,26 @@ tracker](https://github.com/share/sharedb/issues).
- Projections to select desired fields from documents and operations
- Middleware for implementing access control and custom extensions
- Ideal for use in browsers or on the server
- Reconnection of document and query subscriptions

Choose a reason for hiding this comment

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

Perhaps a more clear wording would be - Reconnection of document and query subscriptions on socket reconnection (Check 'Reconnection' section below). Thoughts?

@@ -27,10 +27,26 @@ tracker](https://github.com/share/sharedb/issues).
- Projections to select desired fields from documents and operations
- Middleware for implementing access control and custom extensions
- Ideal for use in browsers or on the server
- Reconnection of document and query subscriptions
- Offline change syncing upon reconnection

Choose a reason for hiding this comment

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

Also, a better wording could be - Offline change syncing upon socket reconnection.

@curran
Copy link
Contributor

curran commented Jun 7, 2017

@nateps @rkstedman Any chance of reviewing this PR? I find it to be a great improvement.

@pedrosanta
Copy link

pedrosanta commented Jun 7, 2017

@curran @nateps @rkstedman I think this PR still needs some updates/improvements though, as I've commented.

It was great if @mbalex99 could address those. 🙂

@curran
Copy link
Contributor

curran commented Jun 7, 2017

@pedrosanta Indeed, thanks for your suggestions. The discussion is quite long, and it will take some work to go through and aggregate the updates/improvements you suggested.

Would you mind summarizing the specific changes you'd like to see?

(or, better yet, maybe you could branch off this and create a new PR with your proposed changes 😉 )

@curran
Copy link
Contributor

curran commented Jun 10, 2017

@pedrosanta To summarize your proposed changes from the discussion:

  // - 'disconnected' Connection is closed, but it will reconnect automatically
  // - 'closed'       The connection was closed by the client, and will not reconnect
  // - 'stopped'      The connection was closed by the server, and will not reconnect

to

  // - 'disconnected' Connection is closed, and reconnect can be attempted
  // - 'closed'       The connection was closed by the client, a reconnect should not be attempted
  // - 'stopped'      The connection was closed by the server, a reconnect should not be attempted

Are these the complete changes to this PR you'd like to see? It almost feels like these changes could be made in a separate documentation-focused PR, after this PR gets merged. Thoughts?

@curran
Copy link
Contributor

curran commented Nov 24, 2017

@nateps @rkstedman @josephg [PR tending note] I think this PR can be merged. It would close the long-standing issue #121 , which would be a big win. Changes are in documentation and examples only, so no risk of ShareDB breakage.

Copy link
Contributor

@nateps nateps left a comment

Choose a reason for hiding this comment

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

Appreciate the contribution!

There are two ways to implement reconnection with ShareDB Connections, either using a WebSocket-like object that implements reconnection or calling Connection::bindToSocket() with a new WebSocket object. I'll merge this and document that both options are supported.

@@ -27,10 +27,26 @@ tracker](https://github.com/share/sharedb/issues).
- Projections to select desired fields from documents and operations
- Middleware for implementing access control and custom extensions
- Ideal for use in browsers or on the server
- Reconnection of document and query subscriptions
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that the frequent confusion indicates we should change the wording.

However, I would like to highlight the fact that subscriptions state is re-established as soon as the connection resumes. This is a key feature of the ShareDB client.

I prefer the suggestion of more clear wording.


The easiest way is to give it a WebSocket object that does reconnect. There are plenty of example on the web. The most important thing is that the custom reconnecting websocket, must have the same API as the native rfc6455 version.

In the "textarea" example we show this off using a Reconnecting Websocket implementation from [https://github.com/pladaria/reconnecting-websocket](reconnecting-websocket).
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a great README addition, and it clarifies one way that ShareDB Connections support reconnection.

I'd also like to highlight that it is possible and intended that reconnection is implemented by calling Connection::bindToSocket() with a new WebSocket instance, as @DetweilerRyan pointed out. We've chosen not to implement reconnection logic in ShareDB, itself. There are other great libraries for that purpose, and application developers may need to leverage different libraries or custom logic depending on their requirements.

Reading the code of Reconnecting WebSocket, a significant amount of its complexity is necessitated by the constraint of exposing a single instance externally and internally creating new instances of WebSocket objects. If the reconnection logic of Reconnecting WebSocket implementation works for you, this is totally fine!

However, if you want to implement custom reconnection logic for your production environment (say you want to do backoff in a different way or fallback to other protocols or libraries), calling the ShareDB bindToSocket() with a new WebSocket or WebSocket-like instance might be simpler.

@nateps nateps merged commit 762e05d into share:master Jul 14, 2018
@curran
Copy link
Contributor

curran commented Jul 16, 2018

Woohoo thanks @nateps !

@mbalex99 mbalex99 deleted the reconnectingwebsocket branch July 18, 2018 01:16
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