have redis working

This commit is contained in:
Nour Alharithi
2023-10-25 14:27:16 -07:00
parent 99cd43c012
commit 7181912607
9 changed files with 384 additions and 11 deletions

View File

@@ -0,0 +1,49 @@
import cors from 'cors';
import express from 'express';
import * as http from 'http';
import { Server } from 'socket.io';
import compression from 'compression';
import { sleep } from './utils/utils';
import { RedisClient } from './utils/redisClient';
const app = express();
app.use(cors({ origin: '*' }));
app.use(compression());
app.set('trust proxy', 1);
const server = http.createServer(app);
const io = new Server(server);
async function main() {
const redisClient = new RedisClient('localhost', '6379');
await redisClient.connect();
io.on('connection', (socket) => {
socket.on('subscribe', (channel) => {
console.log('Subscribing to channel', channel);
redisClient.client.subscribe(channel);
redisClient.client.on('message', (subscribedChannel, message) => {
console.log(message);
if (subscribedChannel === channel) {
socket.emit(channel, JSON.parse(message));
}
});
});
});
server.listen('3000', () => {
console.log('connection manager running on 3000');
});
}
async function recursiveTryCatch(f: () => void) {
try {
await f();
} catch (e) {
console.error(e);
await sleep(15000);
await recursiveTryCatch(f);
}
}
recursiveTryCatch(() => main());