2013-06-19 26 views
5

Tôi có một câu hỏi dễ dàng cho người dùng kết nối lại.Cửa hàng Connect-redis không hoạt động với socket.io

Tôi muốn sử dụng nó với socket.io với chức năng io.set('store', something). Tôi không biết tại sao, khi tôi làm

var RedisSessionStore = require('connect-redis')(express); 
var sessionStore = new RedisSessionStore(); 

app.use(express.session({ 
    secret: 'some totally secret key', 
    cookie: { 
    maxAge: 1000 * 60 * 60 
    }, 
    store: sessionStore 
})); 

//and then I wan't to use the session store for socket.io 
io.set('store', sessionStore); 

Nó nói Object #<RedisStore> has no method 'subscribe'

Trả lời

8

connect-redis là một cửa hàng phiên Redis hậu thuẫn cho Connect/Express, nhưng nó không phù hợp với 'cửa hàng giao thức' mà socket.io sử dụng .

Thay vào đó, bạn cần phải sử dụng việc thực hiện Redis cửa hàng vận chuyển với socket.io:

var SocketIoRedisStore = require('socket.io/lib/stores/redis'), 
    redis    = require('socket.io/node_modules/redis'); 
... 
io.set('store', new SocketIoRedisStore({ 
    redisPub : redis.createClient(), 
    redisSub : redis.createClient(), 
    redisClient : redis.createClient() 
})); 

(docs)

+1

Cảm ơn bạn! Tôi sợ vì tôi nghĩ rằng tôi sẽ phải tạo ra bộ thứ hai của pub/sub/client, một cho socket.io và một cho express. Bây giờ tôi thấy rằng 'new RedisSessionStore' chấp nhận một redis client như một tùy chọn. – Jakub

+0

Điều này có nghĩa rằng dòng này ở trên var RedisSessionStore = require ('connect-redis') (express); nên được loại bỏ khỏi tệp app.js hoặc chỉ cần đổi tên biến để tạo phần io.set? và nếu var bị xóa, bạn lưu trữ phiên thể hiện ở trên là gì? – Lion789

Các vấn đề liên quan