Skip to content

Commit 0200431

Browse files
committed
feat: add enableAwareness option to API and SocketIO server
1 parent 430620a commit 0200431

File tree

4 files changed

+39
-17
lines changed

4 files changed

+39
-17
lines changed

src/api.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ const decodeRedisRoomStreamName = (rediskey, expectedPrefix) => {
8383

8484
/**
8585
* @param {import('./storage.js').AbstractStorage} store
86-
* @param {{ redisPrefix?: string, redisUrl?: string }} opts
86+
* @param {{ redisPrefix?: string, redisUrl?: string, enableAwareness?: boolean }} opts
8787
*/
88-
export const createApiClient = async (store, { redisPrefix, redisUrl }) => {
89-
const a = new Api(store, redisPrefix, redisUrl)
88+
export const createApiClient = async (store, { redisPrefix, redisUrl, enableAwareness = true }) => {
89+
const a = new Api(store, redisPrefix, redisUrl, { enableAwareness })
9090
await a.redis.connect()
9191
try {
9292
await a.redis.xGroupCreate(a.redisWorkerStreamName, a.redisWorkerGroupName, '0', { MKSTREAM: true })
@@ -99,10 +99,13 @@ export class Api {
9999
* @param {import('./storage.js').AbstractStorage} store
100100
* @param {string=} prefix
101101
* @param {string=} url
102+
* @param {Object} opts
103+
* @param {boolean=} opts.enableAwareness
102104
*/
103-
constructor (store, prefix = 'y', url = env.ensureConf('ysr-redis')) {
105+
constructor (store, prefix = 'y', url = env.ensureConf('ysr-redis'), { enableAwareness = true } = {}) {
104106
this.store = store
105107
this.prefix = prefix
108+
this.enableAwareness = enableAwareness
106109
this.consumername = random.uuidv4()
107110
/**
108111
* After this timeout, a new worker will pick up the task
@@ -232,8 +235,11 @@ export class Api {
232235
if (docMessages?.messages) logApi(`processing messages of length: ${docMessages?.messages.length} in room: ${room}`)
233236
const docstate = await this.store.retrieveDoc(room, docid)
234237
const ydoc = new Y.Doc()
235-
const awareness = new awarenessProtocol.Awareness(ydoc)
236-
awareness.setLocalState(null) // we don't want to propagate awareness state
238+
let awareness = null
239+
if (this.enableAwareness) {
240+
awareness = new awarenessProtocol.Awareness(ydoc)
241+
awareness.setLocalState(null) // we don't want to propagate awareness state
242+
}
237243
const now = performance.now()
238244
if (docstate) { Y.applyUpdateV2(ydoc, docstate.doc) }
239245
let changed = false
@@ -249,7 +255,9 @@ export class Api {
249255
break
250256
}
251257
case 1: { // awareness message
252-
awarenessProtocol.applyAwarenessUpdate(awareness, decoding.readVarUint8Array(decoder), null)
258+
if (this.enableAwareness && awareness) {
259+
awarenessProtocol.applyAwarenessUpdate(awareness, decoding.readVarUint8Array(decoder), null)
260+
}
253261
break
254262
}
255263
}
@@ -355,7 +363,7 @@ export class Api {
355363

356364
/**
357365
* @param {import('./storage.js').AbstractStorage} store
358-
* @param {{ redisPrefix?: string, redisUrl?: string }} opts
366+
* @param {{ redisPrefix?: string, redisUrl?: string, enableAwareness?: boolean }} opts
359367
*/
360368
export const createWorker = async (store, opts) => {
361369
const a = await createApiClient(store, opts)

src/socketio.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,15 @@ class YSocketIOServer {
3737
* @param {string} [conf.redisPrefix]
3838
* @param {string} [conf.redisUrl]
3939
* @param {import('./y-socket-io/y-socket-io.js').YSocketIOConfiguration['authenticate']} conf.authenticate
40+
* @param {boolean} [conf.enableAwareness]
4041
*/
41-
export const registerYSocketIOServer = async (io, store, { authenticate, redisUrl, redisPrefix }) => {
42-
const app = new YSocketIO(io, { authenticate })
42+
export const registerYSocketIOServer = async (io, store, {
43+
authenticate,
44+
redisUrl,
45+
redisPrefix,
46+
enableAwareness = true
47+
}) => {
48+
const app = new YSocketIO(io, { authenticate, enableAwareness })
4349
const { client, subscriber } = await app.initialize(store, { redisUrl, redisPrefix })
4450
return new YSocketIOServer(app, client, subscriber)
4551
}

src/subscriber.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const run = async subscriber => {
3232

3333
/**
3434
* @param {import('./storage.js').AbstractStorage} store
35-
* @param {{ redisPrefix?: string, redisUrl?: string }} opts
35+
* @param {{ redisPrefix?: string, redisUrl?: string, enableAwareness?: boolean }} opts
3636
*/
3737
export const createSubscriber = async (store, opts) => {
3838
const client = await api.createApiClient(store, opts)

src/y-socket-io/y-socket-io.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import { createSubscriber } from '../subscriber.js'
3838
*
3939
* @prop {(socket: Socket)=> Promise<UserLike | null> | UserLike | null} authenticate
4040
* Callback to authenticate the client connection.
41+
* @prop {boolean=} enableAwareness
42+
* Enable/disable awareness functionality, defaults to true
4143
*/
4244

4345
/**
@@ -98,7 +100,10 @@ export class YSocketIO {
98100
*/
99101
constructor (io, configuration) {
100102
this.io = io
101-
this.configuration = configuration
103+
this.configuration = {
104+
enableAwareness: true,
105+
...configuration
106+
}
102107
}
103108

104109
/**
@@ -113,9 +118,10 @@ export class YSocketIO {
113118
* @public
114119
*/
115120
async initialize (store, { redisUrl, redisPrefix = 'y' } = {}) {
121+
const { enableAwareness } = this.configuration
116122
const [client, subscriber] = await promise.all([
117-
api.createApiClient(store, { redisUrl, redisPrefix }),
118-
createSubscriber(store, { redisUrl, redisPrefix })
123+
api.createApiClient(store, { redisUrl, redisPrefix, enableAwareness }),
124+
createSubscriber(store, { redisUrl, redisPrefix, enableAwareness })
119125
])
120126
this.client = client
121127
this.subscriber = subscriber
@@ -154,7 +160,9 @@ export class YSocketIO {
154160
this.streamNamespaceMap.set(stream, namespace)
155161

156162
this.initSyncListeners(socket)
157-
this.initAwarenessListeners(socket)
163+
if (this.configuration.enableAwareness) {
164+
this.initAwarenessListeners(socket)
165+
}
158166
this.initSocketListeners(socket)
159167

160168
const doc = await this.client.getDoc(namespace, 'index')
@@ -287,7 +295,7 @@ export class YSocketIO {
287295
).catch(console.error)
288296
}
289297
)
290-
if (doc.awareness.states.size > 0) {
298+
if (this.configuration.enableAwareness && doc.awareness.states.size > 0) {
291299
socket.emit(
292300
'awareness-update',
293301
AwarenessProtocol.encodeAwarenessUpdate(
@@ -322,7 +330,7 @@ export class YSocketIO {
322330

323331
for (const m of messages) {
324332
const decoded = this.fromRedis(m)
325-
if (decoded.type === 'awareness-update') awareness.push(decoded.message)
333+
if (decoded.type === 'awareness-update' && this.configuration.enableAwareness) awareness.push(decoded.message)
326334
else updates.push(decoded.message)
327335
}
328336

0 commit comments

Comments
 (0)