Skip to content

Commit 274b245

Browse files
authored
- add an ability to use RT with custom domain (#220)
1 parent 4ad07b4 commit 274b245

File tree

5 files changed

+325
-116
lines changed

5 files changed

+325
-116
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,6 @@
9292
"dependencies": {
9393
"@babel/runtime": "^7.14.6",
9494
"backendless-request": "^0.1.1",
95-
"backendless-rt-client": "0.0.24"
95+
"backendless-rt-client": "0.0.25"
9696
}
9797
}

src/rt.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import Request from 'backendless-request'
12
import BackendlessRTClient from 'backendless-rt-client'
23

34
import Utils from './utils'
45

56
export const RTListeners = BackendlessRTClient.Listeners
67
export const RTScopeConnector = BackendlessRTClient.ScopeConnector
78

9+
function loadAppInfo(appPath) {
10+
return Request.get(`${appPath}/info`)
11+
}
12+
813
export default class RT extends BackendlessRTClient {
914
constructor(app) {
1015
const { applicationId: appId, secretKey: apiKey, appPath, debugMode } = app
@@ -13,17 +18,29 @@ export default class RT extends BackendlessRTClient {
1318
const lookupPath = `${appPath}/rt/lookup`
1419

1520
super({
16-
appId,
21+
appId: appId || undefined,
1722
lookupPath,
1823
debugMode,
1924
connectQuery() {
2025
const userToken = app.getCurrentUserToken()
2126

2227
return {
23-
apiKey,
28+
apiKey: apiKey || undefined,
2429
clientId,
2530
userToken,
2631
}
32+
},
33+
34+
socketConfigTransform: async socketConfig => {
35+
if (!appId) {
36+
const appInfo = await loadAppInfo(appPath)
37+
38+
socketConfig.url = `${socketConfig.host}/${appInfo.appId}`
39+
socketConfig.options.path = `/${appInfo.appId}`
40+
socketConfig.options.query.apiKey = appInfo.apiKey
41+
}
42+
43+
return socketConfig
2744
}
2845
})
2946

test/unit/helpers/rt.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ export const RTMethodTypes = {
4343
PUB_SUB_COMMAND: 'PUB_SUB_COMMAND',
4444
}
4545

46-
export function createMockRTServer() {
46+
export function createMockRTServer(appId = APP_ID) {
4747
const server = require('http').createServer()
4848

49-
const io = require('socket.io')({ path: `/${APP_ID}` })
49+
const io = require('socket.io')({ path: `/${appId}` })
5050

5151
io.attach(server, {
5252
pingInterval: 10000,
@@ -86,7 +86,7 @@ export function createMockRTServer() {
8686

8787
let socket
8888

89-
io.of(`/${APP_ID}`).on('connection', (s) => {
89+
io.of(`/${appId}`).on('connection', (s) => {
9090
socket = s
9191

9292
const { apiKey, clientId, userToken } = socket.handshake.query

0 commit comments

Comments
 (0)