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

时间:2022-08-22 15:48:32

First Problem
I'm trying to figure out sessions, stores, authorization, and redis. If it's important, I am using Express@3.0.0rc4. I understand that I have two options to put my RedisStore(). Which one do I use? Do I use both?

第一个问题我正试图弄清楚会话,商店,授权和redis。如果这很重要,我使用的是Express@3.0.0rc4。我知道我有两个选项来放置我的RedisStore()。我用哪一个?我同时使用两者吗?

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

I already have node.js, express, and socket.io running. 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. 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', ...).

我已经运行了node.js,express和socket.io.所以现在我正在尝试实现redis,但我不知道如何使用会话/存储实现授权,我不知道如何写入redis数据库。我没有找到任何关于此的文件。我找到了一个使用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. Look at the example for clarification:

我也不知道是否应该使用两个不同的商店,一个用于快递,一个用于socket.io,或者我应该只使用一个。请查看示例以获得澄清:

//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? Do I create seperate ones for each? Do I just use express or socket.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. Which leads to my second problem.

我是否每个都使用相同的RedisStore()?我是否为每个创建单独的?我只使用express或socket.io吗?我真正想要的是能够对客户端进行身份验证(我假设通过会话完成)并让他们在连接时更新redis数据库 - 记录人们访问我的站点的时间。这导致了我的第二个问题。


Second Problem
So I have no idea how to access and edit the redis database from this point. 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. I have visited redis's website but I can't find commands for node.js. Just commands for using regular redis from the command line. Anyways, if someone could at least point me in the right direction that would be great. Thanks. :)

我还没有看到任何关于如何从node.js中更新redis数据库的文档,所以我非常怀疑redis.put()是正确的术语哈哈。我访问过redis的网站但我找不到node.js的命令。只是命令从命令行使用常规redis。无论如何,如果有人能够至少指出我正确的方向,那将是伟大的。谢谢。 :)

1 个解决方案

#1


3  

Express and Socket.IO have their own integration with Redis for session management, as you've seen. 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. You'll need to add a regular redis client like 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.

正如您所见,Express和Socket.IO有自己的集成Redis进行会话管理。它被设计为黑盒集成,其理念是会话存储实现独立于其他代码。由于它是独立的,这意味着您无法进入并使用express或socket.io直接访问Redis。您需要添加一个常规的redis客户端,如node_redis。好处是您不必担心自己进行所有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. Or you could follow your second link and have socket.io listen through express. 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.

所以在你的#1情况下,你可以传递一个新的RedisStore实例,而不是你已经完成的两个新实例。或者你可以关注你的第二个链接并让socket.io通过express听。在这种情况下,它将与快速会话管理集成。这就是为什么你在这个例子中没有看到额外的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. Even thought RedisStore probably relies on something like node_redis, you shouldn't try to access it. 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.

这对你来说似乎是多余的,但试着将RedisStore视为专为会话管理而设计的特殊客户端。即使认为RedisStore可能依赖于node_redis之类的东西,也不应该尝试访问它。您必须包含另一个库才能直接访问您的redis数据库,假设您希望首先在redis中存储其他非会话项。

#1


3  

Express and Socket.IO have their own integration with Redis for session management, as you've seen. 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. You'll need to add a regular redis client like 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.

正如您所见,Express和Socket.IO有自己的集成Redis进行会话管理。它被设计为黑盒集成,其理念是会话存储实现独立于其他代码。由于它是独立的,这意味着您无法进入并使用express或socket.io直接访问Redis。您需要添加一个常规的redis客户端,如node_redis。好处是您不必担心自己进行所有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. Or you could follow your second link and have socket.io listen through express. 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.

所以在你的#1情况下,你可以传递一个新的RedisStore实例,而不是你已经完成的两个新实例。或者你可以关注你的第二个链接并让socket.io通过express听。在这种情况下,它将与快速会话管理集成。这就是为什么你在这个例子中没有看到额外的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. Even thought RedisStore probably relies on something like node_redis, you shouldn't try to access it. 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.

这对你来说似乎是多余的,但试着将RedisStore视为专为会话管理而设计的特殊客户端。即使认为RedisStore可能依赖于node_redis之类的东西,也不应该尝试访问它。您必须包含另一个库才能直接访问您的redis数据库,假设您希望首先在redis中存储其他非会话项。