Skip to content
This repository was archived by the owner on Dec 18, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/realtime/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class RealtimeAsset extends Events {
unload() {
if (!this._document) return;

this._document.unsubscribe();
this._document.destroy();
this._document = null;
this._loaded = false;
Expand Down
25 changes: 19 additions & 6 deletions src/realtime/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ class RealtimeConnection extends Events {
onmessage.call(socket, msg);
};

// intercept send to queue and forward messages
const send = socket.send;
socket.send = (data: string) => {
// if not open use latest send
if (socket.readyState !== WebSocket.OPEN) {
this.send(data);
return;
}
send.call(socket, data);
};

// allow sending messages
this._active.resolve(socket);

Expand Down Expand Up @@ -144,25 +155,27 @@ class RealtimeConnection extends Events {
// create new socket
const socket = new WebSocket(url);

socket.onopen = () => {
socket.addEventListener('open', () => {
this._connected = true;
this._reconnectAttempts = 0;

socket.send(`auth${JSON.stringify({ accessToken: api.accessToken })}`);

this._realtime.emit('connected');
};
});

socket.onmessage = (msg) => {
const onmessage = (msg: Parameters<WebSocket['onmessage']>[0]) => {
if (msg.data.toString().startsWith('auth')) {
// clear this handler
socket.onmessage = null;
socket.removeEventListener('message', onmessage);

this._onauth(socket);
}
};
socket.addEventListener('message', onmessage);

socket.onclose = (reason) => {
// ! use event listener as sharedb overrides socket.on* handlers
socket.addEventListener('close', (reason) => {
// block sending messages
this._active = new Deferred<WebSocket>();

Expand All @@ -184,7 +197,7 @@ class RealtimeConnection extends Events {
this.connect(this._url);
}, this._reconnectInterval * 1000);
}
};
});

document.addEventListener('visibilitychange', this._domEvtVisibilityChange);

Expand Down
1 change: 0 additions & 1 deletion src/realtime/scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class RealtimeScene extends Events {
unload() {
if (!this._document) return;

this._document.unsubscribe();
this._document.destroy();
this._document = null;
this._loaded = false;
Expand Down