如何设置RedisStore

如何设置RedisStore

本文介绍了如何设置RedisStore - Node,Express,Socket.io,Heroku的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Node& Express 4.0部署在Heroku上,我试图用Redis实现Socket.io作为会话存储。所以我有这样作为我当前的代码:

  var app = express(); 
var server = require('http')。createServer(app);
var io = require('socket.io')。listen(server);
var RedisStore = io.RedisStore;

if(process.env.REDISTOGO_URL){
// if if
var rtg = require(url)。parse(process.env.REDISTOGO_URL);
var redis = require(redis)。createClient(rtg.port,rtg.hostname);

redis.auth(rtg.auth.split(:)[1]);
} else {
var redis = require(redis)。createClient();
}

/ **初始化RedisStore for socket.io ** /
io.set('store',new RedisStore({
redis:redis
}));

但是我收到以下错误:

  14:25:03 web.1 | io.set('store',new RedisStore({
14:25:03 web.1 | ^
14:25:03 web.1 | TypeError:undefined不是一个函数






我也看过这样定义RedisStore的方式: p>

  var redis = require('socket.io/node_modules/redis'); 
var RedisStore = require(' io / lib / stores / redis');

然而,我的安装版本的socket.io已安装使用 npm install --save socket.io ,在 lib中不包含 store 目录:








编辑



关于它们的1.0版本的socket.io页面:

  // 2.实现socket.io-redis适配器

var io = require('socket.io')(3000);
var redis = require('socket.io-redis');
io.adapter(redis({host:'localhost',port:6379}));

但是,我没有找到关于这个新模块的其他文档,因为我是新来的整个堆栈,我不认为我可以自己弄清楚。

解决方案

node.js模块的趋势是删除不是模块核心的功能。



这是为什么socket.io 1.0不再支持redis开箱即用。



所以第一步是跟踪你需要的功能。




  1. https://github.com/Automattic/socket.io-redis

然后你需要安装另一个模块 npm install socket.io-redis --save



最后配置你的应用程序。

  var app = express(); 
var server = require('http')。createServer(app);
var io = require('socket.io')。listen(server);
var redis = require('socket.io-redis');

io.adapter(redis(process.env.REDISTOGO_URL));

很好的部分是socket.io-redis适配器接受redis urls并默认为localhost:6379 so你(应该)能够简单地通过 REDISTOGO_URL


I'm using Node & Express 4.0 deployed on Heroku, and I'm trying to implement Socket.io with Redis as aa session store. So I have this as my current code:

 var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var RedisStore = io.RedisStore;

if (process.env.REDISTOGO_URL) {
    // inside if statement
    var rtg   = require("url").parse(process.env.REDISTOGO_URL);
    var redis = require("redis").createClient(rtg.port, rtg.hostname);

    redis.auth(rtg.auth.split(":")[1]);
} else {
    var redis = require("redis").createClient();
}

/** Initialize RedisStore for socket.io **/
io.set('store', new RedisStore({
  redis    : redis
}));

But I get the following error:

14:25:03 web.1  | io.set('store', new RedisStore({
14:25:03 web.1  |                 ^
14:25:03 web.1  | TypeError: undefined is not a function


I've also seen this way of defining a RedisStore:

var redis = require('socket.io/node_modules/redis');
var RedisStore = require('socket.io/lib/stores/redis');

However, my installed version of socket.io, installed using npm install --save socket.io, doesn't include stores in the lib directory:


EDIT

I saw this on the socket.io page in regards to their 1.0 release:

// 2. Implement the socket.io-redis adapter

var io = require('socket.io')(3000);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));

But there's no other documentation I could find regarding this new module, and since I'm new to this whole stack, I don't think I could figure it out on my own.

解决方案

The trend among node.js modules is to remove functionality that isn't truly core to the module.

Which is why socket.io 1.0 no longer supports redis out of the box.

So step one is to track down the functionality you need.

  1. http://socket.io/docs/server-api/
  2. https://github.com/Automattic/socket.io-adapter
  3. https://github.com/Automattic/socket.io-redis

Then you need to install the other module npm install socket.io-redis --save

And finally configure your app.

var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var redis = require('socket.io-redis');

io.adapter(redis(process.env.REDISTOGO_URL));

The nice part is the socket.io-redis adapter accepts redis urls and defaults to localhost:6379 so you (should) be able to simple pass in the REDISTOGO_URL

这篇关于如何设置RedisStore - Node,Express,Socket.io,Heroku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 03:21