Skip to content

Commit 762e05d

Browse files
authored
Merge pull request #138 from mbalex99/reconnectingwebsocket
Reconnecting Websocket Example and Updated Readme
2 parents 68bde00 + e1f7ab0 commit 762e05d

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,26 @@ tracker](https://github.com/share/sharedb/issues).
2626
- Projections to select desired fields from documents and operations
2727
- Middleware for implementing access control and custom extensions
2828
- Ideal for use in browsers or on the server
29-
- Reconnection of document and query subscriptions
3029
- Offline change syncing upon reconnection
3130
- In-memory implementations of database and pub/sub for unit testing
3231

32+
### Reconnection
33+
34+
**TLDR**
35+
```javascript
36+
const WebSocket = require('reconnecting-websocket');
37+
var socket = new WebSocket('ws://' + window.location.host);
38+
var connection = new sharedb.Connection(socket);
39+
```
40+
41+
The native Websocket object that you feed to ShareDB's `Connection` constructor **does not** handle reconnections.
42+
43+
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.
44+
45+
In the "textarea" example we show this off using a Reconnecting Websocket implementation from [https://github.com/pladaria/reconnecting-websocket](reconnecting-websocket).
46+
47+
48+
3349
## Example apps
3450

3551
[<img src="examples/counter/demo.gif" height="300">

examples/textarea/client.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,35 @@ var sharedb = require('sharedb/lib/client');
22
var StringBinding = require('sharedb-string-binding');
33

44
// Open WebSocket connection to ShareDB server
5+
const WebSocket = require('reconnecting-websocket');
56
var socket = new WebSocket('ws://' + window.location.host);
67
var connection = new sharedb.Connection(socket);
78

9+
var element = document.querySelector('textarea');
10+
var statusSpan = document.getElementById('status-span');
11+
status.innerHTML = "Not Connected"
12+
13+
element.style.backgroundColor = "gray";
14+
socket.onopen = function(){
15+
status.innerHTML = "Connected"
16+
element.style.backgroundColor = "white";
17+
};
18+
19+
socket.onclose = function(){
20+
status.innerHTML = "Closed"
21+
element.style.backgroundColor = "gray";
22+
};
23+
24+
socket.onerror = function() {
25+
status.innerHTML = "Error"
26+
element.style.backgroundColor = "red";
27+
}
28+
829
// Create local Doc instance mapped to 'examples' collection document with id 'textarea'
930
var doc = connection.get('examples', 'textarea');
1031
doc.subscribe(function(err) {
1132
if (err) throw err;
12-
var element = document.querySelector('textarea');
33+
1334
var binding = new StringBinding(element, doc);
1435
binding.setup();
1536
});

examples/textarea/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"license": "MIT",
1616
"dependencies": {
1717
"express": "^4.14.0",
18+
"reconnecting-websocket": "^3.0.3",
1819
"sharedb": "^1.0.0-beta",
1920
"sharedb-string-binding": "^1.0.0",
2021
"websocket-json-stream": "^0.0.1",

examples/textarea/static/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!DOCTYPE html>
22
<meta charset="utf-8">
33
<title>ShareDB Textarea</title>
4+
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>
45
<style>
56
body {
67
background: #ddd;
@@ -19,9 +20,14 @@
1920
width: 100%;
2021
box-sizing: border-box;
2122
}
23+
#title {
24+
font-size: 24;
25+
}
2226
</style>
2327

2428
<div id="container">
29+
<h1 id="title">Text Area Example with Reconnecting Websockets</h1>
30+
<p>Connection Status: <span id="status-span"></span></p>
2531
<textarea autofocus></textarea>
2632
</div>
2733

0 commit comments

Comments
 (0)