Skip to content

Commit e1e7ee2

Browse files
committed
Added applicationid to websocket connection
1 parent 9c6808f commit e1e7ee2

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

packages/database/src/realtime/WebSocketConnection.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { logWrapper, splitStringBySize } from '../core/util/util';
2525
import { SDK_VERSION } from '../core/version';
2626

2727
import {
28+
APPLICATION_ID_PARAM,
2829
APP_CHECK_TOKEN_PARAM,
2930
FORGE_DOMAIN_RE,
3031
FORGE_REF,
@@ -99,7 +100,8 @@ export class WebSocketConnection implements Transport {
99100
repoInfo,
100101
transportSessionId,
101102
lastSessionId,
102-
appCheckToken
103+
appCheckToken,
104+
applicationId
103105
);
104106
this.nodeAdmin = repoInfo.nodeAdmin;
105107
}
@@ -115,7 +117,8 @@ export class WebSocketConnection implements Transport {
115117
repoInfo: RepoInfo,
116118
transportSessionId?: string,
117119
lastSessionId?: string,
118-
appCheckToken?: string
120+
appCheckToken?: string,
121+
applicationId?: string
119122
): string {
120123
const urlParams: { [k: string]: string } = {};
121124
urlParams[VERSION_PARAM] = PROTOCOL_VERSION;
@@ -137,6 +140,9 @@ export class WebSocketConnection implements Transport {
137140
if (appCheckToken) {
138141
urlParams[APP_CHECK_TOKEN_PARAM] = appCheckToken;
139142
}
143+
if(applicationId) {
144+
urlParams[APPLICATION_ID_PARAM] = applicationId;
145+
}
140146

141147
return repoInfoConnectionURL(repoInfo, WEBSOCKET, urlParams);
142148
}
@@ -156,6 +162,7 @@ export class WebSocketConnection implements Transport {
156162
PersistentStorage.set('previous_websocket_failure', true);
157163

158164
try {
165+
let options: { [k: string]: object };
159166
if (isNodeSdk()) {
160167
const device = this.nodeAdmin ? 'AdminNode' : 'Node';
161168
// UA Format: Firebase/<wire_protocol>/<sdk_version>/<platform>/<device>
@@ -189,16 +196,8 @@ export class WebSocketConnection implements Transport {
189196
options['proxy'] = { origin: proxy };
190197
}
191198

192-
this.mySock = new WebSocketImpl(this.connURL, [], options);
193-
} else {
194-
const options: { [k: string]: object } = {
195-
headers: {
196-
'X-Firebase-GMPID': this.applicationId || '',
197-
'X-Firebase-AppCheck': this.appCheckToken || ''
198-
}
199-
};
200-
this.mySock = new WebSocketImpl(this.connURL, [], options);
201199
}
200+
this.mySock = new WebSocketImpl(this.connURL, [], options);
202201
} catch (e) {
203202
this.log_('Error instantiating WebSocket.');
204203
const error = e.message || e.data;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { expect } from "chai";
2+
3+
import { APPLICATION_ID_PARAM } from "../src/realtime/Constants";
4+
import { WebSocketConnection } from "../src/realtime/WebSocketConnection";
5+
6+
import { testRepoInfo } from "./helpers/util";
7+
8+
describe('WebSocketConnection', () => {
9+
it('should add an applicationId to the query parameter', () => {
10+
const repoInfo = testRepoInfo('https://test-ns.firebaseio.com');
11+
const applicationId = 'myID';
12+
const websocketConnection = new WebSocketConnection('connId', repoInfo, applicationId);
13+
const searchParams = new URL(websocketConnection.connURL).searchParams;
14+
expect(searchParams.get(APPLICATION_ID_PARAM)).to.equal(applicationId);
15+
});
16+
it('should not add an applicationId to the query parameter if applicationId is empty', () => {
17+
const repoInfo = testRepoInfo('https://test-ns.firebaseio.com');
18+
const applicationId = '';
19+
const websocketConnection = new WebSocketConnection('connId', repoInfo, applicationId);
20+
const searchParams = new URL(websocketConnection.connURL).searchParams;
21+
expect(searchParams.get(APPLICATION_ID_PARAM)).to.be.null;
22+
});
23+
});

0 commit comments

Comments
 (0)