-
Notifications
You must be signed in to change notification settings - Fork 453
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
Conversation
There was a problem hiding this 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Anyone reviewing these PRs? This change looks good to me. |
|
||
element.style.backgroundColor = "gray"; | ||
socket.onopen = function(){ | ||
status.innerHTML = "Connected" |
There was a problem hiding this comment.
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!
@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. |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this 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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
.
@nateps @rkstedman Any chance of reviewing this PR? I find it to be a great improvement. |
@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. 🙂 |
@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 😉 ) |
@pedrosanta To summarize your proposed changes from the discussion:
to
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? |
@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. |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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). |
There was a problem hiding this comment.
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.
Woohoo thanks @nateps ! |
I updated the textarea example and updated the readme about how to handle reconnections.