Skip to content

Commit 9e19efb

Browse files
author
Alec Gibson
committed
Add a presence feature flag
This change adds a presence feature flag, so that consumers have to actively opt in to use presence. This means that servers that don't want to worry about potential fan-out issues don't need to worry about it. Note that the feature flag will disable presence subscription and also disable presence broadcasting, but notably _does not_ disable unsubscribing from presence, so that clients may unsubscribe if the flag is toggled while the server is running.
1 parent 55dcbe4 commit 9e19efb

File tree

6 files changed

+8
-3
lines changed

6 files changed

+8
-3
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ __Options__
9595
through this pub/sub adapter. Defaults to `ShareDB.MemoryPubSub()`.
9696
* `options.milestoneDb` _(instance of ShareDB.MilestoneDB`)_
9797
Store snapshots of documents at a specified interval of versions
98+
* `options.presence` _boolean_
99+
Enable presence functionality. Off by default. Note that this feature is not optimized for large numbers of clients and could cause fan-out issues
98100

99101
#### Database Adapters
100102
* `ShareDB.MemoryDB`, backed by a non-persistent database with no queries

examples/rich-text-presence/server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var WebSocket = require('ws');
66
var WebSocketJSONStream = require('@teamwork/websocket-json-stream');
77

88
ShareDB.types.register(richText.type);
9-
var backend = new ShareDB();
9+
var backend = new ShareDB({presence: true});
1010
createDoc(startServer);
1111

1212
// Create initial document then fire callback

lib/agent.js

+2
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ Agent.prototype._handleMessage = function(request, callback) {
420420
case 'nt':
421421
return this._fetchSnapshotByTimestamp(request.c, request.d, request.ts, callback);
422422
case 'p':
423+
if (!this.backend.presenceEnabled) return;
423424
var presence = this._createPresence(request);
424425
if (presence.t && !util.supportsPresence(types.map[presence.t])) {
425426
return callback({
@@ -429,6 +430,7 @@ Agent.prototype._handleMessage = function(request, callback) {
429430
}
430431
return this._broadcastPresence(presence, callback);
431432
case 'ps':
433+
if (!this.backend.presenceEnabled) return;
432434
return this._subscribePresence(request.ch, request.seq, callback);
433435
case 'pu':
434436
return this._unsubscribePresence(request.ch, request.seq, callback);

lib/backend.js

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function Backend(options) {
3232

3333
this.suppressPublish = !!options.suppressPublish;
3434
this.maxSubmitRetries = options.maxSubmitRetries || null;
35+
this.presenceEnabled = !!options.presence;
3536

3637
// Map from event name to a list of middleware
3738
this.middleware = {};

test/client/presence/doc-presence.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('DocPresence', function() {
1818
var presencePauser;
1919

2020
beforeEach(function(done) {
21-
backend = new Backend();
21+
backend = new Backend({presence: true});
2222
connection1 = backend.connect();
2323
connection2 = backend.connect();
2424

test/client/presence/presence.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('Presence', function() {
1414
var presencePauser;
1515

1616
beforeEach(function(done) {
17-
backend = new Backend();
17+
backend = new Backend({presence: true});
1818
var connectedCount = 0;
1919
connection1 = backend.connect();
2020
connection2 = backend.connect();

0 commit comments

Comments
 (0)