@sync-in/server
Version:
The secure, open-source platform for file storage, sharing, collaboration, and sync
61 lines (60 loc) • 2.67 kB
JavaScript
/*
* Copyright (C) 2012-2025 Johan Legrand <johan.legrand@sync-in.com>
* This file is part of Sync-in | The open source file sync and share solution
* See the LICENSE file for licensing details
*/ "use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "RedisAdapter", {
enumerable: true,
get: function() {
return RedisAdapter;
}
});
const _common = require("@nestjs/common");
const _platformsocketio = require("@nestjs/platform-socket.io");
const _functions = require("../../../common/functions");
let RedisAdapter = class RedisAdapter extends _platformsocketio.IoAdapter {
async connectToRedis(redisUrl) {
const { createAdapter } = await (0, _functions.loadOptionalModule)('@socket.io/redis-adapter');
const { createClient } = await (0, _functions.loadOptionalModule)('redis');
const pubClient = createClient({
url: redisUrl,
socket: {
noDelay: true,
reconnectStrategy: this.reconnectStrategy
}
});
const subClient = pubClient.duplicate();
pubClient.on('error', (e)=>this.logger.error(`PubClient: ${e.message || e}`));
pubClient.on('ready', ()=>this.logger.log(`PubClient: Connected to Redis Server at ${pubClient.options.url}`));
subClient.on('error', (e)=>this.logger.error(`SubClient: ${e.message || e}`));
subClient.on('ready', ()=>this.logger.log(`SubClient: Connected to Redis Server at ${subClient.options.url}`));
pubClient.connect();
subClient.connect();
this.adapterConstructor = createAdapter(pubClient, subClient);
}
createIOServer(port, options) {
const server = super.createIOServer(port, options);
server.adapter(this.adapterConstructor);
return server;
}
constructor(...args){
super(...args), this.logger = new _common.Logger('WebSocketAdapter'), this.reconnectOptions = {
maxAttempts: 3,
minConnectDelay: 6000,
maxConnectDelay: 30000
}, this.reconnectStrategy = (attempts)=>{
if (attempts > this.reconnectOptions.maxAttempts) {
this.logger.error('Too many retries on Redis server. Exiting');
process.exit();
} else {
const wait = Math.min(this.reconnectOptions.minConnectDelay * Math.pow(2, attempts), this.reconnectOptions.maxConnectDelay);
this.logger.warn(`Retrying connection to Redis server in ${wait / 1000}s`);
return wait;
}
};
}
};
//# sourceMappingURL=redis.adapter.js.map