简体   繁体   English

在node.js中,如何使用socket.io和express设置redis?特别是使用RedisStore()

[英]In node.js, how do I setup redis with socket.io and express? Specifically using RedisStore()

First Problem 第一个问题
I'm trying to figure out sessions, stores, authorization, and redis. 我正在试图找出会话,商店,授权和redis。 If it's important, I am using Express@3.0.0rc4 . 如果这很重要,我使用的是Express@3.0.0rc4 I understand that I have two options to put my RedisStore() . 我知道我有两个选项来放置我的RedisStore() Which one do I use? 我用哪一个? Do I use both? 我同时使用两者吗?

express.session({secret: 'secret', key: 'key', store: new RedisStore()});
io.set('store', new RedisStore());

I already have node.js , express , and socket.io running. 我已经运行了node.jsexpresssocket.io . So now I'm trying to implement redis but I don't know how to implement authorization using sessions/stores and I don't know how to write to the redis database. 所以现在我正在尝试实现redis但我不知道如何使用会话/存储实现授权,我不知道如何写入redis数据库。 I haven't found any documentation on this. 我还没有找到任何相关文档。 I found a site that talks about sessions and stores using socket.io and express but no redis, and another one that talks about sessions and stores using all three , but it doesn't use io.set('store', ...) . 我找到了一个使用socket.io和express但没有redis讨论会话和商店的网站,另一个讨论使用所有三个会话和商店的网站,但它没有使用io.set('store', ...)

I also don't know if I should use two different stores, one for express and one for socket.io , or if I should just use one. 我也不知道我是否应该使用两个不同的商店,一个用于express ,一个用于socket.io ,或者我应该只使用一个。 Look at the example for clarification: 请查看示例以获得澄清:

//Redis Variables
var redis = require('socket.io/node_modules/redis');
var RedisStore = require('socket.io/lib/stores/redis');
var pub = redis.createClient();
var sub = redis.createClient();
var client = redis.createClient();
var redis_store = new RedisStore({
                        redisPub: pub,
                        redisSub: sub,
                        redisClient: client
                      });

app.configure(function(){
  //...code goes here...
  app.use(express.session({
                    secret: 'secret',
                    key: 'key',
                    store: redis_store  //Notice I'm using redis_store
                  }));
  //...more code...
});

io.configure(function(){
  io.set('store', redis_store);  //Notice it's the same RedisStore() being used
});

Do I use the same RedisStore() for each? 我是否每个都使用相同的RedisStore() Do I create seperate ones for each? 我是否为每个创建单独的? Do I just use express or socket.io ? 我只使用expresssocket.io吗? What I really want is to be able to authenticate clients (I assume that's done through sessions) and have them update the redis database when they connect - keeping a log of when people accessed my site. 我真正想要的是能够验证客户端(我假设通过会话完成)并让他们在连接时更新redis数据库 - 记录人们访问我的网站的时间。 Which leads to my second problem. 这导致了我的第二个问题。


Second Problem 第二个问题
So I have no idea how to access and edit the redis database from this point. 所以我不知道如何从这一点访问和编辑redis数据库。 I haven't been able to test this because of my first problem but I assume it would be something like this: 由于我的第一个问题,我无法测试这个,但我认为它会是这样的:

 io.sockets.on('connection', function(socket){ var session = socket.handshake.session; redis.put(session); }); 

I also haven't seen any documentation on how to update a redis database from within node.js so I highly doubt that redis.put() is the correct terminology haha. 我还没有看到任何关于如何从node.js中更新redis数据库的文档,所以我非常怀疑 redis.put()是正确的术语哈哈。 I have visited redis's website but I can't find commands for node.js. 我访问过redis的网站,但我找不到node.js的命令。 Just commands for using regular redis from the command line. 只是命令从命令行使用常规redis。 Anyways, if someone could at least point me in the right direction that would be great. 无论如何,如果有人能够至少指出我正确的方向,那将是伟大的。 Thanks. 谢谢。 :) :)

Express and Socket.IO have their own integration with Redis for session management, as you've seen. 正如您所见,Express和Socket.IO有自己的集成Redis进行会话管理。 It is designed as a blackbox integration, the idea being that the session store implementation is independent from the rest of your code. 它被设计为黑盒集成,其理念是会话存储实现独立于其他代码。 Since it's independent, that means you can't go in and use express or socket.io to access Redis directly. 由于它是独立的,这意味着您无法进入并使用express或socket.io直接访问Redis。 You'll need to add a regular redis client like node_redis. 您需要添加一个常规的redis客户端,如node_redis。 The benefit is you don't have to worry about making all those redis calls yourself, instead you'll be interacting with express or socket.io's session store interfaces. 好处是您不必担心自己进行所有redis调用,而是要与express或socket.io的会话存储接口进行交互。

So in your #1 case, you could pass in a single new instance of RedisStore, not two new ones as you've done. 所以在你的#1情况下,你可以传递一个新的RedisStore实例,而不是你已经完成的两个新实例。 Or you could follow your second link and have socket.io listen through express. 或者你可以按照你的第二个链接,让socket.io通过快递听。 In that case it would integrate with express session management. 在这种情况下,它将与快速会话管理集成。 That's why you don't see the extra io.set('store') call in that example. 这就是为什么在这个例子中你没有看到额外的io.set('store')调用的原因。

It'll probably seem redundant to you, but try to think of RedisStore as a special client designed only for session management. 这对你来说似乎是多余的,但试着将RedisStore视为专为会话管理而设计的特殊客户端。 Even thought RedisStore probably relies on something like node_redis, you shouldn't try to access it. 即使认为RedisStore可能依赖于node_redis之类的东西,也不应该尝试访问它。 You have to include another library for accessing your redis database directly, assuming you wanted to store other non-session items in redis in the first place. 您必须包含另一个库才能直接访问您的redis数据库,假设您希望首先将其他非会话项存储在redis中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM