-
Notifications
You must be signed in to change notification settings - Fork 459
Reconnecting Websocket Example and Updated Readme #138
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line 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 | ||
| - Offline change syncing upon reconnection | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Also, a better wording could be |
||
| - In-memory implementations of database and pub/sub for unit testing | ||
|
|
||
| ### Reconnection | ||
|
|
||
| **TLDR** | ||
| ```javascript | ||
| const WebSocket = require('reconnecting-websocket'); | ||
| var socket = new WebSocket('ws://' + window.location.host); | ||
| var connection = new sharedb.Connection(socket); | ||
| ``` | ||
|
|
||
| The native Websocket object that you feed to ShareDB's `Connection` constructor **does not** handle reconnections. | ||
|
|
||
| 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). | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 |
||
|
|
||
|
|
||
|
|
||
| ## Example apps | ||
|
|
||
| [<img src="examples/counter/demo.gif" height="300"> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,14 +2,35 @@ var sharedb = require('sharedb/lib/client'); | |
| var StringBinding = require('sharedb-string-binding'); | ||
|
|
||
| // Open WebSocket connection to ShareDB server | ||
| const WebSocket = require('reconnecting-websocket'); | ||
| var socket = new WebSocket('ws://' + window.location.host); | ||
| var connection = new sharedb.Connection(socket); | ||
|
|
||
| var element = document.querySelector('textarea'); | ||
| var statusSpan = document.getElementById('status-span'); | ||
| status.innerHTML = "Not Connected" | ||
|
|
||
| element.style.backgroundColor = "gray"; | ||
| socket.onopen = function(){ | ||
| status.innerHTML = "Connected" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is quite useful stuff! |
||
| element.style.backgroundColor = "white"; | ||
| }; | ||
|
|
||
| socket.onclose = function(){ | ||
| status.innerHTML = "Closed" | ||
| element.style.backgroundColor = "gray"; | ||
| }; | ||
|
|
||
| socket.onerror = function() { | ||
| status.innerHTML = "Error" | ||
| element.style.backgroundColor = "red"; | ||
| } | ||
|
|
||
| // Create local Doc instance mapped to 'examples' collection document with id 'textarea' | ||
| var doc = connection.get('examples', 'textarea'); | ||
| doc.subscribe(function(err) { | ||
| if (err) throw err; | ||
| var element = document.querySelector('textarea'); | ||
|
|
||
| var binding = new StringBinding(element, doc); | ||
| binding.setup(); | ||
| }); | ||
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.