When attempting to initialize a Redis-backed WebSocket adapter with the following lines:
const redisIoAdapter = new RedisIoAdapter(app);
await redisIoAdapter.connectToRedis();
app.useWebSocketAdapter(redisIoAdapter);
the application hangs and does not complete execution.
So I was wondering why that can happen, so I tried to put try catch around this, hoping to see an error, but it never showed any.
try {
await redisIoAdapter.connectToRedis();
} catch (error) {
console.error('Failed to connect to Redis:', error);
process.exit(1);
}
Any help on this issue would be greatly appreciated.
It just stops executing my NestJS project. Basically stops at this part:
[2:43:37 PM] File change detected. Starting incremental compilation...
[2:43:38 PM] Found 0 errors. Watching for file changes.
is that the RedisIoAdapter instance redisIoAdapter should establish a successful connection to the Redis server through the connectToRedis method. If the connection is successful, the Redis server would then act as a backing store for WebSocket connections in my application.
Then, the useWebSocketAdapter method sets the redisIoAdapter as the adapter for handling WebSocket connections in my NestJS application. This means that the NestJS application would use this adapter to manage all WebSocket connections, including broadcasting messages to clients.
In the context of a running application, this would mean that after these lines of code are executed, the application should be able to handle WebSocket connections and broadcast messages to clients using Redis as a store. This would be crucial since I need this application compatible with load-balancers. And after many many research I found out this could be the right solution to this. If a client makes a WebSocket connection request to the server, the server should be able to accept the connection, handle any messages sent over the connection, and also broadcast messages to multiple clients if needed.
Since the application hangs or does not complete execution, it indicates that there is a problem with setting up the WebSocket adapter.
The link I provided in "steps to reproduce" is basically the same as what I have, I just copy pasted them and I have modified the redis link. VSCode did not show any errors, so technically there shouldn't be any problem.
Most probably the redis URL I pass has no problems, since if I provide it badly an error appears in the console telling me time out or incorrect url. The link contains the password and the redis user as well.
I would point out that if I try as of how digitalocean suggests me this link, so rediss instead of redis it just times out, so I modified it in my code to have redis:// etc...
I attached you my main.ts file here. (renamed to txt because of github limitation)
main.txt
Is there an existing issue for this?
Current behavior
When attempting to initialize a Redis-backed WebSocket adapter with the following lines:
the application hangs and does not complete execution. So I was wondering why that can happen, so I tried to put try catch around this, hoping to see an error, but it never showed any.
Any help on this issue would be greatly appreciated.
It just stops executing my NestJS project. Basically stops at this part: [2:43:37 PM] File change detected. Starting incremental compilation...
[2:43:38 PM] Found 0 errors. Watching for file changes.
[Nest] 231 - 07/20/2023, 2:43:39 PM LOG [NestFactory] Starting Nest application... [Nest] 231 - 07/20/2023, 2:43:39 PM LOG [InstanceLoader] EventsModule dependencies initialized +38ms [Nest] 231 - 07/20/2023, 2:43:39 PM LOG [InstanceLoader] ThrottlerModule dependencies initialized +0ms [Nest] 231 - 07/20/2023, 2:43:39 PM LOG [InstanceLoader] ConfigHostModule dependencies initialized +0ms [Nest] 231 - 07/20/2023, 2:43:39 PM LOG [InstanceLoader] ConfigModule dependencies initialized +1ms [Nest] 231 - 07/20/2023, 2:43:39 PM LOG [InstanceLoader] AppModule dependencies initialized +1ms
Minimum reproduction code
https://github.com/nestjs/nest/tree/master/sample/02-gateways
Steps to reproduce
Just basically insert these files to a NestJS project, which is built on Express. https://github.com/nestjs/nest/tree/master/sample/02-gateways
Expected behavior
The expected behavior when running these two lines of code:
is that the RedisIoAdapter instance redisIoAdapter should establish a successful connection to the Redis server through the connectToRedis method. If the connection is successful, the Redis server would then act as a backing store for WebSocket connections in my application.
Then, the useWebSocketAdapter method sets the redisIoAdapter as the adapter for handling WebSocket connections in my NestJS application. This means that the NestJS application would use this adapter to manage all WebSocket connections, including broadcasting messages to clients.
In the context of a running application, this would mean that after these lines of code are executed, the application should be able to handle WebSocket connections and broadcast messages to clients using Redis as a store. This would be crucial since I need this application compatible with load-balancers. And after many many research I found out this could be the right solution to this. If a client makes a WebSocket connection request to the server, the server should be able to accept the connection, handle any messages sent over the connection, and also broadcast messages to multiple clients if needed.
Since the application hangs or does not complete execution, it indicates that there is a problem with setting up the WebSocket adapter.
The link I provided in "steps to reproduce" is basically the same as what I have, I just copy pasted them and I have modified the redis link. VSCode did not show any errors, so technically there shouldn't be any problem.
Package
Other package
No response
NestJS version
10.0.2
Packages versions
Node.js version
18.17 (Dockerized, image: node:18)
In which operating systems have you tested?
Other
Without these lines:
The console looks like this: (perfect, but websockets don't work)
https://kepkuldes.com/images/a3bacd06c8d794954ac599edebbbfba0.png
With the mentioned lines, it looks like this:
(and never continues, no matter how much time has passed since start) https://kepkuldes.com/images/bbf65778b2af7f621e88d7b92fa5a6ef.png
Most probably the redis URL I pass has no problems, since if I provide it badly an error appears in the console telling me time out or incorrect url. The link contains the password and the redis user as well.
(Similar like this
)
I would point out that if I try as of how digitalocean suggests me this link, so
rediss
instead ofredis
it just times out, so I modified it in my code to have redis:// etc...I attached you my main.ts file here. (renamed to txt because of github limitation) main.txt