Skip to content

Commit

Permalink
Redis for local
Browse files Browse the repository at this point in the history
  • Loading branch information
oklemenz2 committed Jan 15, 2024
1 parent 40f6ccf commit 8e52544
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 50 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,11 @@ The following adapters for WS Standard are supported out-of-the-box.
To use the Redis Adapter (basic publish/subscribe), the following steps have to be performed:

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

Expand Down
71 changes: 36 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"cookie": "^0.6.0",
"express": "^4.18.2",
"redis": "^4.6.11",
"socket.io": "^4.7.3",
"socket.io": "^4.7.4",
"ws": "^8.15.1"
},
"devDependencies": {
Expand All @@ -59,11 +59,11 @@
"@types/express": "^4.17.21",
"eslint": "^8.54.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-jest": "^27.6.2",
"eslint-plugin-jest": "^27.6.3",
"jest": "^29.7.0",
"passport": "0.7.0",
"prettier": "^3.1.1",
"socket.io-client": "^4.7.3"
"prettier": "^3.2.2",
"socket.io-client": "^4.7.4"
},
"license": "Apache-2.0",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function serveWebSocketServer(options) {
serveWebSocketService(socketServer, eventService, options);
}
}
LOG?.info("using websocket", { kind: cds.env.requires.websocket.kind });
LOG?.info("using websocket", { kind: cds.env.requires.websocket.kind, adapter: socketServer.adapterActive });
}
});
}
Expand Down
7 changes: 7 additions & 0 deletions src/redis/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const LOG = cds.log("websocket/redis");

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

const IS_ON_CF = process.env.USER === "vcap";
const TIMEOUT = 5 * 1000;

let primaryClientPromise;
Expand Down Expand Up @@ -42,13 +43,19 @@ const createSecondaryClientAndConnect = () => {
return secondaryClientPromise;
};
const _createClientBase = () => {
const adapterLocal = !!cds.env.requires?.websocket?.adapter?.local;
if (!(IS_ON_CF || adapterLocal)) {
LOG?.info("Redis not available in local environment");
return;
}
let credentials;
try {
credentials = xsenv.serviceCredentials({ label: "redis-cache" });
} catch (err) {
LOG?.info(err.message);
}
if (!credentials) {
LOG?.info("No Redis credentials found");
return;
}
try {
Expand Down
2 changes: 2 additions & 0 deletions src/socket/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class SocketServer {
this.id = crypto.randomUUID();
this.server = server;
this.path = path;
this.adapter = null;
this.adapterActive = false;
cds.ws = null;
}

Expand Down
10 changes: 5 additions & 5 deletions src/socket/socket.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,25 @@ class SocketIOServer extends SocketServer {
}
let client;
let subClient;
let adapter;
const adapterFactory = require(adapterImpl);
switch (adapterImpl) {
case "@socket.io/redis-adapter":
client = await redis.createPrimaryClientAndConnect();
subClient = await redis.createSecondaryClientAndConnect();
if (client && subClient) {
adapter = adapterFactory.createAdapter(client, subClient, options);
this.adapter = adapterFactory.createAdapter(client, subClient, options);
}
break;
case "@socket.io/redis-streams-adapter":
client = await redis.createPrimaryClientAndConnect();
if (client) {
adapter = adapterFactory.createAdapter(client, options);
this.adapter = adapterFactory.createAdapter(client, options);
}
break;
}
if (adapter) {
this.io.adapter(adapter);
if (this.adapter) {
this.io.adapter(this.adapter);
this.adapterActive = true;
}
}
} catch (err) {
Expand Down
6 changes: 3 additions & 3 deletions src/socket/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class SocketWSServer extends SocketServer {
this.wss = new WebSocket.Server({ server });
cds.ws = this.wss;
cds.wss = this.wss;
this.adapter = null;
}

async setup() {
Expand Down Expand Up @@ -123,9 +122,10 @@ class SocketWSServer extends SocketServer {
options = { ...options, ...cds.env.requires.websocket?.adapter?.options };
}
const prefix = options?.key ?? "websocket";
this.adapterFactory = require(`../adapter/${adapterImpl}`);
this.adapter = new this.adapterFactory(this, prefix, options);
const adapterFactory = require(`../adapter/${adapterImpl}`);
this.adapter = new adapterFactory(this, prefix, options);
await this.adapter?.setup();
this.adapterActive = !!this.adapter?.client;
}
} catch (err) {
LOG?.error(err);
Expand Down
1 change: 1 addition & 0 deletions test/_env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"kind": "ws",
"adapter": {
"impl": "redis",
"local": true,
"options": {
"key": "websocket"
}
Expand Down

0 comments on commit 8e52544

Please sign in to comment.