Skip to content

Add forceWebSockets() and forceLongPolling() #6171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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
6 changes: 6 additions & 0 deletions .changeset/lucky-items-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@firebase/database-compat": patch
"@firebase/database": patch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an API change so I think these should both be "minor" bumps.

---

Add forceWebSockets() and forceLongPolling()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add backticks to the two methods?

6 changes: 6 additions & 0 deletions common/api-review/database.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ export function equalTo(value: number | string | boolean | null, key?: string):
// @public
export type EventType = 'value' | 'child_added' | 'child_changed' | 'child_moved' | 'child_removed';

// @public
export function forceLongPolling(): void;

// @public
export function forceWebSockets(): void;

// @public
export function get(query: Query): Promise<DataSnapshot>;

Expand Down
6 changes: 5 additions & 1 deletion packages/database-compat/src/api/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import { FirebaseApp } from '@firebase/app-types';
import { FirebaseService } from '@firebase/app-types/private';
import {
forceLongPolling,
forceWebSockets,
goOnline,
connectDatabaseEmulator,
goOffline,
Expand Down Expand Up @@ -51,7 +53,9 @@ export class Database implements FirebaseService, Compat<ModularDatabase> {
constructor(readonly _delegate: ModularDatabase, readonly app: FirebaseApp) {}

INTERNAL = {
delete: () => this._delegate._delete()
delete: () => this._delegate._delete(),
forceWebSockets,
forceLongPolling
};

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/database/src/api.standalone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export {
enableLogging,
goOffline,
goOnline,
forceWebSockets,
forceLongPolling,
connectDatabaseEmulator
} from './api/Database';
export {
Expand Down
29 changes: 29 additions & 0 deletions packages/database/src/api/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ import {
enableLogging as enableLoggingImpl
} from '../core/util/util';
import { validateUrl } from '../core/util/validation';
import { BrowserPollConnection } from '../realtime/BrowserPollConnection';
import { TransportManager } from '../realtime/TransportManager';
import { WebSocketConnection } from '../realtime/WebSocketConnection';

import { ReferenceImpl } from './Reference_impl';

Expand Down Expand Up @@ -271,6 +274,32 @@ export class Database implements _FirebaseService {
}
}

function checkTransportInit() {
if (TransportManager.IS_TRANSPORT_INITIALIZED) {
fatal(
'Transport has already been initialized. Please call this function before calling ref or setting up a listener'
);
}
}

/**
* Force the use of websockets instead of longPolling.
*
*/
export function forceWebSockets() {
checkTransportInit();
BrowserPollConnection.forceDisallow();
}

/**
* Force the use of longPolling instead of websockets. This will be ignored if websocket protocol is used in databaseURL.
*/
export function forceLongPolling() {
checkTransportInit();
WebSocketConnection.forceDisallow();
BrowserPollConnection.forceAllow();
}

/**
* Returns the instance of the Realtime Database SDK that is associated
* with the provided {@link @firebase/app#FirebaseApp}. Initializes a new instance with
Expand Down
4 changes: 2 additions & 2 deletions packages/database/src/realtime/BrowserPollConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export class BrowserPollConnection implements Transport {
this.addDisconnectPingFrame(this.id, this.password);
}

private static forceAllow_: boolean;
static forceAllow_: boolean;

/**
* Forces long polling to be considered as a potential transport
Expand All @@ -257,7 +257,7 @@ export class BrowserPollConnection implements Transport {
BrowserPollConnection.forceAllow_ = true;
}

private static forceDisallow_: boolean;
static forceDisallow_: boolean;

/**
* Forces longpolling to not be considered as a potential transport
Expand Down
7 changes: 7 additions & 0 deletions packages/database/src/realtime/TransportManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ import { WebSocketConnection } from './WebSocketConnection';
export class TransportManager {
private transports_: TransportConstructor[];

static globalTransportInitialized_ = false;

static get ALL_TRANSPORTS() {
return [BrowserPollConnection, WebSocketConnection];
}

static get IS_TRANSPORT_INITIALIZED() {
return this.globalTransportInitialized_;
}

/**
* @param repoInfo - Metadata around the namespace we're connecting to
*/
Expand Down Expand Up @@ -68,6 +74,7 @@ export class TransportManager {
transports.push(transport);
}
}
TransportManager.globalTransportInitialized_ = true;
}
}

Expand Down
63 changes: 63 additions & 0 deletions packages/database/test/transport.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* @license
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { CONSTANTS } from '@firebase/util';
import { expect } from 'chai';

import { forceLongPolling, forceWebSockets } from '../src';
import { BrowserPollConnection } from '../src/realtime/BrowserPollConnection';
import { TransportManager } from '../src/realtime/TransportManager';
import { WebSocketConnection } from '../src/realtime/WebSocketConnection';

const transportInitError =
'Transport has already been initialized. Please call this function before calling ref or setting up a listener';
describe('Force Transport', () => {
const oldNodeValue = CONSTANTS.NODE_CLIENT;
beforeEach(() => {
CONSTANTS.NODE_CLIENT = false;
});
afterEach(() => {
// Resetting to old values
TransportManager.globalTransportInitialized_ = false;
CONSTANTS.NODE_CLIENT = oldNodeValue;
BrowserPollConnection.forceAllow_ = false;
BrowserPollConnection.forceDisallow_ = true;
WebSocketConnection.forceDisallow_ = false;
});
it('should enable websockets and disable longPolling', () => {
expect(forceWebSockets).to.not.throw();
expect(WebSocketConnection.isAvailable()).to.equal(true);
expect(BrowserPollConnection.isAvailable()).to.equal(false);
});
it('should throw an error when calling forceWebsockets() if TransportManager has already been initialized', () => {
TransportManager.globalTransportInitialized_ = true;
expect(forceWebSockets).to.throw(transportInitError);
expect(WebSocketConnection.isAvailable()).to.equal(true);
expect(BrowserPollConnection.isAvailable()).to.equal(false);
});
it('should enable longPolling and disable websockets', () => {
expect(forceLongPolling).to.not.throw();
expect(WebSocketConnection.isAvailable()).to.equal(false);
expect(BrowserPollConnection.isAvailable()).to.equal(true);
});
it('should throw an error when calling forceLongPolling() if TransportManager has already been initialized', () => {
TransportManager.globalTransportInitialized_ = true;
expect(forceLongPolling).to.throw(transportInitError);
expect(WebSocketConnection.isAvailable()).to.equal(true);
expect(BrowserPollConnection.isAvailable()).to.equal(false);
});
});