From 2b0158673b1d5984a060e307710e9e1269d73a85 Mon Sep 17 00:00:00 2001 From: ragnarbull Date: Fri, 24 Nov 2023 11:45:35 +1100 Subject: [PATCH 1/5] Add docker-compose.yml & scripts needed for Redis --- docker-compose.yml | 7 +++++++ package.json | 7 +++++-- 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..1a92a007 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,7 @@ +version: "3" + +services: + redis: + image: redis:5 + ports: + - "6379:6379" \ No newline at end of file diff --git a/package.json b/package.json index c88eacdc..8b8a48bf 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,11 @@ "description": "Realtime chat app with rooms", "main": "server.js", "scripts": { - "start": "node server", - "dev": "nodemon server" + "docker-up": "docker-compose up -d", + "start": "npm run docker-up && node server", + "dev": "npm run docker-up && nodemon server", + "stop": "docker-compose down", + "docker-down": "docker-compose down -v --rmi all" }, "author": "Brad Traversy", "license": "MIT", From 0a60ec53b1fd007ed6c5ce6e288a8867926f6b47 Mon Sep 17 00:00:00 2001 From: ragnarbull Date: Fri, 24 Nov 2023 11:46:21 +1100 Subject: [PATCH 2/5] Fix: Redis error handling (adds reconnect retry) --- server.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/server.js b/server.js index 385db394..35043f17 100644 --- a/server.js +++ b/server.js @@ -22,12 +22,60 @@ const io = socketio(server); app.use(express.static(path.join(__dirname, "public"))); const botName = "ChatCord Bot"; +let pubClient, subClient; (async () => { - pubClient = createClient({ url: "redis://127.0.0.1:6379" }); - await pubClient.connect(); - subClient = pubClient.duplicate(); - io.adapter(createAdapter(pubClient, subClient)); + try { + pubClient = createClient({ url: "redis://127.0.0.1:6379" }); + await pubClient.connect(); + subClient = pubClient.duplicate(); + io.adapter(createAdapter(pubClient, subClient)); + console.log("Successfully connected to Redis adapter"); + } catch (err) { + if (err.code === 'ECONNREFUSED') { + console.warn("Redis server is not available. Make sure it's running."); + } else { + console.error("Error connecting to Redis:", err); + } + } + + let reconnectAttempts = 0; + const maxReconnectAttempts = 3; + + const handleRedisError = async (err, client) => { + if (err && err.name === 'AbortError') { + console.warn(`${client === pubClient ? 'pubClient' : 'subClient'}: Redis connection aborted.`); + } else if (err && err.errors && err.errors.length > 0) { + const firstError = err.errors[0]; + console.warn(`${client === pubClient ? 'pubClient' : 'subClient'}: ${firstError.message}`); + } else { + console.error(`${client === pubClient ? 'pubClient' : 'subClient'} error:`, err); + } + + // Reconnect retry mechanism + if (reconnectAttempts < maxReconnectAttempts) { + reconnectAttempts++; + console.log(`Reconnecting ${client === pubClient ? 'pubClient' : 'subClient'} (Attempt ${reconnectAttempts} of ${maxReconnectAttempts})...`); + try { + await client.connect(); + console.log(`Successfully reconnected to ${client === pubClient ? 'pubClient' : 'subClient'}`); + reconnectAttempts = 0; // resets the counter if reconnection is successfull + } catch (reconnectError) { + console.error(`Error reconnecting ${client === pubClient ? 'pubClient' : 'subClient'}:`, reconnectError); + } + } else { + console.error(`Max reconnect attempts reached. Shutting down the application.`); + process.exit(1); + } + }; + + pubClient.on('error', (err) => { + handleRedisError(err, pubClient); + }); + + subClient.on('error', (err) => { + handleRedisError(err, subClient); + }); })(); // Run when client connects From b4957e73777d42da3ff426b090c159ada719e94e Mon Sep 17 00:00:00 2001 From: ragnarbull Date: Fri, 24 Nov 2023 12:00:41 +1100 Subject: [PATCH 3/5] Update README.md to add info for setting up Docker and running/stopping the app --- README.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 49c11c06..dfe3f04c 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,28 @@ Realtime chat app with websockets using Node.js, Express and Socket.io with Vanilla JS on the frontend with a custom UI [![Run on Repl.it](https://repl.it/badge/github/bradtraversy/chatcord)](https://repl.it/github/bradtraversy/chatcord) ## Usage + +### To start the application + +Ensure Docker Desktop is up and running. +See [the Docker website](https://www.docker.com/products/docker-desktop/) to get started. + ``` npm install npm run dev - +``` Go to localhost:3000 + +### To shutdown the application & the Redis container + +``` +npm run stop +``` + +### To shutdown the application, Redis container & cleanup Redis data + +``` +npm run docker-down ``` ## Notes From a9aa900565094ffade1a752cc75f3548b639e36e Mon Sep 17 00:00:00 2001 From: ragnarbull Date: Fri, 24 Nov 2023 12:13:25 +1100 Subject: [PATCH 4/5] Specify project name in Docker --- docker-compose.yml | 7 ++++++- package.json | 6 +++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 1a92a007..09f1c4e2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,4 +4,9 @@ services: redis: image: redis:5 ports: - - "6379:6379" \ No newline at end of file + - "6379:6379" + volumes: + - redis_data:/data + +volumes: + redis_data: diff --git a/package.json b/package.json index 8b8a48bf..47ed4445 100644 --- a/package.json +++ b/package.json @@ -4,11 +4,11 @@ "description": "Realtime chat app with rooms", "main": "server.js", "scripts": { - "docker-up": "docker-compose up -d", + "docker-up": "docker-compose -p chatcord up -d", "start": "npm run docker-up && node server", "dev": "npm run docker-up && nodemon server", - "stop": "docker-compose down", - "docker-down": "docker-compose down -v --rmi all" + "stop": "docker-compose -p chatcord down", + "docker-down": "docker-compose -p chatcord down -v --rmi all" }, "author": "Brad Traversy", "license": "MIT", From 2e3ac5d19c638b7ce696a747ffb286b64a52b725 Mon Sep 17 00:00:00 2001 From: Joel Biddle Date: Fri, 24 Nov 2023 12:30:05 +1100 Subject: [PATCH 5/5] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dfe3f04c..70618ae7 100644 --- a/README.md +++ b/README.md @@ -14,13 +14,13 @@ npm run dev ``` Go to localhost:3000 -### To shutdown the application & the Redis container +### To shutdown the application & the Redis image (Docker container) ``` npm run stop ``` -### To shutdown the application, Redis container & cleanup Redis data +### To shutdown the application, Redis image (Docker container) & cleanup Redis volume data ``` npm run docker-down