Skip to content

Commit d4f2dd0

Browse files
jpcamaraeliias
authored andcommitted
feat(websocket-provider): Utilizes whisper (from anycable) for awareness updates, when available
1 parent 6742df4 commit d4f2dd0

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

packages/yrb-actioncable/src/websocket-provider.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ enum MessageType {
4747

4848
type MessageHandlers = Record<MessageType, MessageHandler>;
4949

50+
interface SubscriptionWithWhisper extends ActionCable.Channel {
51+
channel: {
52+
whisper(data: any): boolean;
53+
};
54+
}
55+
5056
const permissionDeniedHandler = (
5157
provider: WebsocketProvider,
5258
reason: string
@@ -184,7 +190,7 @@ export class WebsocketProvider {
184190
encoder,
185191
encodeAwarenessUpdate(this.awareness, changedClients)
186192
);
187-
this.send(toUint8Array(encoder));
193+
this.send(toUint8Array(encoder), { whisper: true });
188194
};
189195

190196
get synced() {
@@ -209,9 +215,14 @@ export class WebsocketProvider {
209215
this.doc.off('update', this.updateHandler);
210216
}
211217

212-
private send(buffer: Uint8Array) {
218+
private send(buffer: Uint8Array, { whisper = false }: { whisper?: boolean } = {}) {
213219
const update = encodeBinaryToBase64(buffer);
214-
this.channel?.send({ update });
220+
221+
if (whisper && hasWhisper(this.channel)) {
222+
this.channel.channel.whisper({ update });
223+
} else {
224+
this.channel?.send({ update });
225+
}
215226

216227
if (this.bcconnected) {
217228
publish(this.bcChannelName, buffer, this);
@@ -356,3 +367,14 @@ function encodeBinaryToBase64(bin: Uint8Array) {
356367
function decodeBase64ToBinary(update: string) {
357368
return Uint8Array.from(atob(update), c => c.charCodeAt(0));
358369
}
370+
371+
function hasWhisper(channel: ActionCable.Channel | undefined): channel is SubscriptionWithWhisper {
372+
return (
373+
channel !== undefined &&
374+
'channel' in channel &&
375+
channel.channel !== null &&
376+
typeof channel.channel === 'object' &&
377+
'whisper' in channel.channel &&
378+
typeof channel.channel.whisper === 'function'
379+
);
380+
}

0 commit comments

Comments
 (0)