forked from share/sharedb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
31 lines (25 loc) · 1.09 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var ReconnectingWebSocket = require('reconnecting-websocket');
var sharedb = require('sharedb/lib/client');
// Open WebSocket connection to ShareDB server
var socket = new ReconnectingWebSocket('ws://' + window.location.host);
var connection = new sharedb.Connection(socket);
// Create local Doc instance mapped to 'examples' collection document with id 'counter'
var doc = connection.get('examples', 'counter');
// Get initial value of document and subscribe to changes
doc.subscribe(showNumbers);
// When document changes (by this client or any other, or the server),
// update the number on the page
doc.on('op', showNumbers);
function showNumbers() {
document.querySelector('#num-clicks').textContent = doc.data.numClicks;
};
// When clicking on the '+1' button, change the number in the local
// document and sync the change to the server and other connected
// clients
function increment() {
// Increment `doc.data.numClicks`. See
// https://github.com/ottypes/json0 for list of valid operations.
doc.submitOp([{p: ['numClicks'], na: 1}]);
}
// Expose to index.html
global.increment = increment;