Skip to content

Commit 8e52544

Browse files
committed
Redis for local
1 parent 40f6ccf commit 8e52544

File tree

9 files changed

+64
-50
lines changed

9 files changed

+64
-50
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,11 @@ The following adapters for WS Standard are supported out-of-the-box.
405405
To use the Redis Adapter (basic publish/subscribe), the following steps have to be performed:
406406

407407
- Set `cds.requires.websocket.adapter.impl: "redis"`
408-
- Application needs to be bound to a Redis instance
409-
- Locally a `default-env.json` file need to exist with index configuration
408+
- Application needs to be bound to a Redis instance
409+
- Cloud Foundry: Redis automatically active
410+
- Local (or other):
411+
- Option `cds.requires.websocket.adapter.local: true` needs to be set
412+
- File `default-env.json` need to exist with Redis configuration
410413
- Redis Adapter options can be specified via `cds.requires.websocket.adapter.options`
411414
- Redis channel key can be specified via `cds.requires.websocket.adapter.options.key`. Default value is `websocket`.
412415

package-lock.json

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

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"cookie": "^0.6.0",
4646
"express": "^4.18.2",
4747
"redis": "^4.6.11",
48-
"socket.io": "^4.7.3",
48+
"socket.io": "^4.7.4",
4949
"ws": "^8.15.1"
5050
},
5151
"devDependencies": {
@@ -59,11 +59,11 @@
5959
"@types/express": "^4.17.21",
6060
"eslint": "^8.54.0",
6161
"eslint-config-prettier": "^9.0.0",
62-
"eslint-plugin-jest": "^27.6.2",
62+
"eslint-plugin-jest": "^27.6.3",
6363
"jest": "^29.7.0",
6464
"passport": "0.7.0",
65-
"prettier": "^3.1.1",
66-
"socket.io-client": "^4.7.3"
65+
"prettier": "^3.2.2",
66+
"socket.io-client": "^4.7.4"
6767
},
6868
"license": "Apache-2.0",
6969
"repository": {

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function serveWebSocketServer(options) {
7777
serveWebSocketService(socketServer, eventService, options);
7878
}
7979
}
80-
LOG?.info("using websocket", { kind: cds.env.requires.websocket.kind });
80+
LOG?.info("using websocket", { kind: cds.env.requires.websocket.kind, adapter: socketServer.adapterActive });
8181
}
8282
});
8383
}

src/redis/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const LOG = cds.log("websocket/redis");
99

1010
xsenv.loadEnv(path.join(process.cwd(), "default-env.json"));
1111

12+
const IS_ON_CF = process.env.USER === "vcap";
1213
const TIMEOUT = 5 * 1000;
1314

1415
let primaryClientPromise;
@@ -42,13 +43,19 @@ const createSecondaryClientAndConnect = () => {
4243
return secondaryClientPromise;
4344
};
4445
const _createClientBase = () => {
46+
const adapterLocal = !!cds.env.requires?.websocket?.adapter?.local;
47+
if (!(IS_ON_CF || adapterLocal)) {
48+
LOG?.info("Redis not available in local environment");
49+
return;
50+
}
4551
let credentials;
4652
try {
4753
credentials = xsenv.serviceCredentials({ label: "redis-cache" });
4854
} catch (err) {
4955
LOG?.info(err.message);
5056
}
5157
if (!credentials) {
58+
LOG?.info("No Redis credentials found");
5259
return;
5360
}
5461
try {

src/socket/base.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class SocketServer {
1717
this.id = crypto.randomUUID();
1818
this.server = server;
1919
this.path = path;
20+
this.adapter = null;
21+
this.adapterActive = false;
2022
cds.ws = null;
2123
}
2224

src/socket/socket.io.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,25 @@ class SocketIOServer extends SocketServer {
8989
}
9090
let client;
9191
let subClient;
92-
let adapter;
9392
const adapterFactory = require(adapterImpl);
9493
switch (adapterImpl) {
9594
case "@socket.io/redis-adapter":
9695
client = await redis.createPrimaryClientAndConnect();
9796
subClient = await redis.createSecondaryClientAndConnect();
9897
if (client && subClient) {
99-
adapter = adapterFactory.createAdapter(client, subClient, options);
98+
this.adapter = adapterFactory.createAdapter(client, subClient, options);
10099
}
101100
break;
102101
case "@socket.io/redis-streams-adapter":
103102
client = await redis.createPrimaryClientAndConnect();
104103
if (client) {
105-
adapter = adapterFactory.createAdapter(client, options);
104+
this.adapter = adapterFactory.createAdapter(client, options);
106105
}
107106
break;
108107
}
109-
if (adapter) {
110-
this.io.adapter(adapter);
108+
if (this.adapter) {
109+
this.io.adapter(this.adapter);
110+
this.adapterActive = true;
111111
}
112112
}
113113
} catch (err) {

src/socket/ws.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class SocketWSServer extends SocketServer {
1313
this.wss = new WebSocket.Server({ server });
1414
cds.ws = this.wss;
1515
cds.wss = this.wss;
16-
this.adapter = null;
1716
}
1817

1918
async setup() {
@@ -123,9 +122,10 @@ class SocketWSServer extends SocketServer {
123122
options = { ...options, ...cds.env.requires.websocket?.adapter?.options };
124123
}
125124
const prefix = options?.key ?? "websocket";
126-
this.adapterFactory = require(`../adapter/${adapterImpl}`);
127-
this.adapter = new this.adapterFactory(this, prefix, options);
125+
const adapterFactory = require(`../adapter/${adapterImpl}`);
126+
this.adapter = new adapterFactory(this, prefix, options);
128127
await this.adapter?.setup();
128+
this.adapterActive = !!this.adapter?.client;
129129
}
130130
} catch (err) {
131131
LOG?.error(err);

test/_env/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"kind": "ws",
3434
"adapter": {
3535
"impl": "redis",
36+
"local": true,
3637
"options": {
3738
"key": "websocket"
3839
}

0 commit comments

Comments
 (0)