From 7e8a27e140ddea52e2697028651bd1cfef4a9e36 Mon Sep 17 00:00:00 2001 From: mntmn Date: Mon, 8 Jan 2018 00:08:42 +0100 Subject: [PATCH] replace redis by in-memory object --- helpers/redis.js | 110 ++++++++++++++++++++++++++++++++++++++---- helpers/websockets.js | 23 ++++----- 2 files changed, 113 insertions(+), 20 deletions(-) diff --git a/helpers/redis.js b/helpers/redis.js index 90b440a..610742c 100644 --- a/helpers/redis.js +++ b/helpers/redis.js @@ -1,14 +1,103 @@ 'use strict'; -const RedisConnection = require('ioredis'); -const websockets = require('./websockets'); +var notRedis = { + state: {}, + topics: {}, + + publish: function(topic, msg, cb) { + //console.log("[notredis] publish",topic,msg); + if (!this.topics[topic]) { + this.topics[topic] = { + subscribers: [] + }; + } + var t=this.topics[topic]; + for (var i=0; i { cb(); }); }, - rateLimit(namespace, ip, cb) { + rateLimit: function(namespace, ip, cb) { const key = "limit_"+ namespace + "_"+ ip; const redis = this.connection; @@ -47,7 +136,7 @@ module.exports = { } }); }, - isOnlineInSpace(user, space, cb) { + isOnlineInSpace: function(user, space, cb) { this.connection.smembers("space_" + space._id.toString(), function(err, list) { if (err) cb(err); else { @@ -59,3 +148,6 @@ module.exports = { }); } }; + +return module.exports; + diff --git a/helpers/websockets.js b/helpers/websockets.js index 26fcd1c..9394913 100644 --- a/helpers/websockets.js +++ b/helpers/websockets.js @@ -3,18 +3,19 @@ require('../models/schema'); const WebSocketServer = require('ws').Server; -const Redis = require('ioredis'); const async = require('async'); const _ = require("underscore"); const mongoose = require("mongoose"); const crypto = require('crypto'); -module.exports = { - startWebsockets: function(server){ - this.setupSubscription(); - this.state = new Redis(6379, process.env.REDIS_PORT_6379_TCP_ADDR || 'localhost'); +var redis = require("./redis.js"); - if(!this.current_websockets){ +module.exports = { + startWebsockets: function(server) { + this.setupSubscription(); + this.state = redis.getConnection(); + + if(!this.current_websockets) { this.current_websockets = []; } @@ -117,8 +118,7 @@ module.exports = { }, setupSubscription: function() { - this.cursorSubscriber = new Redis(6379, process.env.REDIS_PORT_6379_TCP_ADDR || 'localhost'); - this.cursorSubscriber.subscribe(['cursors', 'users', 'updates'], function (err, count) { + this.cursorSubscriber = redis.getConnection().subscribe(['cursors', 'users', 'updates'], function (err, count) { console.log("[redis] websockets to " + count + " topics." ); }); this.cursorSubscriber.on('message', function (channel, rawMessage) { @@ -206,7 +206,7 @@ module.exports = { console.log("websocket not found to remove"); } - this.state.del(ws.id, function(err, res) { + this.state.del(ws.id+"", function(err, res) { if (err) console.error(err, res); else { this.removeUserInSpace(ws.space_id, ws, (err) => { @@ -221,7 +221,8 @@ module.exports = { addUserInSpace: function(username, space, ws, cb) { console.log("[websockets] user "+username+" in "+space.access_mode +" space " + space._id + " with socket " + ws.id); - this.state.set(ws.id, username, function(err, res) { + + this.state.set(ws.id+"", username+"", function(err, res) { if(err) console.error(err, res); else { this.state.sadd("space_" + space._id, ws.id, function(err, res) { @@ -238,7 +239,7 @@ module.exports = { }.bind(this)); }, removeUserInSpace: function(spaceId, ws, cb) { - this.state.srem("space_" + spaceId, ws.id, function(err, res) { + this.state.srem("space_" + spaceId, ws.id+"", function(err, res) { if (err) cb(err); else { console.log("[websockets] socket "+ ws.id + " went offline in space " + spaceId);