Skip to content

Commit

Permalink
feat:impliment redise session and manage cuncurrent users
Browse files Browse the repository at this point in the history
  • Loading branch information
vishakh-abhayan committed Nov 23, 2024
1 parent 591c12f commit 3105f5f
Show file tree
Hide file tree
Showing 8 changed files with 919 additions and 291 deletions.
362 changes: 361 additions & 1 deletion package-lock.json

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@
"author": "",
"license": "ISC",
"dependencies": {
"compression": "^1.7.5",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.21.1",
"express-rate-limit": "^7.4.1",
"groq-sdk": "^0.8.0",
"helmet": "^8.0.0",
"ioredis": "^5.4.1",
"node-cache": "^5.1.2",
"nodemon": "^3.1.7",
"ws": "^8.18.0"
"util": "^0.12.5",
"uuid": "^11.0.3",
"ws": "^8.18.0",
"zustand": "^5.0.1"
}
}
23 changes: 22 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,34 @@ const WebSocket = require("ws");
const cors = require("cors");
const config = require("./config/config");
const setupWebSocket = require("./routes/websocket");
const helmet = require("helmet");
const rateLimit = require("express-rate-limit");
const compression = require("compression");
const NodeCache = require("node-cache");

const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
const wss = new WebSocket.Server({
server,
clientTracking: true,
maxPayload: 50 * 1024,
});
const myCache = new NodeCache({ stdTTL: 100 });

app.use(cors());
app.use(express.json());
app.use(helmet());
app.use(compression());
app.use(
rateLimit({
windowMs: 15 * 60 * 1000,
max: 100,
})
);

process.on("uncaughtException", (error) => {
console.error("Uncaught Exception:", error);
});

app.get("/v1/status", (req, res) => {
res.json({ status: "ok" });
Expand Down
24 changes: 24 additions & 0 deletions src/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,28 @@ require("dotenv").config();
module.exports = {
port: process.env.PORT || 8000,
groqApiKey: process.env.GROQ_API_KEY,
redis: {
host: process.env.REDIS_HOST || "127.0.0.1", // Use IP instead of localhost
port: process.env.REDIS_PORT || 6379,
password: process.env.REDIS_PASSWORD,
retryStrategy: function (times) {
const delay = Math.min(times * 50, 2000);
return delay;
},
maxRetriesPerRequest: 3,
enableReadyCheck: true,
reconnectOnError: function (err) {
const targetError = "READONLY";
if (err.message.includes(targetError)) {
return true;
}
return false;
},
},
limits: {
maxConnections: 100,
maxConnectionsPerIP: 5,
sessionTTL: 3600, // 1 hour
messageRateLimit: 60, // messages per minute
},
};
Loading

0 comments on commit 3105f5f

Please sign in to comment.